简体   繁体   English

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

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

how I can pass a variable into the blade command "@section" with the one line approach?如何使用单行方法将变量传递给刀片命令“@section”? I have this:我有这个:

@section('title', 'Hello')

Now I want something like现在我想要类似的东西

@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

The last two just working fine, but I don't want to use plain php in my blade template and also I don't want to use @endsection for title.最后两个工作正常,但我不想在我的刀片模板中使用普通的 php 并且我不想使用@endsection 作为标题。

How I can do this?我怎么能做到这一点? Is there even a possibility or should I go with one of the two solutions?甚至有可能还是我应该使用两种解决方案之一 go ?

Don't use blade echo in blade directives, they're already php code不要在刀片指令中使用刀片回声,它们已经是 php 代码

Remove the double curly braces and wrap it all in double quotes删除双花括号并将其全部用双引号括起来

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

This is as short as you can get这是尽可能短的

Maybe this is what you're looking for也许这就是你要找的

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

How about this这个怎么样

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

You can remove the curly braces like this:您可以像这样删除花括号:

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

all you need to do is format a string that support by blade.您需要做的就是格式化刀片支持的字符串。

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

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