更改 Laravel 刀片分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17363150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:02:44  来源:igfitidea点击:

Changing Laravel Blade Delimiter

laravellaravel-4

提问by Chris Breslaw

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.phpwhich affect whole application.

但是我不知道我应该把它放在哪里,这样它只会影响单个刀片模板,而不是将它放在app/start/global.php影响整个应用程序的地方。

回答by Dwight

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.

如果您想在应用程序布局中使用普通标签{{}}但在嵌套视图中使用自定义标签,这可能是一个问题- 我不确定最好的方法是什么。

回答by Stalinko

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

带有Blade::setEscapedContentTags/的解决方案Blade::setContentTags在最新版本的 Laravel 中不起作用(在 5.6 中检查)。

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

推荐的方法是(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 @verbatimdirective so that you do not have to prefix each Blade echo statement with an @symbol:

@verbatim
    <div class="container">
        Hello, {{ name }}.
    </div> 
@endverbatim

Blade 和 JavaScript 框架

由于许多 JavaScript 框架还使用“花括号”来指示应在浏览器中显示给定的表达式,因此您可以使用该@符号来通知 Blade 渲染引擎该表达式应保持不变。例如:

Hello, @{{ name }}.

在这个例子中,@符号将被 Blade 删除;然而,{{ name }}Blade 引擎不会改变表达式,而是允许它由您的 JavaScript 框架呈现。

@verbatim 指令

如果您在模板的大部分中显示 JavaScript 变量,您可以将 HTML 包装在 @verbatim指令中,这样您就不必在每个 Blade echo 语句前添加@符号:

@verbatim
    <div class="container">
        Hello, {{ name }}.
    </div> 
@endverbatim

回答by AxissXs

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

只需使用@verbatim指令。将整个代码包裹在其中,blade 将忽略所有花括号。