php - Laravel 4 Blade @include variable
Get the solution ↓↓↓Solution:
Why would you pass it from the include to the calling template? If you need it in the calling template, create it there, then pass it into the included template like this:
@include('view.name', array('some'=>'data'))
Above code snippet from http://laravel.com/docs/templates
Answer
Solution:
Unfortunately Laravel Blade engine doesn't support what you expected!.But a little traditional way you can achieve this!
SOLUTION 1 - withoutLaravel Blade Engine
Step a:
from
file_include.blade.php
to
file_include.php
Step b:
main.blade.php
<?php
include('app/views/file_include.php')
?>
{{$myvar}}
SOLUTION 2 withLaravel Blade Engine
routes.php
$data = array(
'data1' => "one",
'data2' => "two",
);
View::share('data', $data);
Access $data array fromAny View
like this
{{ $data['data1'] }}
Output
one
Answer
Solution:
Blade is a Template Engine for Laravel. So try passing the value from the controller or you may define it in the routes.php for testing purposes.
@include is used to include sub-views.
Answer
Solution:
I think you must understand the variable scope in Laravel Blade template. Including a template using@include
will inherit all variables from its parent view(the view where it was defined). But I guess you can't use your defined variables in your child view at the parent scope. If you want your variable be available to the parent try useView::share($variableName, $variableValue)
it will be available to all views as expected.
Answer
Solution:
In this scenarion$myvar
would be available only on the local scope of the include call.
Why don't you send the variable directly from the controller?
Answer
Solution:
I suggest you do a classic PHP require if you really want to change your variable (then it's automatically by reference)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.