Laravel Classroom image

Yasar  Khan / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3|
Lessons:-Blade Templates

13.3 Blade Layouts
Blade offers another method of writing complex or nested layouts. Using its clean syntax, this
could well be the best layout implementation available within the framework. You simply have
to use it to understand its beauty. Let’s have a look at our primary template, which is a blade
view (in this case named template.blade.php) like any other.1 <!DOCTYPE HTML>
2 <html lang="en-GB">
3 <head>
4 <meta charset="UTF-8">
5 <title>@yield('title')</title>
6 </head>
7 <body>
8 <div class="header">
9 <ul>
10 @section('navigation')
11 <li><a href="#">Home</a></li>
12 <li><a href="#">Blog</a></li>
13 @yield_section
14 </ul>
15 </div>
16
17 @yield('content')
18
19 </body>
20 </html>
Our main template uses the @yield() method to define a content region that can be filled by a
view that uses this layout. Simply pass a string to the method to provide a nickname for that
content region, it will then be used to identify it later.
The @section() and @yield_section are used to define a content region that will contains some
default data that could be replaced at a later date. Let’s have a look at a view (page.blade.php)
that makes use of the template we have just created.
1 @layout('template')
2
3 @section('title')
4 Dayle's Webpage!
5 @endsection
6
7 @section('navigation')
8 @parent
9 <li><a href="#">About</a></li>
10 @endsection
11
12 @section('content')
13 <h1>Welcome!</h1>
14 <p>Welcome to Dayle's web page!</p>
15 @endsection
In this view we use the @layout() method to specify that we want to use a view named template
that we created earlier as our layout. Now we can use @section() and @endsection to replace
yielded content regions with the code found between these two methods.In the case of the navigation section you will notice that we have a @parent stub inside the
content area. Blade will replace this with the content from the base template.
If we return..
1 return View::make('page');
From our route/action we can now see our page, wrapped within the layout template, like so..
1 <!DOCTYPE HTML>
2 <html lang="en-GB">
3 <head>
4 <meta charset="UTF-8">
5 <title>Dayle's Webpage!</title>
6 </head>
7 <body>
8 <div class="header">
9 <ul>
10 <li><a href="#">Home</a></li>
11 <li><a href="#">Blog</a></li>
12 <li><a href="#">About</a></li>
13 </ul>
14 </div>
15
16 <h1>Welcome!</h1>
17 <p>Welcome to Dayle's web page!</p>
18
19 </body>
20 </html>
Great! We can also use as many templates as we want to, they are simply normal Blade views!
What if we want to provide the @section contents from our Action/Route? Simply call the
Section::inject() method with the section name, and a string representing the sections
contents, for it to be injected into the view.
1 Route::get('/', array('do' => function()
2 {
3 Section::inject('title', 'My Site');
4
5 return View::make('page');
6 }));
and that’s all! Now you can use Blade to make your views look clean and efficient, your designer
will love you for it.

 

 

 

 
 
 

Yasar  Khan

Skills    Laravel

Qualifications :-
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:- Hi i am yasar working as web developer since last 3 years.
Explore
 

  Students (0)