Laravel刀片模板-将php构建的函数或Laravel助手放入yield中



您在blade yield中使用过PHP内置函数吗?例如,我们可以这样做吗:

// master layouts
@yield(ucwords('title'))
// view
@section('title', $title)

注:$title来自控制器

我已经尝试过第一个例子,但它不起作用。在我看来,它不会输出$title。现在我正在使用这个在我的所有观点

// master layouts
@yield('title')
// view 1
@section('title', ucwords($title))
// view 2
@section('title', ucwords($title))
// view 3
@section('title', ucwords($title))

但我认为在第二个例子中,我并没有DRY我的代码,因为我总是在每个视图上重复ucwords()。我们可以在主布局上使用它吗?

谢谢你们,干得好!

您可以生成自己的blade指令,例如,如果您想生成@ucfirst((,则在AppServiceProvider中执行类似操作。

Blade::directive('ucfirst', function ($expression) {
return ucfirst($expression);

});

将其转换为boot()

或者在每个section()上,你可以扩展主布局@section('title', ucwords($title)),或者像我上面提到的那样制作助手

如上所述,您可以使用yield()

@yield('title',ucwords(strtolower('Your title')))

最新更新