Utilizing advanced Blade usage in laravel

Using Laravel’s Blade templating system, we have access to some powerful features that
make our development much quicker. For this recipe, we’ll pass some data to our blade
views and loop through it, along with some conditionals.

Getting ready

For this recipe, we’ll need the code created in the Creating a view using Blade recipe .

How to do it…

Follow these steps to complete this recipe:
1. Open the routes.php file and update the blade-home and blade-second routes
as follows:
Route::get(‘blade-home’, function()
{
$movies = array(
array(‘name’ => ‘Star Wars’, ‘year’ => ‘1977’, ‘slug’
=> ‘star-wars’),
array(‘name’ => ‘The Matrix’, ‘year’ => ‘1999’,
‘slug’ => ‘matrix’),
array(‘name’ => ‘Die Hard’, ‘year’ => ‘1988’, ‘slug’
=> ‘die-hard’),
array(‘name’ => ‘Clerks’, ‘year’ => ‘1994’, ‘slug’
=> ‘clerks’)
);
return View::make(‘blade.home’)->with(‘movies’,
$movies);
});

Route::get(‘blade-second/(:any)’, function($slug)
{
$movies = array(
‘star-wars’ => array(‘name’ => ‘Star Wars’, ‘year’
=> ‘1977’, ‘genre’ => ‘Sci-Fi’),
‘matrix’ => array(‘name’ => ‘The Matrix’, ‘year’
=> ‘1999’, ‘genre’ => ‘Sci-Fi’),
‘die-hard’ => array(‘name’ => ‘Die Hard’, ‘year’
=> ‘1988’, ‘genre’ => ‘Action’),
‘clerks’ => array(‘name’ => ‘Clerks’, ‘year’
=> ‘1994’, ‘genre’ => ‘Comedy’)
);
return View::make(‘blade.second’)->with(‘movie’
, $movies[$slug]);
});

2. In the views/blade directory, update the home.blade.php file with the
following code:
@extends(‘layout.index’)
@section(‘page_title’)
@parent
Our List of Movies
@endsection
@section(‘content’)
<ul>
@foreach ($movies as $movie)
<li>{{ HTML::link(‘blade-second/’ . $movie[‘slug’],
$movie[‘name’]) }} ( {{ $movie[‘year’] }} )</li>
@if ($movie[‘name’] == ‘Die Hard’)
<ul>
<li>Main character: John McClane</li>
</ul>
@endif
@endforeach
</ul>
@endsection
3. In the views/blade directory, update the second.blade.php file with the
following code:
@extends(‘layout.index’)
@section(‘page_title’)
@parent
Our {{ $movie[‘name’] }} Page
@endsection
@section(‘content’)
@include(‘blade.info’)

<p>
Go to {{ HTML::link(‘blade-home’, ‘the Home Page.’)
}}
</p>
@endsection
4. In the views/blade directory, create a new file named info.blade.php and add
the following code to it:
<h1>{{ $movie[‘name’] }}</h1>
<p>Year: {{ $movie[‘year’] }}</p>
<p>Genre: {{ $movie[‘genre’] }}</p>
5. Test the views by going to http://{your-server}/blade-home (where
your-server is our URL) and click on the links to see the views work.

How it works…

For this recipe, we’ll be passing some data to our Blade views, looping through it, and adding
in some conditionals. Typically, we would use this with results from a database but, for our
purposes, we’ll create a simple data array in our routes.
Our first route contains an array of movies, with their year and a slug that we can use for the
URL. Our second route will create an array with the slug as a key and accept the slug in the
URL. We then pass in the details of a single movie into the view, by calling the movie that has
the slug as a key.
In our first view, we create a @foreach loop, to run through also the data in the array. We’ve
also included a simple @if statement that checks for a specific movie and then prints out
some extra information. As we loop through, we display links to the second route, with the slug
added on.
The second view displays the name of the movie, but all includes another Blade view by using
@include() in the content block. This way, all the data is also available in the included view;
thus, for our info view, we can just use the same variables that we set in our route.

Leave a Reply

Your email address will not be published. Required fields are marked *