I'm trying to include a template (widget.blade.php) inside a template (master.blade.php) as below. both templates use sections named 'content' but the widget output uses the content from the master instead of the widget content.
master.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>
#section('title')
#show
</title>
</head>
<body>
<div>
#section('content')
#show
</div>
<div>
#section('sidebar')
#show
</div>
</body>
</html>
widget.blade.php
<div class="widget">
#section('content')
#show
</div>
index.blade.php
#extends('master')
#section('content')
Main Content
#stop
#section('sidebar')
#include('mywidget')
#stop
mywidget.blade.php
#extends('widget')
#section('content')
My Widget Content
#stop
The final result
Main Content
Main Content
Any ideas how to fix this collision?
This could be fixed by using #overwrite instead of #stop
#extends('widget')
#section('content')
My Widget Content
#overwrite
source:
http://laravel.com/docs/templates#other-blade-control-structures
Related
I'm newbie in laravel. When I try to make master layout example, I get the content of my view printed out twice.
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>
page.blade.php:
#extends('layouts.master')
#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
and my Route web.php:
Route::get('blade', function () {
return view('page');
});
I trying use #stop intead of #endsection but it doesn't work.
I am getting this error:
View [layouts.master] not found. (View: C:\xampp5\htdocs\laravel\laravel\resources\views\page.blade.php)
This is my 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>
This is the page.blade.php:
#extends('layouts.master')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<h2>{{$name}}</h2>
<p>This is my body content.</p>
#endsection
And here is the relevant section of route:
Route::get('blade', function () {
return view('page',array('name' => 'Random Name'));
});
Put master.blade.php in this directory:
C:\xampp5\htdocs\laravel\laravel\resources\views\layouts
Or change path for the layout:
#extends('layouts.master')
to this:
#extends('master')
Do like this
#extends('master')
#section('title', 'Page Title')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#endsection
#section('content')
<h2>{{$name}}</h2>
<p>This is my body content.</p>
#endsection
i'm learning Laravel frame work, and have a question:
in this code:
<!DOCTYPE HTML>
<html>
<body>
<nav>
#section('nav')
<a>Home page</a>
#show
</nav>
<div id="content">
#yield('content')
</div>
</body>
</html>
what to do: #show ?!
Having a layout (layout.blade.php) view of
<!DOCTYPE HTML>
<html>
<body>
<nav>
#section('nav')
<a>Home page</a>
#show
</nav>
<div id="content">
#yield('content')
</div>
</body>
</html>
And a view (users.blade.php) of
#extends('layout')
#section('nav')
> <a>Users</a>
#stop
You should see rendered:
Home page > Users
But you could also use stop on layout:
#section('nav')
<a>Home page</a>
#stop
And #parent on your view:
#section('nav')
#parent
> <a>Users</a>
#stop
Take a look at video's 6 and 7 regarding "blade basics" and "master pages"
https://laracasts.com/series/laravel-from-scratch
I have these three blade templates
In my controller I'm calling dashboard view like this:
View::make('layouts.dashboard');
master.blade.php
<?= stylesheet_link_tag() ?>
<?= javascript_include_tag() ?>
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#yield('leftside', 'left-side content')
</div>
<div class="rightside">
#yield('rightside', 'right-side content')
</div>
</div>
</body>
dashboard.blade.php
#extends('layouts.master')
#section('leftside')
#yield('sidebar', 'sidebar content')
#stop
sidebar.blade.php
#extends('layouts.dashboard')
#section('sidebar')
aaa
#stop
The dasbhoard blade shows properly in master blade, but sidebar blade doesn't want to show. Does anyone know what i'm doing wrong?
Thank your very much for any help :)
I don't think you can use yield() on a blade template that is not called.
Basically what you can do is make your master.blade.php into this
<html>
<body class="fixed">
<div class="wrapper">
<div class="leftside">
#include('sidebar') {{-- This is sidebar.blade.php --}}
</div>
<div class="rightside">
#include('dashboard') {{-- This is dashboard.blade.php --}}
</div>
</div>
</body>
</html>
Then, assuming that your are calling the dashboard.blade.php template, with both blade templates included. You can use the sections on either master.blade, or sidebar.blade inside dashboard.blade
#extends('layouts.master')
{{-- You can define now the sections available on master.blade and dashbard.blade --}}
OK I found out what was wrong :)
I called View::make('layouts.dashboard') instead of View::make('layouts.sidebar');
This is my scenario:
I have made a main.blade.php and it contains the following code:
<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
<div id='header'>
#yield('header')
</div>
<div id='main-wrapper'>
#yield('content')
</div>
<div id='footer'>
#yield('footer')
</div>
</body>
</html>
And then this is the controller:
class Userindex extends BaseController
{
protected $layout = "main";
function register ()
{
$this->layout = View::make("user.register")
->with("title","Registration");
}
}
Here is the register.blade.php in user folder:
#extends('main')
#section('content')
<p>This the content of the page register.</p>
#stop
My PROBLEM: I have two other pages as header.blade.php and footer.blade.php that I don't know how should I include them into the register.blade.php
Do a little adjustment to you layout file, replace #yield with #include and that's it!
<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
<div id='header'>
#include('partials.header')
</div>
<div id='main-wrapper'>
#yield('content')
</div>
<div id='footer'>
#include('partials.footer')
</div>
</body>
</html>