Table of Contents

Blade Files

Blade

A zombie developer's code has long and combersome syntax, whereas a Laravel developer's code contains clean and readable views by using the blade templating engine.

Using Blade Templating in your app will make your views look super clean and easily readable. Blade templating is optional to use, but after you get the hang of it, you'll find yourself wanting to use it over and over again.


Blade

Blade templating is an easy way to render your views in a more readable fashion. Blade templating is a syntax that you can use in your view files that will render into valid PHP code. Let me give you a quick example. Taking the foreach loop from our previous section we have the following code which displays our zombies in an unordered list:

<ul>
    <?php foreach($zombies as $zombie): ?>
        <li><?php echo $zombie->name; ?></li>
    <?php endforeach; ?>
</ul>

We could re-write that code using blade templating like so:

<ul>
    @foreach($zombies as $zombie)
        <li>{{ $zombie->name; }}</li>
    @endforeach
</ul>

And this makes it so much easier to read and write!

As you can see from the example above instead of doing a foreach and having to open and close your PHP tags, you can just use the @ symbol. Additionally, when we add anything inside of {{ }} it will automatically be output to the screen.

The only thing that we need to do to use the blade templating engine is to rename our files from .php to .blade.php. So, from the example in the previous section we created a new file located at resources\views\zombies.php.

If we renamed this file to resources\views\zombies.blade.php we could then use blade templating inside of that file.

Let's take another look at the blade syntax. First, take a look at the following example:

<?php if($var){ ?>
    The statement is true. variable is equal to <?php echo $var ?>
<?php } else {
    The statement is false. variable is equal to <?php echo $var ?>
<?php } ?>

All this does is print to the screen whether the statement is true or false, and then it displays the value of the variable. To clean this up using the blade templating engine, it could be written as follows:

@if($var)
    The statement is true. variable is equal to {{ $var }}
@else
    The statement is false. variable is equal to {{ $var }}
@endif

As you can see the above code is much cleaner and easier to read. All thanks to Blade Templates!

There are many other shorthands that you can use in your blade templates including layouts, loops, if statements, and much much more.

Be sure to checkout the full documentation on using all the blade shorthand syntax here: http://laravel.com/docs/blade

Now that we've got the hang of how Views work in a Laravel app we are going to move on to Controllers, which is where most of the functionality for our app will live.

Let's read on and learn more about controllers.