103

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

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

and I get the following error...

FatalErrorException syntax ... error, unexpected ','

I've narrowed down that the error is solely from the "(" in the "mainContent" variable string, and when I remove the "(" the error disappears and everything runs fine. I can't find anything in documentation on this or any similar errors listed online.

Does anyone know if this is expected behavior or if this is a bug that should be reported?

Thanks so much for your time!

joeyfb
  • 3,044
  • 3
  • 19
  • 25

3 Answers3

152

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 :("])
u01jmg3
  • 712
  • 1
  • 11
  • 31
joeyfb
  • 3,044
  • 3
  • 19
  • 25
  • 4
    The limitation seems to be lifted with Laravel `5.6`: https://cloud.mxchange.org/s/149vSQYmDeENiuY/download?path=%2FLaravel%205.6&files=blade-multiline-example.txt – Roland Jul 05 '18 at 10:46
  • The issue claims as of 5.6 it still doesn't work but should soon, no update on that issue though if it was implemented https://github.com/laravel/framework/issues/8502 – joeyfb Aug 22 '20 at 00:38
38

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'])
Community
  • 1
  • 1
Matteus Barbosa
  • 2,409
  • 20
  • 21
  • 1
    Don't know why people are down-voting it! – Alauddin Ahmed Oct 02 '19 at 09:47
  • I can not see reason either – Matteus Barbosa Oct 20 '19 at 13:54
  • I downvoted this answer because it doesn't address the actual question. The premise of the question is that include breaks with a specific variable inclusion and format, see my github issue for an understanding of it: https://github.com/laravel/framework/issues/8502 – joeyfb Aug 22 '20 at 00:32
  • Hi, is it possible to attach some kind of @only keyword to this, to restrict access to the included context? [I'm asking in relation to the 'only' keyword in twig](https://stackoverflow.com/questions/66819354/twigs-include-with-in-blade-or-regular-php) – Tomk07 Mar 30 '21 at 10:40
30

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

varun sharma
  • 1,017
  • 15
  • 19
  • 1
    Please don't do it like this. You don't need php tags in blade files. Not only don't you need $data at all you should do all php processing in the controller. – Adam Mudd Oct 10 '22 at 14:07
  • 1
    you can also use this with `@php` `@endphp` tag from `` as to maintain consistency of blade – Anthony Kal Oct 21 '22 at 06:39