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
Related
I would like to know how to display alternative 'title' and other metadata on the login page.
Say I have a target URL say, https://example.com/thing/12345 ... if not logged in the user gets redirected to the login page, fine that's what I want, but on the login page I would like to inject the title, and maybe other metadata from the target page.
So that if a link such as above is shared in say Facebook, it will display the title/meta of the target URL and not the login URL.
AS per Laravel Documenation:
In your master template use #yield('title') to dynamically change the title
<html>
<head>
<title>App Name - #yield('title')</title> // add this code
</head>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
From your blade template use #section('title') to pass title to master layout
#extends('layouts.master')
#section('title', 'Page Title') // pass dynamic title from here
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#stop
#section('content')
<p>This is my body content.</p>
#stop
Laravel -> Templates -> Blade Templating
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
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
I a'm trying to yield my content from controllers. But I don't want to define again and again that I want to yield the 'Content' section
How can I archive this, so I don't need to place
#section('body')
<h1>Content</h1>
<p>More content</p>
#stop
again is every view
For example, ASP.NET MVC with RenderBody()
Use Blade templating engin, create a master layout in your `app/views/layouts' folder something like this template:
<!-- master.blade.php -->
<html>
<head></head>
<body>
<div class="container">
#yield('content')
</div>
</body>
</html>
Then in your every child view, just extend the master view, for example:
<!-- home.blade.php -->
#extends('layouts.master')
#section('body')
<h1>Content</h1>
<p>More content</p>
#stop
So, whenever you'll use something like this:
return View::make('home');
Your child view will extend the master view and content will be yielded inside the div.container.
I have login.blade.php in views/users/ that I would like to exclude from the master layout that I have.
Instead I want the login page to be a standalone page with just the login form on it.
How may I achieve that?
Use a different layout for login page:
File app/views/login.blade.php:
#extends('layouts.standalone')
#section('content')
...
#stop
And for your other pages:
File app/views/home.blade.php:
#extends('layouts.master')
#section('content')
...
#stop
And here your layouts:
File app/views/layouts/standalone.blade.php:
<html>
<body>
This is a master layout
#yield('content')
</body>
</html>
File app/views/layouts/master.blade.php:
<html>
<body>
This is a standalone layout
#yield('content')
</body>
</html>