Skip to content

Instantly share code, notes, and snippets.

@jasonvarga
Created February 1, 2016 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonvarga/efa205f88ffc93fd497d to your computer and use it in GitHub Desktop.
Save jasonvarga/efa205f88ffc93fd497d to your computer and use it in GitHub Desktop.
title overview id
Laravel Blade
Blade is the templating engine supplied with Laravel. We think its neat and we won't even be offended if you want to use it. Here's how.
efa34b0d-b4c3-43fc-bb91-a6d331dc6026

The Blade Templating Language

You can find out how to use Blade, the templating language over on the Laravel docs.
Here we'll be explaining how to use it in the context of Statamic.

Using in a theme

Firstly, you'll want to check out how to create a theme in our Theming Guide.

To use Blade in a theme, you'll need to add .blade.php templates instead of .html templates. You can mix and match between Antlers and Blade template files, just be careful not to overlap the template names.

For instance, telling a page to use the foo template:

template: foo
your-theme/
`-- templates/
    |-- foo.html
    `-- foo.blade.html

There are two foo templates. Which one should be used? Don't make this mistake.

Extends and Includes

When using @extends or @include, be aware that the files will be located in your templates directory.

Accessing Data

When using a Blade template, you will get access to the top level page data. For example, if you were to visit /blog/my-first-post, which is an entry with the following data:

---
title: My First Post
favorite_things:
  - bacon
  - whisky
gamertags:
  swanson45: Ron Swanson
  npeppers: Niles Peppertrout
---
This is the first post, how exciting.

Your Blade template would have access to the variables in this file:

@extends('site')

<h1>{{ $title }}</h1>

@foreach ($favorite_things as $thing)
    {{ $thing }}
@endforeach

@foreach ($gamertags as $tag => $name)
    {{ $name }} goes by {{ $tag }}
@endforeach

{{ $content }}

Modifiers

You can use Modifiers in your Blade templates, but they will use a different syntax than what you're used to with Antlers.

First, wrap your variable in a modify() method, then feel free to chain modifiers as you wish. The value will get passed along like normal. Any parameters should be specified like regular PHP parameters.

{{ modify($content)->striptags()->backspace(1)->ensureRight('!!!') }}
THIS IS THE FIRST POST, HOW EXCITING!!!

Note that when using multi-word modifiers, like ensure_right, you can either use the documented snake_case version, or use the camelCased version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment