Laravel Blade 使用 string through@include 传递变量会导致错误

在 Laravel 5.0.27中,我包含了一个带有变量和以下代码的视图:

@include('layouts.article', [
'mainTitle' => "404, page not found",
'mainContent' => "sorry, but the requested page does not exist :("
])

我得到了下面的错误..。

语法... 错误,意外’,’

我已经缩小了范围,错误完全来自“ mainContent”变量字符串中的“(”,当我删除“(”时,错误消失,一切运行正常。我在文档中找不到任何关于这个或任何在线列出的类似错误的内容。

是否有人知道这是否是预期的行为,或者这是否是一个应该报告的错误?

非常感谢您抽出时间!

142715 次浏览

It's not a bug but a limitation of blade syntax due to regex. Solution came from github:

The problem is using multi-line. You can only use a single line to [pass variables] in Blade, since syntax is limited [by regular expressions]

Try the code below and you should be good to go:

@include('layouts.article', ['mainTitle' => "404, page not found", 'mainContent' => "sorry, but the requested page does not exist :("])

You can pass a $data array

<?php $data=[
'mainTitle' => "404, page not found",
'mainContent' => "sorry, but the requested page does not exist :("
]  ?>
@include('layouts.article', $data)

use $data['mainTitle'] etc in layouts.article

In 5.8v, included views inherit all variables from the parent as per in documentation:

Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:

@include('view.name', ['some' => 'data'])