问题
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