laravel 如果值未设置刀片,如何回显默认值

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

How to echo a default value if value not set blade

laravellaravel-4laravel-5bladetemplating

提问by paquettg

I would like to know what would be the best way to display a default value if the given value is not set. I have the following in a blade file (I can not guaranty that the key is set, it depends on a multitude of factors).

如果未设置给定值,我想知道显示默认值的最佳方式是什么。我在刀片文件中有以下内容(我不能保证设置了密钥,这取决于多种因素)。

{{ $foo['bar'] }}

I would know if the following is the best way to go about it,

我想知道以下是否是最好的方法,

{{ (isset($foo['bar']) ? $foo['bar'] : 'baz' }}

or is there a better way to do this?

或者有更好的方法来做到这一点?

Thanks :)

谢谢 :)

回答by Jazerix

Use php's null coalesce operator:

使用 php 的 null 合并运算符:

{{ $variable ?? "Default Message" }}


Removed as of Laravel 5.7

从 Laravel 5.7 开始移除

With Laravel 4.1-5.6 you could simply do it like this:

使用 Laravel 4.1-5.6 你可以简单地这样做:

{{ $variable or "Default Message" }}

It's the same as:

它与以下内容相同:

echo isset($variable) ? $variable : 'Default Message'; 

回答by joemaller

PHP 5.3's ternary shortcut syntax works in Blade templates:

PHP 5.3 的三元快捷语法适用于 Blade 模板:

{{ $foo->bar ?: 'baz' }}

It won't work with undefined top-level variables, but it's great for handling missing values in arrays and objects.

它不适用于未定义的顶级变量,但它非常适合处理数组和对象中的缺失值。

回答by Edmund Sulzanok

Since Laravel 5.7 {{$Variable or "Default Message"}}throws $Variable is not defined. This {{$Variable ?? "Default Message"}}works though.

由于 Laravel 5.7{{$Variable or "Default Message"}}抛出$Variable is not defined. 这{{$Variable ?? "Default Message"}}虽然有效。

回答by Chris Bier

I recommend setting the default value in your controller instead of making a work-around in your view.

我建议在您的控制器中设置默认值,而不是在您的视图中进行变通。

This is the best way because it keeps logic out of your view, and keeps the view's markup clean.

这是最好的方法,因为它将逻辑排除在您的视图之外,并保持视图的标记干净。

For example in your controller, before passing data to the view:

例如在您的控制器中,在将数据传递给视图之前:

if(!isset($foo['bar'])){
     $foo['bar'] = 'baz';
}

回答by twmbx

While Chris B's answer is perfectly valid; I felt perhaps this is a question that can have an alternative answer. Some would prefer not to make their controllers "fat" and in this case at least, the use of a Presentercould be the answer you seek for allowing a great deal of flexibility in your application views.

虽然 Chris B 的回答是完全有效的;我觉得也许这是一个可以有其他答案的问题。有些人不想让他们的控制器变得“胖”,至少在这种情况下,Presenter的使用可能是您寻求在应用程序视图中提供极大灵活性的答案。

Take a look at the following project/packageon Github. The readme is pretty robust with a number of examples to get you going.

在 Github 上查看以下项目/包。自述文件非常强大,有许多示例可以帮助您前进。

It will allow you to do just what you asked and simply call

它可以让你做你所要求的,只需调用

{{ $foo['bar'] }}

in your view.

在你看来。

回答by Laurel

Usually I use the null coalescing operator (introduced in PHP 7.0):

通常我使用空合并运算符(在 PHP 7.0 中引入):

{{ $foo['bar'] ?? 'baz' }}

However, if 'baz' is actually a long expression (such as several lines of HTML, possibly with some PHP variables), then I use Blade‘s @isset(@elseis optional but works):

但是,如果 'baz' 实际上是一个长表达式(例如几行 HTML,可能带有一些 PHP 变量),那么我使用 Blade's @isset@else是可选的,但有效):

@isset($foo['bar'])
    {{ $foo['bar'] }}
@else
    {{ $b }}
    <br>
    {{ $a }}
    <br>
    {{ $z }}
@endisset

(Taking a little liberty with the example, obviously.)

(显然,这个例子有点随意。)

There's also @empty($expr)which has similar syntax but works the same as the PHP function of the same name. This may be helpful: In where shall I use isset() and !empty()

还有@empty($expr)它具有类似的语法,但与同名的 PHP 函数的工作方式相同。这可能会有所帮助:我应该在哪里使用 isset() 和 !empty()