繁体   English   中英

Laravel - 如何在一条线上将变量传递到刀片的部分?

[英]Laravel - How I Pass Variable Into Blade's Section In One Line?

如何使用单行方法将变量传递给刀片命令“@section”? 我有这个:

@section('title', 'Hello')

现在我想要类似的东西

@section('title', 'Hello {{ $user->name}} foo') // doesn't work
// or
@section('title', 'Hello ' . {{ $user->name}} . ' foo') // doesn't work
// or
@section('title', 'Hello ' . htmlspecialchars($user->name) . ' foo') // this works
// or
@section('title')Hello {{ $user->name }} foo @endsection // this works

最后两个工作正常,但我不想在我的刀片模板中使用普通的 php 并且我不想使用@endsection 作为标题。

我怎么能做到这一点? 甚至有可能还是我应该使用两种解决方案之一 go ?

不要在刀片指令中使用刀片回声,它们已经是 php 代码

删除双花括号并将其全部用双引号括起来

@section('title', "Hello $user->name foo")

这是尽可能短的

也许这就是你要找的

@section('title')
 Hello, {{ $user->name}}
@endsection

这个怎么样

@section('title', 'Hello ' . auth()->user()->name )

您可以像这样删除花括号:

@section('title', 'Hello ' . $user->name . ' foo') // This should work
@section('title', 'whatever your string')

您需要做的就是格式化刀片支持的字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM