Can blade yield to a section in the same file? - laravel-4

I have file master.blade.php, which contains #yield('mainsection'). Then I have another file, with:
#extends('layouts.master')
#section('mainection')
test1
#yield('othersection')
#stop
#section('othersection')
test2
#stop
I can see test1, but not test2 - from which I conclude that blade does not allow you to yield to a section defined in the same file. Is there any way to work around that? Or will I have to add a third file between these two, to contain the mainsection and yield to othersection?

it can be shown, but #section must be written before #yield
#extends('layouts.master')
#section('othersection')
test2
#stop
#section('mainection')
test1
#yield('othersection')
#stop

Related

Laravel Blade: What is nesting sections doing here?

I don't know what does putting a section inside another section do. In a Laravel project I'm reading, a Blade file's code is like this:
login.blade.php :
#extends('layout') #section('content')
#section('title', 'Log in')
Lots of content.
#endsection
In above, what's the point of having title section inside content section? How will it be different if title section is placed outside:
#extends('layout')
#section('title', 'Log in')
#section('content')
Lots of content.
#endsection
I tested, and both are producing same output (HTML source code).
layout.blade.php
<head><title>#yield('title')</title></head>
<body>#yield('content')</body>
Is there any case of layout.blade.php in which different outputs will be produced?
The section directive simply copies whatever you have within #section and #endsection to the name #yield place holder on the extended template.
This means it does not consider where its placed, but for clarity and readability, the second example is the right structure.
For example, this shows that the order of the directive doesn't matter
In welcome.blade.php
#extends('default')
#section('a')
This is a
#section('c', 'This is c')
#section('b')
This is b
#section('d', 'This is d')
#endsection
#endsection
In default.blade.php
#yield('c')
#yield('a')
#yield('b')
#yield('d')
The output
This is c This is a This is b This is d
The best practice is to make your code more readable by opening and closing each block:
#extends('default')
#section('a')
This is a
#endsection
#section('c', 'This is c')
#section('b')
This is b
#endsection
#section('d', 'This is d')
It's better to put them on different rows for readability sake.
First section just puts "Log in" wherever the section "title" is:
#section('title', 'Log in')
Second section has the beginning and the end and is selfdescriptive.

how to pass dynamic value in #include as blade name in laravel5.4

This is my code in laravel blade
#foreach($modules as $module) // {{ mod.$module['module_name'] }}
giving me right value
#section('content')
#include("mod.$module['module_name']") // mod is my folder name and
$module['module_name'] value is
coming from database
#endsection
#endforeach
{{ mod.$module['module_name'] }} is giving me correct value but when i pass it to #include it gives me error. Please help me !!!
I think it has to do with the way the blade template translates the #include directive into compiled PHP. Including the snippet from the compiled PHP file would help, but try this:
#include( 'mod.' . $module['module_name'] )
Looking at some of my past code, you can definitely use a variable in an include. In my case I simply built the desired filename in the controller and passed it along to the view, so my include statement was simply:
#include( $filename )

jekyll filename for current file

The title says it all. Is there a way to access the current filename in Jekyll? Before you jump the gun and try to mark it as duplicate, keep in mind that I'm asking for the current filename, not for the generated filepath which can be access by page.url.
As an example, lets say that there is a file1.html which include file2.html. I want to be be able to get file1.html if I'm inside file1.html and file2.html vice-versa. page.path only returns the filename for the generated file as in I will get file1.html even from within file2.html.
A concrete example:
file2.html:
<div>
{{ page.name }}
</div>
file1.html:
<div>
{% include file2.html %}
</div>
index.html:
{% include file1.html %}
rendered:
<div>
<div>
index.html
</div>
</div>
Instead of index.html, I want it to return file2.html
Thanks
Edit : An include is just a fragment that is inserted into a page. Once every include are made, the page is then processed as a single block of code.
There is no way to get any info from liquid about an include name.b

Laravel/Blade - Extend same template multiple times on same page

So there must be a simple way around this... On my site there are multiple modals, depending on the page. I've created a modal template that these can all extend. However, the last modal I include on the page ends up 'taking over' the rest of them, and so all my modals end up with the same sections from that last include. How can I make it so that each extension is unique to the file from which it extends?
Example of what's happening:
//template.blade.php
<htmls and stuff>
#yield('section_1')
#yield('section_2')
</htmls and stuff>
//Modal 1
#extends('template')
#section('section_1')
Some words
#stop
#section('section_2')
More words
#stop
//Modal 2
#extends('template')
#section('section_1')
Rabbit
#stop
#section('section_2')
Stew
#stop
Instead of two unique modals being loaded, I end up with two modals full of Rabbit Stew.
Try using the #overwrite command instead of #endsection
Example:
#section('stuff')
Stuff goes here...
#overwrite
Source: https://github.com/laravel/framework/issues/1058#issuecomment-17194530
I personally would use includes in this instance, unless you've got markup in your sections. If it's just text you could do something like this:
//template.blade.php
<htmls and stuff>
{{ $section1 }}
{{ $section2 }}
</htmls and stuff>
//Modal 1
#include('template', ['section1' => 'Some words', 'section2' => 'More words'])
//Modal 2
#include('template', ['section1' => 'Rabbit', 'section2' => 'Stew'])
I had the same problem. I really wanted to use Blade templates too, but ended up using php includes, even with basic html markup.
//Modal 1
#include('layout.template', array(
'section1' =>
'<h1>Modal 1</h1><p><b>Some</b> words</p>',
'section2' =>
'<p>Some <u>words</u></p>'
))
//Modal 2
#include('layout.template', array(
'section1' =>
'<h1>Modal 2</h1><p><b>Some</b> words</p>',
'section2' =>
'<p>Some <u>words</u></p>
'
))
The markup all works just fine, including links. Where I ran into trouble was when I wanted to use includes inside the include arrays, which I understand is not possible. That is why I wanted to use Blade Templates.

Blade template not executing #else clause

I have a Blade template that loads a list of users and displays their various details.
If a user has no mobile number I want to display the message "No Mobile Number" (I've tried single and double quotes), this never gets displayed:
#if ($person->Mobile >= "")
{{ $person->Mobile }}
#else
'No Mobile Number'
#endif
I tried substituting the "No Mobile" message with {{ $person->EMail }} (which I'm displaying elsewhere, so I know everyone has an email address), but still go nothing, as far as I can tell the logic isn't going into the #else block.
Any ideas?
This should work
#if (!empty($person->Mobile))
{{{ $person->Mobile }}}
#else
'No Mobile Number'
#endif
I use this approach:
#if( isset($error_message) )
#section('content')
#parent
#stop
#section('error_message')
<strong>Holly molly! </strong><em>{{ $error_message }} :(</em>
#stop
#endif
I have a section content and inside content have another section error_message so if error_message variable is set, show that content section and inside print my error_message.
PD: I haven't my original code to hand... Im currently using Twig as primary template engine in Laravel4. Twig beats Blade in simplycity and is very usefull

Resources