Laravel blade: Overwrite section using show - laravel

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

Related

How to convert a blade file to HTML and save it?

For example, there is a file \resources\views\test.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
{{ $article->content }}
</div>
#endsection
I want to save it as \resources\views\html\test.html. What should I do?
I would try something like this, although I haven't test it my self:
File::put('test.html',
view('resources.views.test')
->with(["article" => $article])
->render()
);

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 - Difference between #yield and #section?

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

How to add javascript in views in Laravel 4 Starter Site

i have a question related to Laravel 4 Starter Site
The following is one of the view page.
#extends('site.layouts.default')
{{-- Web site Title --}}
#section('title')
{{{ Lang::get('site.contact_us') }}} ::
#stop
{{-- Content --}}
#section('content')
{{{ Lang::get('site.contact_us') }}}
#stop
In the site.layouts.default template, Jquery is included. How could i include other javascript like
{{ HTML::script('http://cdn.datatables.net/1.10.0/js/jquery.dataTables.js') }}
into the view file? It should included after the jquery library.
Thanks in advance!
You may use:
#section('scripts')
{{ HTML::script('...') }}
#stop
Anywhere in your view after #extends('...'). Because there is #yield('scripts') in the bottom of the layout, right after the scripts (jQuery and BootStrap).
This is another example:
{{-- Scripts --}}
#section('scripts')
<script type="text/javascript">
$(document).ready(function() {
//...
});
</script>
#stop

Laravel Blade Templates Section Repeated / cache error

I am looking to Bootstrap my site and as such, have put common twitter bootstrap components into blade templates.
sidebar.blade.php
#include('panel1')
#include('panel2')
panelTemplate.blade.php
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
#yield('title')
</div>
</div>
<div class="panel-body">
#yield('body')
</div>
<div class="panel-footer">
#yield('footer')
</div>
</div>
This way, every time I wish to use a panel, then I can use #extends('panelTemplate').
panel1.blade.php
#extends('panelTemplate')
#section('title')
title panel 1
#stop
#section('body')
body panel 1
#stop
#section('footer')
footer panel 1
#stop
panel2.blade.php
#extends('panelTemplate')
#section('title')
title panel 2
#stop
#section('body')
body panel 2
#stop
#section('footer')
footer panel 2
#stop
The problem that I am facing is that instead of showing the contents of panel1.blade.php, then the contents of panel2.blade.php as declared in sidebar.blade.php the contents of panel1.blade.php is being repeated (shown twice).
Is Blade caching the request which is why panel1 is being repeated twice? Is there a way to override this behaviour or am I using the blade templating engine in a way which it was never intended?
You can achieve this by overwriting the sections.. try what you have already, but with these two updated sub-views:
panel1.blade.php
#extends('panelTemplate')
#section('title')
title panel 1
#overwrite
#section('body')
body panel 1
#overwrite
#section('footer')
footer panel 1
#overwrite
panel2.blade.php
#extends('panelTemplate')
#section('title')
title panel 2
#overwrite
#section('body')
body panel 2
#overwrite
#section('footer')
footer panel 2
#overwrite
It's tested and working here, though I'm not sure it's the intended use of #overwrite, so test thoroughly!

Resources