如果未设置刀片,如何回显默认值 [英] How to echo a default value if value not set blade

查看:71
本文介绍了如果未设置刀片,如何回显默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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' }}

或者有更好的方法吗?

谢谢:)

推荐答案

使用php的null合并运算符:

Use php's null coalesce operator:

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


从Laravel 5.7版本中删除

使用Laravel 4.1-5.6,您可以像这样简单地做到这一点:

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

{{ $variable or "Default Message" }}

与以下相同:

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

这篇关于如果未设置刀片,如何回显默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆