Laravel - Difference between #yield and #section? - laravel

From the Laravel docs, you can include 'sections' inside layouts using two methods:
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
Since #yield can also pass in some default content using #yield('section', 'Default Content'), is #yield just a shorthand for a #section that does not use #parent?
#section
<!-- Nothing here -->
#show
What other differences are there?

Short Answer: Always use #yield unless you want to do something more complicated then providing a default string.
Long Answer:
Both #yield and #section .. #show are used to be optionally overwritten whenever you extend the blade template. Everything you can do with #yield can also be done with #section .. #show but not the other way around. Here is what they do:
#yield('main')
Can be replaced by #section('main') .. #endsection
Can be provided with a default string but no HTML! The default string will be shown in the sub-blade-template when no #section('main') .. #endsection is provided.
#section('main') .. #show
Can be replaced by #section('main') .. #endsection
Can be provided with a default HTML code. The default HTML code will be shown in the sub-blade-template when no #section('main') is provided.
Can be replaced by #section('main')#parent .. #endsection and additionally shows the default HTML code.
Here some examples:test.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<h1>This is a test</h1>
#yield('mainA')
#yield('mainB', 'This is the alternative 1')
#yield('mainC', '<p>This is the alternative 2</p>')
#yield('mainD', 'This is the alternative 3')
#section('testA')
#show
#section('testB')
This is the alternative 4
#show
#section('testC')
<p>This is the alternative 5</p>
#show
#section('testD')
<p>This is the alternative 6</p>
#show
</body>
</html>
here is another file called testA.blade.php which extends the other bladed file:
#extends('test')
#section('mainD')
<div>
<p>First replacement!</p>
<hr>
</div>
#endsection
#section('testC')
<div>
<p>Second replacement!</p>
<hr>
</div>
#endsection
#section('testD')
#parent
<div>
<p>Additional content</p>
<hr>
</div>
#endsection
And that is the outcome:

This line clears out the confusion: "Note that views which extend a Blade layout simply override sections from the layout. Content of the layout can be included in a child view using the #parent directive in a section".
So, if you already have a #section defined in the master layout, it will be overriden unless you specify #parent inside the child layout's #section.
But for #yield, it always gets the section from the child layout. That means it always overrides the #yield part, even if it has a default defined as #yield('section', 'Default Content') .
I hope that clears your confusion. Let me know if you have more questions. Thanks

The shortest answer:
Use #yield in master if you want to overwrite child data on master layout completely.
Use #section in master if you want to use master and child data together on child with #parent (Or overwrite child data on master layout like #yield)

Basically yield('content') is a marker. For example, in the tag if you put a yield('content'), your saying this section has the name of content and by the way, you can name inside of the parenthesis anything you want. it doesn't have to be content. it can be yield('inside'). or anything you want.
And then in the child page where you want to import html from your layout page, you just say section('name of the section').
for example, if you have marked your header in your layout page as yield('my_head_band') <-- or anything else you want, then in your child page you just say #section('my_head_band').
This would import the header from the layout page into your child page. vice versa with your body section which in this case was named as content.
Hope this helps.

Just to add on something small, #yield basically defines a section to be injected by overwriting the data and it also works if our view #extends the parent view.
Now when we overwrite, we replace an implementation completely with a new implementation, like a company can decide to change/overwrite its entire technology if they realize something went wrong.
It should not be confused with override

#yield having default value in second parameter and section doesn't have in master if you want to overwrite child data on master layout completely then you will use #yield.
Use #section in master if you want to use master and child data together on child with #parent (Or overwrite child data on master layout like #yield)
Example
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
#extends('layouts.master')
#section('title', 'Page Title')
#section('sidebar')
##parent
<p>This is appended to the master sidebar.</p>
#stop
#section('content')
<p>This is my body content.</p>
#stop

Related

Laravel Blade - yield inside section

I'm trying to yield a section inside another section. But this does not work as expected, I see blank output.
#section('3show')
This is a text string
#stop
#section('page-content')
<div id="content">
<article>
#yield('3show')
</article>
</div>
<!--#content-->
#stop
Any ideas to yield section inside another section ?
Ok, this is what I tried and I can confirm that this works, at least for Laravel 5+ (I have L5.2). This is how I suggest you to use your blade templates.
Lets start saying that to yield a section into another section you have to define your included section before container section definition. So, with that clear, I solved this situation like this:
I got a main blade (main.blade.php) template which has something like:
<section class="content">
<!-- Your Page Content Here -->
#yield('main-content')
</section><!-- /.content -->
I got a second blade (common.blade.php) template which has that common stuff you may want to show across many pages and where main-content section is defined. This one looks like:
#section('main-content')
<div class="container">
#yield('extra-content')
</div>
#endsection
Finally I got a 3rd template (test.blade.php) which extend the main template and include the common stuff I want to show, but be careful because the order matters. This one looks like:
#extends('main')
#section('extra-content')
<div>
<span> This is a test! </span>
</div>
#endsection
#include('common')
In your controller or your route (wherever you return your view), you should return the 3rd template.
In my projects i create some partials in order to have cleaner code and i give them as an example a name : 3show.blade.php. In order to use them in a section i just include them.
I think this will do what you want.
#section('content')
#include('3show.blade.php')
#endsection
I had the same issue.
You can't use the #extends option in this case, you need to use #include .
So lets say you have:
the root layout (layouts/app.blade.php)
an extra layout (layouts/extra.blade.php)
the actual view that you are calling (someview.blade.php)
The solution is to use add/inherit the root layout (1) to the top line of your view (3):
#extends('layouts.app')
Next, add/inherit the extra layout (2) to the second line of your view, BUT use #include not #extends:
#include('layouts.extra')
...and remember to wrap the content of your extra layout in an #section name, for example #section('extra')
Finally you can call your extra layout content from your view:
<p>Look below, you will see my extra content...</p>
#yield('extra')
So in summary, your someview.blade.php view would look like:
#extends('layouts.app')
#include('layouts.extra')
#section('content')
<p>Look below, you will see my extra content...</p>
#yield('extra')
#endsection
solution 1:
you can use #show instead of #stop at the end of section
then laravel will render your #yield parts ...
like this :
#section('page-content')
<div id="content">
<article>
#yield('3show')
</article>
</div>
#show # not #stop
#section('3show')
This is a text string
#stop
this way render you view and show result
so if you cll your section for twice then result will be shoed twice
solution 2:
insert call section before yiel it
like this :
**first call section :**
#section('3show')
This is a text string
#stop
**then define your section : 3show**
#section('page-content')
<div id="content">
<article>
#yield('3show')
</article>
</div>
#stop **not #show**
assume you have two files:
-article_base.blade.php -> the default data in every article.
-article_index.blade.php -> the customized file.
article_base.blade.php
#extends('layout')
#section('page-content')
<div id="content">
<article>
....
#yield('3show')
....
</article>
</div>
<!--#content-->
#stop
article_index.blade.php
#extends('article_base')
#section('3show')
This is a text string
#endsection
I hope this works
You have to both #include and #yield the child template in your main template to be able to add the child template at a specific place inside the main template (not just before or after the main template - this is done by adding #parent in the child template - but in between):
main.blade.php
#include('child')
<div>
...
#yield('child')
</div>
child.blade.php
#section('child')
<div>
...
</div>
#endsection

Laravel blade: Overwrite section using show

I have default blade , it will have 3 section and one yield content as like below
<div id="wrapper">
#show
#section('sidemenu')
#include('partials.admin_sidemenu')
<div id="page-wrapper" class="gray-bg">
#show
#section('navheader')
#include('partials.admin_navheader')
#show
#yield('content')
#show
#section('footer')
#include('partials.admin_footer')
</div>
</div>
In my login blade i don't want to show header,menu and footer so i have used below code to overwrite the header menu and footer
#extends('layout.default')
#section('sidemenu')
#stop
#section('navheader')
#stop
#section('content')
Login Form
#stop
#section('footer')
#stop
It's not over writing the footer content ... it was a issue?
How can i handle this?
Thanks Advance
You forgot about putting: #show at the end of section declaration:
#section('footer')
#include('partials.admin_footer')
#show

Same section name in blade inherited templates

Let's say i have three blade templates: A, B and C. Template A is the global layout, template B is some specific section's layout and template C is the view template.
Templates A and B expect a section called content to be assigned. That section is defined in the view templates (C).
Here's a simplified version of templates A and B:
Template A:
<html>
<body>
#yield('content')
</body>
</html>
Template B:
#extends('template_a')
#section('content')
<div class="sidebar">
...
</div>
<div class="content">
#yield('content')
</div>
#endsection('content')
As you can see, both templates output a content section. My problem is that in views that extend B it's content is simply ignored. The content section defined in the view is output on the #yield('content') present at template A.
I would like to know if it is possible to propagate the content section up in the view hierarchy, i.e., replacing the the content placeholder in template B with the value defined in template C and replace the result in content placeholder in template A.
Sorry if i made this sound too confusing. I hope you get my idea.
Thanks in advance.
I found an answer to your problem here: https://laracasts.com/discuss/channels/laravel/trouble-with-blade-section-inheritance?page=1
The trick is to use #overwrite instead of #endsection in the blades that contain a #yield inside a #section with the same name.
#extends('app')
#section('content')
{{--Some common code--}}
#yield('content')
#overwrite
Using #endsection causes yielded views to override the intermediate view, but using #overwrite forces inclusion along the way.
The confusing thing, in my opinion, is that in conventional programming the directive to make use of the superclass method goes in the subclass (calling the superclass method from the subclass) whereas in Laravel blade inheritance it must be placed in the superclass itself (the superclass declares within itself that it cannot be ignored when overridden).
Just rename the #yield('content') in Template B. A #endsection (Laravel5) is enough to end those sections.
Template A:
<html>
<body>
#yield('body')
</body>
</html>
Template B:
#section('body')
<div class="sidebar">
...
</div>
<div class="content">
#yield('content')
</div>
#endsection
Template C:
#section('content')
<!--yourContent-->
#endsection
This way you can easily change your Template B with any other given Template to modify the body (i.e. Template D):
#section('body')
<!-- Some different Body Style -->
#yield('content')
#endsection
as well as your content (i.e. Template E):
#section('content')
<!--some different Content-->
#endsection
Edit:
Probably the reason for Template B not showing any data from Template C is an infinite Loop caused by Template B: Every time you call the section('content') you also yield('content') and inserting Template B into itself.

Does source binding require a single root element even for non-arrays?

The documentation on source binding has an aside which states:
Important: A single root element should be used in the template when
binding to an array. Having two first level DOM elements will result
in an erratic behavior.
However, I'm finding that this is the case even for non arrays.
I have the following HTML, which sets up two div's populated by two templates. The only difference is that the working template wraps that databound spans in a div.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://cdn.kendostatic.com/2013.3.1119/js/kendo.all.min.js"></script>
<title>JS Bin</title>
<script id="broken-template" type="text/x-kendo-template">
Foo: <span data-bind="text: foo"></span><br/>
Foo Again: <span data-bind="text: foo"></span>
</script>
<script id="working-template" type="text/x-kendo-template">
<div>
Foo: <span data-bind="text: foo"></span><br/>
Foo Again: <span data-bind="text: foo"></span>
</div>
</script>
</head>
<body>
<div id="broken-div" data-template="broken-template" data-bind="source: this">
</div>
<br/>
<br/>
<div id="working-div" data-template="working-template" data-bind="source: this">
</div>
</body>
</html>
And the JavaScript simply creates a view model with a single property and binds it to both divs:
var viewModel = kendo.observable({foo: "bar"});
kendo.bind($("#broken-div"), viewModel);
kendo.bind($("#working-div"), viewModel);
In both cases, only the first root element and it's children are being bound properly. This suggests that every time I databind to template with more than one element I need to make sure it is wrapped in a single root.
Is this behavior documented somewhere? Is there a bug in Kendo or in my sample code? An explanation for why Kendo requires a single root would be great to hear as well.
(Sample code as a jsfiddle)
It's not documented except in the one place you mentioned. Such is the state of Kendo UI documentation - it's less than complete. I've been using Kendo UI for three years and as far as I can tell you, this is its default behavior and not a bug. Unfortunately, it's one of the many quirks you simply learn (stumble upon) from experience.

Sinatra indent partial erb at statement

Is it a way to make partials in sinatra be indented at the level where I call it?
Example
<body>
<div>
<%= partial :"mypartial" %>
</div>
</body>
Results in
<body>
<div>
<div id="i am defined in mypartial">
//etc
</div>
</div>
</body>
When I want
<body>
<div>
<div id="i am defined in mypartial">
//etc
</div>
</div>
</body>
This is possible if I indent the partial currectly, but that makes it hard to work with. I want the partial to be starting indentation all the way to the left(in the source file).
Maybe there is some kind of post processor that can format that html for me?
This is for a project that is not going public, but it's important that internal users can read the generated html easily. Including correct indentation.

Resources