Laravel Blade passing variable with string through @include causes error

后端 未结 3 1516
有刺的猬
有刺的猬 2020-12-13 12:10

In Laravel 5.0.27 I am including a view with with a variable and the following code:

@include(\'layouts.article\', [
        \'mainTitle\' => \"404, page          


        
相关标签:
3条回答
  • 2020-12-13 12:27

    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

    0 讨论(0)
  • 2020-12-13 12:27

    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'])
    
    0 讨论(0)
  • 2020-12-13 12:42

    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 :("])
    
    0 讨论(0)
提交回复
热议问题