Laravel Blade

Blade is the simple template/theme system built into Laravel and up until this point it has been very basic. It supported most of the simple things such as variables {{ $var }}, @if @else, @section, and finally a thing called @yield.

Because of the simplicity of it on a new project I thought we would need something more advanced and went with Twig. Twig had more features and was more powerful but it also didn’t really fit into my workflow. The biggest problem was working with Eloquent results and twig doesn’t like calling results like this:

foreach (Book::with('author')->get() as $book)
    echo $book->author->name;

I asked Taylor about it and he did a few changes to get that working but then I had troubles with other calls. So it just felt like it was more of a fight than what it was worth. After discussing this, last week Taylor and Shawn McCool really made Blade awesome. First Shawn recommended a “render_each” method. Which is an excellent feature in my opinion. It works my including a looped view:

    {{ render_each('categories.list', $categories, 'cat') }}

// categories/list.blade.php
<li>{{ $cat->name }}</li>

As you can see that is a big time saver. But it doesn’t stop there. Now Blade can have layouts inheritance and several other really useful goodies:



    <p>My content</p>


Then inside layouts.common:


This is just some of the basics of the new features and it does a lot more. Although I don’t consider it a full replacement for Twig it has 90% or more of the features I wanted to use. The biggest missing feature (depending on your needs) is that Blade still just parses as php. So it can call things that are not assigned and doesn’t prevent you from using php code in your views. Personally I like that feature but if you are really wanting to limit what people can do in the views then this may not be ideal. So use what is best for the task at hand.

For a video overview of what is coming please watch this:



Comments disabled.