Laravel Tutorials / updated: August 06, 2014

Blade “or” Operator

The following is an excerpt from a previous issue of my weekly Laravel News Digest. Signup and have useful tips like this delivered right to your inbox.

This week I came across a question about ternaries in Laravel blade. Well, not asking that exactly, but that was the premise. I replied about the use of the “or” operator and apparently a few people didn’t know about it. So I’m making it this weeks tip of the week.

The basic usage of this is:

{{ $name or 'John' }}

Which compiles into:

isset($name) ? $name : 'John'

This is one of those nice little features that is easy to skip over in the documentation, but it is really great for keeping your views clean.

This also works in Envoy tasks:

@task('dev', ['on' => ['dev']])  
    git checkout origin/{{ $branch or 'develop' }}

You would call this with either of these:

envoy run dev  
envoy run dev --branch=foo

Nice and simple!

Laravel News Partners

Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.