Templating in Laravel
You are mixing two different layout approaches of Laravel.
This way you are rendering the layout view, include the home view and try to include inside again the layout.
My personal preference is the controller approach.
Controller Layouts
The controller and the layouts can remain the same.
Note: As a shortcut you could nest the content instead of View::make, that automaically renders it when you echo it out in the layout.
In home.blade.php remove the @layout function.
Edit (example):
controllers/home.php
<?php
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public function action_index()
{
$this->layout->title = 'title';
$this->layout->nest('content', 'home', array(
'data' => $some_data
));
}
}
views/layouts/default.blade.php
<html>
<title>{{ $title }}</title>
<body>
{{ $content }}
</body>
</html>
views/home.blade.php
Partials are included in the content.
@include('partials.header')
{{ $data }}
@include('partials.footer')
Blade Layouts
If you want this approach you have a few problems there. First, you are including new content after the layout. Not sure if intentional, but the @layout function itself is basicly just an @include restricted to be at the very beginning of the view. So if your layout is a closed html, any include after that will be appended after your html layout.
Your content should use sections here with the @section function and @yield it in your layout. The header and footer could be included in the layout with @include or if you want to define it in the content view then put those in a @section too, like below. If you define it that way if a section doesn't exist nothing gets yielded.
controllers/home.php
<?php
class Home_Controller extends Base_Controller {
public function action_index()
{
return View::make('home')->with('title', 'title');
}
}
views/layouts/default.blade.php
<html>
<title>{{$title}}</title>
<body>
@yield('header')
@yield('content')
@yield('footer')
</body>
</html>
views/home.blade.php
@layout('layouts.default')
@section('header')
header here or @include it
@endsection
@section('footer')
footer
@endsection
@section('content')
content
@endsection
How @include works in blade templating in Laravel
The blade template engine works by turning blade-html files into php-html files. @include
will be replaced only once e.g.
<!-- parts/post.blade.php -->
<p>This is my post: {{$post}} </p>
<!-- some-template.blade.php -->
@foreach($posts as $post)
@include('parts.post')
@endforeach
Will be rendered into the following php-html code and saved into a view file (see storage/framework/views
if you want to see this):
<?php for($posts as post){ ?>
<p>This is my post: <?php echo($post); ?> </p>
<?php } ?>
How can i template a partial in Laravel?
Blade does allow for this out of the box.
Layouts ( Docs )
<!-- Stored in resources/views/layouts/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>
includes (Docs)
You can then add smaller templates to your layout using includes
@include('view.name', ['some' => 'data'])
Components (Docs)
And finally, if you want you have even more control, try components.
Note: Components are now a little more complicated than they were but are still backward compatible. So you can still define components like so:
modal.blade (Component)
<!-- Modal -->
<div
class="modal fade {{ $class ?? '' }}"
id="{{ $id }}"
tabindex="-1"
role="dialog"
aria-labelledby="{{ $id }}Title"
aria-hidden="true"
>
<div class="modal-dialog modal-dialog-centered {{ $size ?? '' }}" role="document">
<div class="modal-content shadow">
<div class="modal-header">
<h5 class="modal-title font-weight-normal" id="{{ $id }}Title">
{{ $title }}
</h5>
<button type="button" class="close close-icon" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@if ($form) {{ $action }} @endif
<div class="modal-body">
{{ $body }}
</div>
<div class="modal-footer">
{{ $footer }}
</div>
@if ($form) {!! Form::close() !!} @endif
</div>
</div>
</div>
Usage
@component('components.modal', [
'id' => 'myModalID',
'class' => 'modal-info',
'form' => true
])
@slot('title')
My Modal
@endslot
@slot('action')
{!! Form::open([]) !!}
@endslot
@slot('body')
Some content
@endslot
@slot('footer')
<button type="submit" class="btn">
Submit
</button>
@endslot
@endcomponent
Laravel Blade Template
You don't use Laravel blade syntax when passing variables.
@section('title', $category->name)
Remember that anything inside {{}}
or passed as a @func()
variable is interpreted by PHP literally.
laravel blade template not rendering
You want to be using @include('layouts.header')
rather than @yield
.
@yield
is what you use on a master template to specify where content will go that you can then define on a child view.
@include
"allows you to easily include a Blade view from within an existing view." - https://laravel.com/docs/5.1/blade
How to make php layouts in Laravel 5?
There are many methodologies that deal with handling templates.
Here are few,
1. Using a regular include
or require
You can include the header.php
, sidebar.php
and footer.php
and as many files that you prefer for each sector(It depends on the size of the template)
2. Using a common file and having classes inside it
Include a single file and call the classes to render each area
like
class Head {
public function render($_page, $_data) {
extract($_data);
include($_page);
}
}
3. Use a Templating Engine
You shall prefer few templating engine like smart, raintpl etc., (I guess you don't prefer it ;) )
4. Acquiring by inc
You can include as suggested here
<html>
<head>
<title><?=$this->title</title>
</head>
<body>Hey <?=$this->name?></body>
</html>
And the php area would be
$view = new Template();
$view->title="Hello World app";
$view->properties['name'] = "Jude";
echo $view->render('hello.inc');
5. By having template segments in db
Believe me, I saw many good sites which stores the template in the database and it will be rendered each time. It might look like strange idea, but even i tried it for one of my project.
Conclusion :
But if i use Laravel, for sure i will prefer the Blading Tempalte Engine and I recommend you the same.
Update :
Few benefits of Using Blade Templates
1. Easy Setting of attributes
Set the attributes on the go
<title>App Name - @yield('title')</title>
2. Easy yielding
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
3. Simple echoing
Like this
Hello, {{ $name }}
4. Easy Condition
Like this
{{ isset($name) ? $name : 'Default' }}
5. Never Escape
Like this
Hello, {!! $name !!}.
6. Beautiful If Statements
I prefer this way to make my code more beautiful
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
7. Checking Authentication
The simplest way to check the authentication
@unless (Auth::check())
You are not signed in.
@endunless
8. Easy For Loop
How this for loop looks like
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
9. Awesome foreach statement
Splitting the key and value can't be more easy than this
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
10. Include the files
How about include
file like this
@include('view.name')
11. Passing parameters to views
Can Pass this array to your view
@include('view.name', ['some' => 'data'])
Source : Laravel Templates
Related Topics
Next Business Day of Given Date in PHP
$_File Upload Large File Gives Error 1 Even Though Upload_Max_Size Is Bigger Than the File Size
Multi-Dimensional Array Post from Form
Upload Multiple Files to PHP Server Using Curl Command Line
JSON: PHP to JavaScript Safe or Not
PHP Image Resize on the Fly VS Storing Resized Images
Parsing Http_Range Header in PHP
Imagecreatefromjpeg and Similar Functions Are Not Working in PHP
Mysql_Connect VS MySQL_Pconnect
How to Allow Remote Access to My Wamp Server for Mobile(Android)
Store HTML Entities in Database? or Convert When Retrieved
Codeigniter: How to Do a Select (Distinct Fieldname) MySQL Query
How to Eliminate PHP5 Strict Standards Errors
PHP Put a Space in Front of Capitals in a String (Regex)
How to Find Annotations in a PHP5 Object
Best Way to Parse an Invalid HTML in PHP
What Is Difference of Developing a Website in MVC and 3-Tier or N-Tier Architecture