如何在不转义blade中的大括号的情况下使用@{{ }}?
How to use @{{ }} without escaping the braces in blade?
我有像这样的用户配置文件的路由 example.com/@username
。所以现在我想说这样的话:
<a href="/@{{ $user->name }}">Go to {{ $user->name }} profile</a>
但这实际上转义了 href
属性中的 {{ $user->name }}
(如 in the documentation 所述),因此这实际上会将我重定向到 example.com/{{ $user->name }}
.
当然,我可以使用 @<?= $user->name; ?>
之类的纯 php 方式或任何其他方式。但是我想用laravel的花括号{{ }}
.
可能吗?
试试这个
<a href="{{ url('/@'.$user->name) }}">Go to {{ $user->name }} profile</a>
解决此问题的唯一方法是使用上述答案。它将使用 blade 的 {{ }}
.
不过,我推荐另一种(半)方法。您可以像这样为 User
模型(在我的例子中)创建一个方法:
class User {
public function handle()
{
return '@' . $this->username;
}
}
那么你可以使用:
<a href="/{{ $user->handle }}">Go to {{ $user->name }} profile</a>
正如我所说,这不是一个通用的解决方案。但它适用于我的情况。如果你想要更通用的解决方案,请参考上面的答案。
我有像这样的用户配置文件的路由 example.com/@username
。所以现在我想说这样的话:
<a href="/@{{ $user->name }}">Go to {{ $user->name }} profile</a>
但这实际上转义了 href
属性中的 {{ $user->name }}
(如 in the documentation 所述),因此这实际上会将我重定向到 example.com/{{ $user->name }}
.
当然,我可以使用 @<?= $user->name; ?>
之类的纯 php 方式或任何其他方式。但是我想用laravel的花括号{{ }}
.
可能吗?
试试这个
<a href="{{ url('/@'.$user->name) }}">Go to {{ $user->name }} profile</a>
解决此问题的唯一方法是使用上述答案。它将使用 blade 的 {{ }}
.
不过,我推荐另一种(半)方法。您可以像这样为 User
模型(在我的例子中)创建一个方法:
class User {
public function handle()
{
return '@' . $this->username;
}
}
那么你可以使用:
<a href="/{{ $user->handle }}">Go to {{ $user->name }} profile</a>
正如我所说,这不是一个通用的解决方案。但它适用于我的情况。如果你想要更通用的解决方案,请参考上面的答案。