php - Laravel 5.4+ blades 新组件的默认值?

标签 php laravel laravel-5 laravel-5.4

Laravel 5.4+ 允许使用以下结构的组件:

<div class="alert alert-{{ $type }}">
    <div class="alert-title">{{ $title }}</div>
    {{ $slot }}
</div>

它们被称为:

@component('alert', ['type' => 'danger'])
    @slot('title') 
        oh no 
    @endslot
    Foo
@endcomponent

是否有速记或 Blade 标记来为传入的变量设置默认值?

例如,在上面,如果我不传入“type”,我会得到一个undefined variable错误。我可以做类似的事情:

<div class="alert alert-{{ isset($type) ? $type : 'default' }}">

但是上面的内容感觉很冗长,尤其是当变量在多个位置使用时。

最佳答案

我不这么认为。

话虽如此,blade 具有包装 isset() 调用 ( see docs ) 的 or 运算符。

<div class="alert alert-{{ $type or 'default' }}">

如果您运行的是 php 7,这就是 Null coalescing operator 的情况.

<div class="alert alert-{{ $type ?? 'default' }}">

两者都会使您的调用站点更整洁。

关于php - Laravel 5.4+ blades 新组件的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42966549/

相关文章:

mysql - 使用 laravel 查询生成器在另一个表中使用列值

php - 使用 jQuery AJAX 从 Laravel 5 中的联系表单发送电子邮件

php - laravel blade 使用 substr 方法禁用转义

php - 检测提交文本中使用的字母表(论坛/博客)

php - 简单的照片分享网站 : How to structure the MySQL database for multiple users and their photos

php - 从 MySQL 数据库错误中选择最新条目

php - 在 Laravel 4 中通过迁移脚本创建 MySQL View

php - 按最近 10 分钟文章浏览量从高到低排序

laravel - 入口 API {"error_message":"You must support\u0027gzip\u0027 to use F13"}

php - Laravel php artisan 配置 :cache and AWS S3 access error