108

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!

6
  • I not sure for this answer give spacebar to "( " Could u try ? @joey Commented Apr 20, 2015 at 7:41
  • This is seems to be a bug.. It can getting work using some kind of escaping method. but by default It should be work as it is! Commented Apr 20, 2015 at 8:28
  • 3
    This bug can be reported to the github.com/laravel/framework/issues Commented Apr 20, 2015 at 8:31
  • Thanks guys, just reported it as a bug on this issue Commented Apr 20, 2015 at 15:40
  • Great find! Could you please answer your question or something so that it is no longer in the php/unanswered list? Thanks! Commented Jul 14, 2015 at 2:47

3 Answers 3

165

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 :("]) 
Sign up to request clarification or add additional context in comments.

2 Comments

The limitation seems to be lifted with Laravel 5.6: cloud.mxchange.org/s/149vSQYmDeENiuY/…
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 github.com/laravel/framework/issues/8502
42

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

4 Comments

Don't know why people are down-voting it!
I can not see reason either
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: github.com/laravel/framework/issues/8502
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
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

2 Comments

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.
you can also use this with @php @endphp tag from <?php ... ?> as to maintain consistency of blade

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.