I am new to Laravel and I am trying to create my first layout for a new site I'm making.
The problem I am having is that content I want in the <head>
is going into <body>
, and <head>
is empty.
I have:
layout.blade.php
<!DOCTYPE html>
<html>
<head>
<title>@section('title')</title>
{{ HTML::style('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'); }}
</head>
<body>
<h1>@yield('h1')</h1>
@yield('content')
{{ HTML::script('js/jquery-1.11.1.min.js'); }}
{{ HTML::script('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'); }}
</body>
</html>
users.blade.php
@extends('layouts.layout')
@section('title')
Users Page - 1
@section('h1')
<?PHP echo $h1;?>
@stop
Where am I going wrong?
Also what is the difference in @yeild
and @section
in my view?
Try this in layout.blade.php. You are extending this blade template, so you should use @yield('title')
in <title>
tag ,and you should @stop
every @section
<!DOCTYPE html>
<html>
<head>
<title>@yield('title')</title>
{{ HTML::style('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'); }}
</head>
<body>
<h1>@yield('h1')</h1>
@yield('content')
{{ HTML::script('js/jquery-1.11.1.min.js'); }}
{{ HTML::script('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'); }}
</body>
Actually you should use yield
to dump out the content, for example, if you have a mastwer layout like:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@yield('content')
</body>
</html>
Then you may use in your child view something like this:
extends('layouts.master')
@section('content')
Everything within this section will be dumped
in to `@yield('content')` in your master layout.
@stop
You should use @stop
for any section to close that section. Probably you may try something like this:
<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
{{ HTML::style('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css') }}
</head>
<body>
@yield('content')
{{ HTML::script('js/jquery-1.11.1.min.js'); }}
{{ HTML::script('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'); }}
</body>
</html>
From your controller
you may pass the title like:
return View::make('viewname')->with('title', $title);
Read more in about Templates
on Laravel website.
We had the same issue, though the cause was different. There were two @includes that were in the source that came BEFORE the @section('body'). We don't know why this would cause the header issue, but it did. It was
@extends('layouts.plane')
@include("layouts.modal-quote-create")
@include("layouts.modal-mulch-types")
@section('body')
and we changed it to:
@extends('layouts.plane')
@section('body')
@include("layouts.modal-quote-create")
@include("layouts.modal-mulch-types")
and that took care of the issue.