I just started working with Laravel / Blade a few weeks ago and was wondering how Blade #include works.
I have a top level index view that then includes some other views. These other views require specific variables, which I know you can pass in through #include.
I also have a controller which creates the top level view. The controller is where I pass in the variables. And it seems that once they've been passed in to the top view, I don't need to pass them in to the sub views.
For a visual
controller
View::make('index', array('abc' => $abc))
index.blade.php
#include('sub.piece') - Do I still need to pass in the array with abc here?
sub/piece.blade.php
{{ abc }}
Does #include work like other includes where it's essentially a copy paste?
You do not have to pass in data that is already available to the parent view.
Passing data in #include is useful for when the variable name differs for the two views.
Related
I have this file sidebar.blade.php inside views/content, I want to include this (sidebar.blade.php) file to my views home.blade.php.
Did you look at the docs?
Blade's #include directive, allows you to easily include a Blade view
from within an existing view. All variables that are available to the
parent view will be made available to the included view:
#include('shared.errors')
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'])
When I include a blade template that extends a base blade, any variables within the section of the included blade show only the variables of the first iteration.
Reading around it seems the render order here is important, and views are rendered before variables, or vice versa.
Note
I have read this SO question/answer: Laravel Blade #yield variable scope
The below snippet is greatly reduced in complexity, so the example could be restructured to exclude sections/extends. However my real case can't be
Example
// index.blade.php
//
#foreach($jobs as $job)
{{ $job->id }} // <-- Correct output, 1,2,3,..N
#include('job-detail', ['id' => $job->id])
#endforeach
Then in the job detail blade
// job-detail.blade.php
//
#extends('job-base')
A: {{ $id }} // <-- Correct output, 1,2,3,..N
#section('content')
B: {{ $id }} // <-- Incorrect output, 1,1,1,..1 (or whatever the first index is)
#endsection // have also tried #stop
Then in the job base blade
// job-base.blade.php
//
#yield('content') // Have also tried #section('content') + #show
After wading through the source code, namely BladeCompiler and View/Factory I noticed the following snippet:
protected function compileOverwrite($expression)
{
return '<?php $__env->stopSection(true); ?>';
}
In the background Laravel appears to store rendered content (by including the file, and extract current variables in a ob_style fashion) in a keyed array, with the view name being the key.
When stopSection is not passed a boolean true, it creates a new key, and the view gets the data from the original key.
Long story short, it's now undocumented (for 5.1+) but can be found in the docs for 5.0:
https://laravel.com/docs/5.0/templates
However it doesn't really explain the "why". This page seems to explain it a little better:
http://laravel-recipes.com/recipes/244/stopping-injecting-content-into-a-section-and-overwriting
I need to pass a variable to an included Blade file. I have attempted this two-ways; however, neither have been successful.
Pass a variable, title, to the included file:
#section('left')
#include('modal', ['title' => 'Hello'])
#stop
Use #yield and set the section:
#section('left')
#include('modal')
#section('title')
Hello
#stop
#stop
I am using Laravel 4.2. I am unaware if what I am trying to do is possible, but I imagine it is.
According to the documentation, the include-way should be the way to do it:
Including Sub-Views
#include('view.name')
You may also pass an array of data to the included view:
#include('view.name', array('some'=>'data'))
My hunch is that $title is conflicting with another variable in your nested templates. Just for troubleshooting, try temporarily calling it something else.
pass an array of data to the included view
#include('view.name', array('some'=>'data'))
then use this on view/name folder
{{ $some }}
I was trying to do include with Laravel blade, but the problem is it can't pass the variable. Here's my example code:
file_include.blade.php
<?php
$myvar = "some text";
main.blade.php
#include('file_include')
{{$myvar}}
When I run the file, it return the error "Undefined variable: myvar". So, how can I pass the variable from the include file to the main file?
Thank you.
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
Unfortunately Laravel Blade engine doesn't support what you expected!.But a little traditional way you can achieve this!
SOLUTION 1 - without Laravel 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 with Laravel Blade Engine
routes.php
$data = array(
'data1' => "one",
'data2' => "two",
);
View::share('data', $data);
Access $data array from Any View like this
{{ $data['data1'] }}
Output
one
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.
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 use View::share($variableName, $variableValue) it will be available to all views as expected.
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?
I suggest you do a classic PHP require if you really want to change your variable (then it's automatically by reference)
I've just started looking into Twig and I'm wondering how I would accomplish the following.
I have a variable $logged_in that I need to have access to in every single page on my site, I was hoping that rather than passing this to the twig renderer every single time in the data array, there would be a way for me to declare this somewhere, and for every template to have access to it.
Do I need to build an extension to accomplish this / or is it even possible? I have looked through every page of the documentation but I'm having trouble having tried to extend the base template as described here...
Twig Documentation | Recipes | Making the Templates aware of the Context Dead link
Is this the right approach?
Thanks
Just read about the new features in the 1.0RC release which should help.
Taken from the blogpost:
Globals:
PHP
// a global can be a constant
$twig->addGlobal('pi', 3.14);
// or any other valid PHP expression, like an object
$twig->addGlobal('request', new Request());
Template
{{ pi }}
{{ request.params('name') }}