Same section name in blade inherited templates - laravel

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.

Related

Including laravel blade template with button click

I can not find a solution how to include blade template using button click.
For example lets say I have a main view:
<div id="HEREGOESMYTEMPLATES">
</div>
<div class="row">
<button onclick="addTemplate()">ADD TEMPLATE</button>
</div>
Then script, which should include template
<script type="app/js">
function addTemplate()
{
//Get template and include into div with id HEREGOESMYTEMPLATE
}
</script>
And then the template itself (lets say partial.blade.php):
<div>Include me to main blade file</div>
Is there any possible way by including it without creating every tag in JS and then appending to the div?
Thank you.
UPDATE.
As blade template is not supposed to do such work as it is rendered in backend, I used VueJS to this, which I believe is the best solution in this situation.

odoo xpath add existing element inside element

I am trying to add existing division of Odoo product template in a new division. for that, I can do the "replace" of the whole division. and add my custom division.
for example, here is default Odoo template section with the div.
<section t-attf-class="container mt8 oe_website_sale" id="product_detail">
<div class="row" id="odoo_default_row">
</div>
</section>
and I wanted to add odoo_default_row div in my custom div element. like
<section t-attf-class="container mt8 oe_website_sale" id="product_detail">
<div id="my_custom_div">
<div class="row" id="odoo_default_row">
</div>
</div>
</section>
what is the best way to add existing division in the custom division despite using xpath replace?
We can replace whole div element using "replace" attribute. With these, we can freely design custom div element.
Try with following code:
<div id="odoo_default_row" position="replace">
<!-- Design your div element as per your requirement-->
</div>
NOTE:
As per my understanding with your situation is that "inside" attribute will not work.

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

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