I'm starting with laravel. Now have created an admin Panel design.
I want to display one view (menu in the left side for admin Panel) in default for all routings started on /admin
Then load the content of the container depending on the route.
Do I have to set in all routes my view for left side or I can set it "globally" for all routes?
Laravel version I'm using is 5.7
You have just to create a layout for the admin, and all controller related to admin action will render view which extend the admin layout which you have define.
The layout file admin.blade.php
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<div id="menu">
<!-- use the css to style this element on the left -->
<ul>
<li>Your Menu code goes here</li>
</ul>
</div>
<!-- Here is where all content related to each route will be rendered -->
<div class="container">
#yield('content')
</div>
</body>
</html>
If you have a Controller which allow you to Edit Post you can render a view admin/edit.blade.php which is define like this
#extends('admin.blade.php')
#section('content')
Content to be display related to each route goes here
#endsection
The content which is wrap within the #section('content') will be render in the admin.blade.php which is the admin layout in the the container which wrap the #yield('content)`
Related
i want to extend the master.blade.php located in views\backend folder.
view/backend/master.blade.php <--i want to extend this file
view/backend/partials/header.blade.php <--the file that will extend the master.blade.php
folder structure
views
backend
-master.blade.php
partials
-header.blade.php
-footer.blade.php
-sidebar.blade.php
I tried this in the header.blade.php file but fail:
#extends('backend.master')
Edited
master.blade.php
<body>
<div class="wrapper">
#yield('header')
#yield('sidebar')
#yield('content')
#yield('footer')
</div>
</body>
header.blade.php
#extends('backend.master')
#section('header')
<p> this is the header</p>
#endsection
The page shows master content only in the browser
Normally header, footer and sidebar contain the markup which remain common across pages. Only the content varies from page to page.
Also the concept of extending master layout is to avoid repeating the shared parts across various pages. Using #yield('header') #yield('footer')
#yield('sidebar') defeats the concept of extending master layout. Because then all those sections need to be included on all pages.
So you master layout should be like
<body>
<div class="wrapper">
#include('backend.partials.header')
#include('backend.partials.sidebar')
#yield('content')
#include('backend.partials.footer')
</div>
</body>
Then for any page you can extend the master layout as
#extends('backend.master')
#section('content')
<!-- content markup here -->
#endsection
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 have a full html template, that I am trying to use with laravel.
The template has a big image slider (that should only be in homepage) and a couple of other codes like contact form, accordion, twitter widget... my goal is to place those parts in separate blade templates and call them when needed. For this, I have this folder scheme.
..
/views
/emails
/home
- slider.blade.php
- contact-form.blade.php
- twitterwidget.blade.php
/layouts
- master.blade.php
So, for example master.blade.php looks like this.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class='header'>
//header
#yield('slider')
</div>
<div class='content'>
//content
#yield('contact-form')
</div>
<div class='footer'>
//footer
</div>
</body>
</html>
Now that is a basic examples which yields a slider inside the headers tags, and this is the slider.blade.php:
#extends('layouts.master')
#section('details')
<div class='slider'> I am a slider </div>
#stop
but when I create a route, I am forced to create one point to one template. as:
Route::get('/', function(){
return View::make('home/slider');
});
This only renders the layout by pulling the slider. But I want to contact form to be rendered also.
In master.blade.php file instead of
#yield('contact-form')
you should use:
#include('home.contact-form')
or you can edit your slider.blade.php file and add at the end:
#section('contact-form')
#include('home.contact-form')
#stop
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.