Eric Barnes

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:

<ul>
    {{ render_each('categories.list', $categories, 'cat') }}
</ul>

// 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:

@layout('layouts.common')

@section('content')

    <p>My content</p>

@endsection

Then inside layouts.common:

<html>
<head>
    <title>Test</title>
</head>
<body>
    @yield('content')
</body>
</html>

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:

Laravel Bundle Assets

The past few weeks I have been building a few Laravel bundles and one of the features of bundles is the ability to have assets that are moved to your public folder during install. This is great when your bundle has css, js, images, etc..

The downside to this though is that while building the actual bundle it is a pain to keep having to run the following command each time you change a file:

php artisan bundle:publish

I have came up with a few different options to make this easier for developers. The first method which I believe would be supported across every os is to symlink the assets to public/bundles/folder/. However the down side to this is you have to add new symlinks when you add new files. So not ideal for the way I typically develop.

The second option is specific to mac but it works wonderfully. What I have done is purchase live reload and use it to run the bundle publish command. The reason I originally purchased the app was because I am using less for my css and I liked the fact that it would compile the css and also refresh your browser so you can see changes instantly.

The bigger benefit is live reload allows you to run terminal commands after you save and before it refreshes your browser. So all you have to do is add in your artisan command and not have to worry about it again. Here is a screenshot showing this:

Hopefully this will help you if you are building bundles with public assets. If you have any other tips please post them in the comments.

Laravel Log Viewer

A simple Laravel bundle to display your log files via the browser.

Installation

php artisan bundle:install logviewer

Publish assets

php artisan bundle:publish logviewer

Then edit your application/bundles.php file and add:

return array(
    'logviewer' => array(
        'location' => 'logviewer',
        'handles' => 'logviewer'
    )
);

Download

You can clone the repo by running the code below:

$ git clone git://github.com/ericbarnes/Laravel-Log-Viewer

Or visit the GitHub Repo.

New Laravel Bundle App

Since starting at UserScape I have been tasked with building a new bundle application for the Laravel framework. So far all the work has been done “behind the scenes” but I wanted to share a few screenshots of what I have completed so far.

Home Page

This is a temporary home page which showcases the latest and the most popular. Currently the most popular will be determined by number of installs but I have some ideas for better stats and filtering that will come later.

Bundle Grid

This is the grid list. I tried keeping it simple so you can quickly scan the titles and also show each users gravatar for branding.

I am really happy with what I have completed so far and it should be a simple and effective way for users of the framework to find pre-built code to put in their applications.

If you are interested in seeing how to create a bundle check out Taylor’s Videos

A nice and simple flat file markdown based blog application built by Ian Landsman for use on his site. The description says:

I built this to run ianlandsman.com over a weekend :) It may not work for you. I’m posting it up in case anyone else may find it useful. It might also be useful to those interested in working with the PHP Laravel framework.

Although he states it isn’t complete yet from my tests it works like it supposed to but still missing a few small features. I like the setup and the use of the command line to publish the posts and clear the cache.