Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Laravel Blade Delimiter

I know that you can change the default blade delimiter using

Blade::setEscapedContentTags('[[', ']]');
Blade::setContentTags('[[[', ']]]');

However I don't know where should I put it so that it only affect single blade template as opposed to putting it at app/start/global.php which affect whole application.

like image 781
Chris Breslaw Avatar asked Jun 28 '13 10:06

Chris Breslaw


People also ask

How to create a custom blade directive in Laravel?

How to Create Custom Blade Directive in Laravel? 1 Step 1: Create Custom Blade Directive 2 Step 2: Create Route 3 Step 3: Create Blade File 4 Step 4: Run Development Server More ...

What is Laravel blade?

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.

What is layout in Laravel?

Layouts are often the starting point of many web development projects. Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template.

How do I disable double encoding in Laravel?

By default, Blade (and the Laravel ehelper) will double encode HTML entities. If you would like to disable double encoding, call the Blade::withoutDoubleEncodingmethod from the bootmethod of your AppServiceProvider:


2 Answers

If you only want to use different tags for a single view, you can set the tags in the closure or controller action that will generate the view.

Route::get('/', function()
{
    Blade::setEscapedContentTags('[[', ']]');
    Blade::setContentTags('[[[', ']]]');

    return View::make('home');
});

This could be an issue if you want to use the normal tags {{ and }} in an application layout but your custom ones in a nested view - I'm not sure what the best approach there would be.

like image 37
Dwight Avatar answered Oct 11 '22 16:10

Dwight


The solution with Blade::setEscapedContentTags / Blade::setContentTags doesn't work in the latest versions of Laravel (checked at 5.6).

The recommended approach is (https://laravel.com/docs/5.6/blade#blade-and-javascript-frameworks):

Blade & JavaScript Frameworks

Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the @ symbol to inform the Blade rendering engine an expression should remain untouched. For example:

Hello, @{{ name }}.

In this example, the @symbol will be removed by Blade; however, {{ name }} expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.

The @verbatim Directive

If you are displaying JavaScript variables in a large portion of your template, you may wrap the HTML in the @verbatim directive so that you do not have to prefix each Blade echo statement with an @ symbol:

@verbatim
    <div class="container">
        Hello, {{ name }}.
    </div> 
@endverbatim
like image 125
Stalinko Avatar answered Oct 11 '22 17:10

Stalinko