Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a sub-view in Blade templates?

I am trying to set up a site using laravel, but I'm really having trouble with basic things that the documentation just doesn't cover.

In this case, I see that it says I can include one view inside another by using @include('view.name'). What is view.name? Where is it saved? I tried creating a file app/views/view.name.blade.php, but it wasn't read. How does the file name map to the blade name?

like image 584
Benubird Avatar asked Feb 13 '14 12:02

Benubird


People also ask

How do you put a blade in another blade in Laravel?

We almost need to use include blade file inside another blade file. as specially when you need same design and code use again and again then you will create conman file and call it each tile. laravel provide @include directive to include sub view inside blade file.

What is @include in Laravel?

@include is just like a basic PHP include, it includes a "partial" view into your view. @extends lets you "extend" a template, which defines its own sections etc. A template that you can extend will define its own sections using @yield , which you can then put your own stuff into in your view file.

Can I use blade template without Laravel?

Yes you can use it where ever you like.


2 Answers

You can use the blade template engine:

@include('view.name')  

'view.name' would live in your main views folder:

// for laravel 4.X app/views/view/name.blade.php    // for laravel 5.X resources/views/view/name.blade.php 

Another example

@include('hello.world'); 

would display the following view

// for laravel 4.X app/views/hello/world.blade.php  // for laravel 5.X resources/views/hello/world.blade.php 

Another example

@include('some.directory.structure.foo'); 

would display the following view

// for Laravel 4.X app/views/some/directory/structure/foo.blade.php  // for Laravel 5.X resources/views/some/directory/structure/foo.blade.php 

So basically the dot notation defines the directory hierarchy that your view is in, followed by the view name, relative to app/views folder for laravel 4.x or your resources/views folder in laravel 5.x

ADDITIONAL

If you want to pass parameters: @include('view.name', array('paramName' => 'value'))

You can then use the value in your views like so <p>{{$paramName}}</p>

like image 62
GWed Avatar answered Sep 19 '22 06:09

GWed


EDIT: Below was the preferred solution in 2014. Nowadays you should use @include, as mentioned in the other answer.


In Laravel views the dot is used as folder separator. So for example I have this code

return View::make('auth.details', array('id' => $id)); 

which points to app/views/auth/details.blade.php

And to include a view inside a view you do like this:

file: layout.blade.php

<html>   <html stuff>   @yield('content') </html> 

file: hello.blade.php

@extends('layout')  @section('content')   <html stuff> @stop 
like image 39
winkbrace Avatar answered Sep 20 '22 06:09

winkbrace