首页 > 解决方案 > 如何在 laravel 的 @include() 中使用 {{ }} 发送参数

问题描述

我想在 Laravel 的 @include() 中将路由作为参数发送。我正在尝试使用 {{ }} 发送参数以将其作为 href 值,但我收到错误消息。有没有办法把'{{}}'放在字符串中?

这就是导致错误的原因:

@include('includes.content.single-article', ['href' => '{{ route('article') }}'])

在 single-article.blade.php 中应该是这样的:

<a class="card-link btn btn-warning" href="{{ $href }}">more<span class="icon icon-plus"></span></a>

它应该像这样工作:

    <a class="card-link btn btn-warning" href="{{ route('article') }}">more<span class="icon icon-plus"></span></a>

我得到的错误:

ParseError 语法错误,意外的“文章”(T_STRING),期待“]”

标签: phphtmllaravel

解决方案


您不需要发送变量。@include是“复制/粘贴”(相同@extend)。

这样做:

@php($href=route("article"))
@include("includes.content.single-article")

就这样。现在您的刀片includes.content.single-article知道$href.

如果你真的只需要这个刀片的变量

@include("catalog.task.foo", ["href" => route("purchase.provider")])

当您使用刀片指令时,您在上下文中,如果您不需要字符串PHP,请不要使用。"

你可以

@include("catalog.task.foo", ["href" => 45+58])

并且会做数学


推荐阅读