Laravel Blade 通过@include 传递带字符串的变量导致错误

Laravel Blade passing variable with string through @include causes error

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

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

我收到以下错误...

FatalErrorException syntax ... error, unexpected ','

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

有谁知道这是预期的行为还是应该报告的错误?

非常感谢您的宝贵时间!

这不是错误,而是正则表达式对 blade 语法的限制。解决方案来自 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

5.8v 中,包含的视图按照文档从父级继承所有变量:

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'])