I'm just starting to learn Laravel and want to know how to go about doing the below. I'll give the code then explain.
I have a file includes/head.blade.php. this file contains things you find inside the <head>. So it contains <title>#yield('title')</title> If I now include this file in a page let say pages/about.blade.php like this #include('includes.head'), How then can I modify the <title> nested inside the include using this line #section('title', ' bout Us')
If you include the blade file like #include('includes.head') then you cannot do <title>#yield('title')</title> in head.blade.php . Correct way to do this is passing the value while including the file like :
#include('includes.head',['title'=>'About Us'])
and in head.blade.php you must do like:
<title>
#if(isset($title))
{{ $title }}
#endif
</title>
But if you extends the heade.blade.php then you can do like this :
head.blade.php
<title>#yield('title')</title>
about.blade.php
#extends('includes.head')
#section('title')
{{ "About Us" }}
#endsection
For more info Check this
I think you can use the #include like this, check this DOC.
#include('includes.head', ['title' => 'About Us'])
and the title should be print as,
<title>{{ $title }}</title>
FOR the best practice
Check the laravel blade templating feature,
you can define a master layout, extending that layout you can create new views. As like in the this DOC.
master.blade.php
<html>
<head>
<title>#yield('title')</title>
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
about.blade.php
#extends('master')
#section('title', 'About Us') // this will replace the **title section** in master.blade
//OR
//#section('title')
// About Us
//#endsection
#section('sidebar')
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
Related
Extending layouts and dividing a view file into sections, wherein "x" section goes inside "x" section of the layout when rendering. Like it's done in blade using #extend() and #section() []. I just want to implement this on my own PHP without using any library or framework. This is how blade does it :
Layout File [app.blade.php]
<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>
View File [my-page.blade.php]
#extends('layouts.app')
#section('title', 'Page Title')
#section('sidebar')
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<p>This is my body content.</p>
#endsection
When trying to use #yield and #section it does not work. With only extending the layout, all the layout is rendered, I can't choose with #section and #endsection what will be rendered.
This is for a Laravel project with HomeStead on my local machine
plantilla.blade.php is:
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<div>
#yield('sidebar')
This is the master sidebar.
</div>
<div class="container">
#yield('content')
This is another container
</div>
</body>
</html>
And the contact.blade.php is:
#extends("layouts.plantilla")
#section('content')
#endsection
When opening contact.blade.php, both sections are displayed (sidebar AND content), instead of only content, which is the section I'm actually calling.
This happens also if I just leave the first line (#extends("layouts.plantilla")
without calling any section, it will render all of its content
What could am I doing wrong here?
That's because you are yielding both content and sidebar inside your div already, so the "This is the master sidebar" and "This is another container" will always show, even if you don't use that section.
You need to change your code in main layout like this:
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
#yield('sidebar')
#yield('content')
</body>
</html>
So now, if you only want to display a content of your page, you do it like this:
#extends("layouts.plantilla")
#section('content')
<div class="container">
This is another container
</div>
#endsection
With Blade it seems the yield does not work with included parts. How can I fill a section defined inside an included part in the parent template?
It seems this is a known issue:
https://github.com/laravel/framework/issues/8970
template-body.blade.php
<body>
#yield('body')
<body>
template-html.blade.php
<html>
#include('template-body')
#yield('other')
</html>
foo.blade.php
#extends('template-html')
#section('body')
Hello World! (does not work)
#endsection
#section('other')
Does work
#endsection
what you are doing will give you an error. you can include pages on the template and other sub pages. it just depends on what you are doing and how you doing it. what you should have done is:
//app.layout
<!DOCTYPE html>
<html>
<head>
<title>#yield('title')</title>
<your-css-files>
#yield('styles')
<your-script files>
#yield('scripts')
</head>
<body>
#include('your page')
#yield('content')
</body>
</html>
then on your pages you do:
#extends(app.layout)
#section('content')
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
#include('your page')
#endsection
on the samepage you can call your styles and scripts as
#section('styles')
//css files
#stop
#section('scripts')
// javascript files
#stop
I want to write to a region in my template from the view and also from an included view thing the first view but no matter what I try it doesn't work. I have tried using #parent and #append but nothing has produced the result that I want so far.
In my layout I have the following:
#yield('scripts')
In my view primary I have the following:
#include('admin/rules/partials/_form')
#section('scripts)
// javascript #1 here
#stop
In admin/rules/partials/_form I have the following:
#section('scripts)
// javascript #2 here
#stop
So like I said I have tried using #parent after #section('scripts') and also using #append instead of #stop but nothing I do includes the two script blocks properly. The best I've had so far is javascript #1 being included once and javascript #2 being included twice. How would I do this so that each block of code is appended to the scripts region only once?
I solved it in the end I had to do the following:
#yield('scripts')
In my view primary I have the following:
#include('admin/rules/partials/_form')
#section('scripts')
// javascript #1 here
#stop
In admin/rules/partials/_form I have the following:
#section('scripts')
#parent
// javascript #2 here
#overwrite
This answer worked for me.
EDIT, but now it doesn't.
EDIT, I updated the code below to work. I was yielding the javascript section on the page and the layout.
tests.layout.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>
#yield('title') - test
</title>
#yield('head')
</head>
<body>
#yield('content')
#yield('javascript')
</body>
</html>
tests.page.blade.php
#extends('layouts.default')
#section('title', 'Poses')
#section('content')
<div class="container">
<div class="row">
#include('tests.partials.one')
#include('tests.partials.two')
</div>
</div>
#stop
tests.partials.one.blade.php
#section('content')
#parent
<h1>One</h1>
#stop
#section('javascript')
#parent
<script>Scripts from one</script>
#stop
tests.partials.two.blade.php
#section('content')
#parent
<h1>Two</h1>
#stop
#section('javascript')
#parent
<script>Scripts from two</script>
#stop
For further reference: http://laravel.com/docs/5.0/templates
In my case, I'm not set the section #stop. After using #stop, it works properly.
So,
I basically have a component which requires javascript to be loaded beforehand.
master layout:
//layouts/master.blade.php
...
#yield('scripts')
...
#include('forms.search')
...
my component:
//forms/search.blade.php
#section('scripts')
some scripts here
#stop
...
what Im calling:
//main.blade.php
#extends('layouts.master')
This does not work. Section is not added to header. Am I doing something wrong or it's not possible at all with laravel?
You are trying to yield the section before including. So try this.
In your //main.blade.php
#extends('layouts.master')
And in //layouts/master.blade.php
#include('forms.search')
And //forms/search.blade.php
some scripts here
you are calling
#extends('layouts.master')
which has a
#yield('scripts')
but you are declaring the section scripts on forms/search.blade.php
so if you examine correctly, you are declaring scripts on the wrong blade template OR you have put the yield area on the wrong blade template.. because since the #yield is on the layouts/master.blade.php, it had been executed already before the #include, which does not extend anything so declaring #section there wouldn't matter.
to achieve what you want, the
#section('scripts')
some scripts here
#stop
section should be in the main.blade.php file..
if i'm gonna do that, it would be something like this:
layouts/master.blade.php
<html>
<head>
<!-- more stuff here -->
#yield('scripts')
<!-- or put it in the footer if you like -->
</head>
<body>
#yield('search-form')
#yield('content')
</body>
</html>
forms/search.blade.php
//do whatever here
main.blade.php
#extends('layouts/master')
#section('scripts')
{{ HTML::script('assets/js/search-form.js') }}
#stop
#section('search-form')
#include('forms/search')
#stop
OR remove the #yield('search-form') totally on master.blade.php and on main.blade.php do this:
#section('scripts')
{{ HTML::script('assets/js/search-form.js') }}
#stop
#section('content')
#include('forms/search')
<!-- other stuff here -->
#stop
I was having the same problem, What got it working for me was adding "#parent" to ALL my sections...
{{-- Main area where you want to "yield" --}}
#section('js')
#show
#section('js')
#parent
{{-- Code Here --}}
#stop