Changing Laravel Blade Delimiter

三世轮回 提交于 2019-12-21 09:19:10

问题


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.


回答1:


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.




回答2:


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



回答3:


Simply use @verbatim directive.wrap your whole code in it and blade will just ignore all the curly braces.



来源:https://stackoverflow.com/questions/17363150/changing-laravel-blade-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!