Unisys12's Blog

My adventures, trials & tribulations of learning web development as a hobby! I mean... who does that?


Correcting Blade Syntax

Phillip Jackson - 2014-09-25 02:14:33
How to change Blades default syntax

What the Hell?

So, for some reason, Taylor thought it would be funny to go and change the default Blade styntax from {{ }} to {!! !!} and I really don't know why. Not funny Taylor!!

Here is what the new syntax looks like:

{!! Form::open() !!}

    {!! Form::label('thing', 'This is a thing!') !!}
    {!! Form::text('thing') !!}

{!! Form::close() !!}

Yeah, that's pretty dumb!

If you followed my last post on Getting Blade Back Into Laravel or you already have Blade installed, take a look at vendor/laravel/framework/src/illuminate/View/Compilers/BladeCompiler.php, we can see on lines 33 - 52 where the changes have been made:

    /**
     * Array of opening and closing tags for escaped echos.
     *
     * @var array
     */
    protected $rawTags = array('{!!', '!!}');

    /**
     * Array of opening and closing tags for escaped echos.
     *
     * @var array
     */
    protected $contentTags = array('{{', '}}');

    /**
     * Array of opening and closing tags for escaped echos.
     *
     * @var array
     */
    protected $escapedTags = array('{{{', '}}}');

We Can Fix This!

Ok, let's simmer down a bit. Take a breath. There ya go! We have been able to do this in AngularJS for sometime, with very little effort, using the $interpolateProvider like so...

var employeeApp = angular.module('employeeApp', [], function($interpolateProvider) {
    $interpolateProvider.startSymbol('{%');
    $interpolateProvider.endSymbol('%}');
});

We can actually do something similar, but will far less code. If you look closely at the above section of code from BladeCompiler.php, the older syntax now simply doubles in escaping content in the view. So all we really have to change is the rawTags variable. We do not want to manipulate a vendor file, so if we look down the same file to line 712, we see a method called setRawTags. All we need to do is call this method and pass it the needed characters. In my app/Http/routes.php file, I added the following line at the top of the file:

// Return Blade to Old Syntax
\Blade::setRawTags('{{', '}}');

$router->get('/', 'HomeController@index');

With that, everything should be back to basically normal. But I do have a few other tips...

  • If you find that after adding the above line to your routes.php file and refreshing does not seem to have an affect on your Blade templates, navigate to app/storage/views and delete any rendered files in the directory. They will have a name similar to 067986f7d2f1123f84e202126329a79e. Just delete these files, return your browser and refresh the page. Everything should render perfectly now.
  • Some have suggested placing the above line in app/Providers/AppServiceProviders.php within the boot() method. And I have no problem with that, except it tended to really slow down the rendering process on my box. Maybe I am a isolated incident, but it really got annoying. Placing the line of code in the routes file, although it seems a little hacky, does the job just fine.

Ok! Now go code something!

LARAVEL 5 BETA, LARAVEL 5, BLADE
comments powered by Disqus