Laravel Blade passing variable with string through @include causes error
Solution 1
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 :("])
Solution 2
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 inlayouts.article
Solution 3
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'])
joeyfb
Web Project Manger at Envivent. Developer with an expertise in frontend web development and build systems (mainly GulpJS). Currently working towards a master's in CS at Portland State University.
Updated on October 30, 2020Comments
-
joeyfb over 3 years
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!
-
Roland almost 6 yearsThe limitation seems to be lifted with Laravel
5.6
: cloud.mxchange.org/s/149vSQYmDeENiuY/… -
Alauddin Ahmed over 4 yearsDon't know why people are down-voting it!
-
Matteus Barbosa over 4 yearsI can not see reason either
-
joeyfb over 3 yearsI 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
-
joeyfb over 3 yearsThe 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
-
Tomk07 about 3 yearsHi, 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