Laravel 9内联刀片模板渲染;渲染常数而不是变量

发布于 2025-02-06 13:34:29 字数 726 浏览 2 评论 0原文

Laravel 9中有一项新功能,可以允许内联刀片模板施用。链接: https://laravel.com/docs/9。 x/blade#渲染inline-inline-blade-templates

假设我想从数据库中渲染的数据库中存储一个值(作为电子邮件的正义)。

例如 < p>亲爱的{{$ first_name}}} {{$ last_name}}</p>

我将使用此;

Blade :: Render($ body,['first_name'=> $ first_name,'last_name'=> $ last_name]));

我可以输出类似的内容;

亲爱的John Doe

在发送电子邮件的正文中。

但是,如果我想使用该怎么办 < p>亲爱的{{first_name}} {{last_name}}</p>

在我的电子邮件模板中存储在数据库中(没有美元符号,使其成为常数而不是可变的)。如何使用Blade :: Render()渲染这个常数?

There is a new feature in Laravel 9 that allows inline blade template rending. Link: https://laravel.com/docs/9.x/blade#rendering-inline-blade-templates

Let’s say I have a value stored in the database (as the body of the email) I want to render from database as email.

E.g
<p> Dear {{$first_name}} {{$last_name}} </p>

I would use this;

Blade::render($body, [‘first_name’ => $first_name, ‘last_name’ => $last_name]);

With this I’m able to output something like;

Dear John Doe

In the body of the email sent.

However, what if I want to use
<p> Dear {{first_name}} {{last_name}} </p>

in my email template stored in the database (without the dollar sign, making it a constant rather than a variable). How can I render this constant using the Blade::render()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2025-02-13 13:34:29

我认为,在将输出发送到刀片之前,您必须操纵模板。

拉维尔(Laravel)有许多助手可以做这样的事情。

use Illuminate\Support\Str;
 
$string = '{{first_name}} {{last_name}}';
 
$replaced = Str::replace('{{', '{{

上面的代码将完成这项工作。

, $string); // in $replaced you'll have '{{$first_name}} {{$last_name}}'

上面的代码将完成这项工作。

I think you will have to manipulate the templates, before sending output to blade.

Laravel has many helpers to do things like that.

use Illuminate\Support\Str;
 
$string = '{{first_name}} {{last_name}}';
 
$replaced = Str::replace('{{', '{{

Above code will do the job.

, $string); // in $replaced you'll have '{{$first_name}} {{$last_name}}'

Above code will do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文