Blade Templates

Antlers is not always the best template engine for the job. If you're using Statamic as a headless CMS or want to share views with a Laravel application already using Blade or another engine, you can do that. Just know there are a few caveats before you do.

Overview

While Statamic's Antlers template language is powerful, tightly integrated, and simple to learn, it's not the only way to build your frontend.

Antlers handles the responsibilities of both Blade and Controllers, all in your template. If you choose to not use Antlers, know that you'll have to do additional work in PHP somewhere to fetch and prep content another other way.

You can use Blade or other template engines by using their respective file extensions.

How to Render A Template with Blade

Instead of naming your views myview.antlers.html use myview.blade.php extension (or whatever other engine's extensions you may have installed).

View Data

You will have access to the same top level data as you would in Antlers views.

---
title: My First Breakdance Competition
moves:
- Toprock
- 6-step
- Windmill
- L-kick
- Headspin
---
I did not win but I did have good timez.
<h1>{{ $title }}</h1>
 
<p>First I did
@foreach ($moves as $move)
{{ $move }}, then I did
@endforeach
and it was sick.</p>
 
{{ $content }}
Hot Tip!

Antlers outputs unescaped values by default, while {{ $content }} in Blade will be escaped. If you need to output unescaped HTML, use {!! $content !!}

Using Modifiers with Blade

You can use Modifiers in Blade templates with a Laravel-style fluent syntax.

Wrap your value with the Statamic\Modifiers\Modify::value() method and chain modifiers as you wish. The value will get passed along in sequence like it does in Antlers. Any parameters should be specified like regular PHP parameters.

{{ Statamic\Modifiers\Modify::value($content)->striptags()->backspace(1)->ensureRight('!!!') }}
THIS IS THE FIRST POST, HOW EXCITING!!!

You can use service injection to make your templates read a little nicer, and pass the value straight into
it, rather than using a value method.

@inject('modify', 'Statamic\Modifiers\Modify')
 
{!! $modify($title)->wrap('h1') !!}
{{ $modify($content)->striptags()->backspace(1)->ensureRight('!!!') }}

You could also opt to create a global helper in your project.

use Statamic\Modifiers\Modify;
 
function modify($value): Modify
{
return Modify::value($value);
}
{!! modify($title)->wrap('h1') !!}
{{ modify($content)->striptags()->backspace(1)->ensureRight('!!!') }}
Hot Tip!

When using multi-word modifiers, like ensure_right, you must use the camelCased version (ensureRight).

Layouts

When Statamic attempts to render a URL (eg. an entry), two views are combined. A template gets injected into a layout's template_content variable.

When the template is not an Antlers view, this rule doesn't apply. The layout is ignored, allowing you to use @extends the way you would expect.

{{-- mytemplate.blade.php --}}
 
@extends('layout')
 
@section('body')
The body content
@endsection
{{-- mylayout.blade.php --}}
 
<html>
<body>
@yield('body')
</body>
</html>
<html>
<body>
The body content
</body>
</html>

This rule only applies to the template. You're free to use a .antlers.html template and a .blade.php layout.
If you want to do this, instead of a yield, the contents of the template will be available as in the template_content variable.

{{# mytemplate.antlers.html #}}
 
The template content
{{-- mylayout.blade.php --}}
 
{!! $template_content !!}
<html>
<body>
The template contents
</body>
</html>
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →