Laravel Blade 模板中的全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22686029/
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 09:16:01  来源:igfitidea点击:

Global variables in Laravel Blade template

laravellaravel-4blade

提问by web rocker

I hope to define global variable in a separate file and then inject in wherever I want, so for Laravel 4 + Blade, I did this.

我希望在一个单独的文件中定义全局变量,然后在我想要的任何地方注入,所以对于 Laravel 4 + Blade,我这样做了。

Top file example.blade.php:

顶级文件example.blade.php:

@include('head')    
@include('body')

head.blade.php

头刃.php

$STYLE_PATH_CSS="styleFolder/foundation/css" 
$STYLE_PATH_JS="styleFolder/foundation/js" 
$STYLE_PATH_JS_VENDOR="styleFolder/foundation/js/vendor"
<link rel="stylesheet" href=" <?php echo $STYLE_PATH_CSS ?>/normalize.css ">
<link rel="stylesheet" href=" <?php echo $STYLE_PATH_CSS ?>/foundation.css ">
<link rel="stylesheet" href=" <?php echo $STYLE_PATH_JS_VENDOR ?>/modernizr.js "> 

body.blade.php

body.blade.php

<script src="styleFolder/foundation/js/vendor/jquery.js"></script>
<script src="styleFolder/foundation/js/foundation.min.js"></script>

Until then, it works fine. Then I hope to simplify one more time: replace the duplicate path by global variable. So,

在那之前,它工作正常。那么我希望再简化一次:用全局变量替换重复的路径。所以,

variables_G.blade.php

变量_G.blade.php

<?php  $STYLE_PATH_CSS="styleFolder/foundation/css" ?>
<?php  $STYLE_PATH_JS="styleFolder/foundation/js" ?>
<?php  $STYLE_PATH_JS_VENDOR="styleFolder/foundation/js/vendor" ?>

and new head.blade.php

和新的 head.blade.php

@include('variables_G.blade.php')
<link rel="stylesheet" href=" <?php echo $STYLE_PATH_CSS ?>/normalize.css ">
<link rel="stylesheet" href=" <?php echo $STYLE_PATH_CSS ?>/foundation.css ">
<link rel="stylesheet" href=" <?php echo $STYLE_PATH_JS_VENDOR ?>/modernizr.js "> 

then it can't find variables' value. Error message:

那么它找不到变量的值。错误信息:

Undefined variable: STYLE_PATH_CSS.

Can anyone help me out?

谁能帮我吗?

回答by Antonio Carlos Ribeiro

Views are supposed to be dumb, this means also that you should not create variables inside of one to give to another, you should not give too much resposibility to a view.

视图应该是愚蠢的,这也意味着你不应该在一个内部创建变量给另一个,你不应该给一个视图太多的责任。

The best way to provide global variables to Blade Views and make sure they will be available at any time is using View::share():

为 Blade Views 提供全局变量并确保它们随时可用的最佳方法是使用View::share()

View::share('STYLE_PATH_CSS', 'styleFolder/foundation/css');
View::share('STYLE_PATH_JS', 'styleFolder/foundation/js');
View::share('STYLE_PATH_JS_VENDOR', 'styleFolder/foundation/js/vendor');

You can add this to a composers.php file and load it in your app/start/global.php:

您可以将其添加到 composers.php 文件并将其加载到您的app/start/global.php

require app_path().'/composers.php';

Take a look at the docs: http://laravel.com/docs/responses#views

看看文档:http: //laravel.com/docs/responses#views

回答by ryanwinchester

View::share();is the best way to make a global variable.

View::share();是制作全局变量的最佳方式。

View::share('name', 'Steve');

To make sure a certain variable is included every time a certain view is rendered, I like View Composers

为了确保每次渲染某个视图时都包含某个变量,我喜欢View Composers

You can include stuff when certain views are rendered. I suppose you could use it on your master layout(s), to make things almostglobal. It gives more control, which would be good if you need something widely available, but if it is sensitive and you don't want it everywhere.

您可以在呈现某些视图时包含内容。我想你可以在你的主布局上使用它,使事情几乎是全球性的。它提供了更多的控制权,如果您需要一些广泛可用的东西,这会很好,但如果它很敏感并且您不希望它无处不在。

View::composer('profile', function($view)
{
    $view->with('count', User::count());
});