Skip to main content

Blueprint based page aliases in Statamic templates

🚨 Warning: This tip uses undocumented Statamic APIs. Use at your own risk.

The page variable in Statamic templates contains the current entry, and is the primary way to access entry values in Blade. This works great, but when you’re working on non-page collections you might prefer to use a variable name that reflects the current blueprint, perhaps because it simplifies copying code from other templates:

<h1>{{ $article->title }}</h1>
<p>{{ $article->date }}</p>

You could manually define another variable in each template, but there’s a better way! Cascade hydrated callbacks allow you to add variables to the cascade. The example below will add an extra entry variable to the cascade named after the blueprint handle.

# app/Providers/AppServiceProvider.php
use Facades\Statamic\View\Cascade;
use Statamic\Entries\Entry;
use Statamic\Taxonomies\LocalizedTerm;
Cascade::hydrated(function ($cascade) {
$content = $cascade->content();
if ($content instanceof Entry) {
$handle = $content->blueprint()->handle();
$cascade->set($handle, $content);
} elseif ($content instanceof LocalizedTerm) {
$handle = $content->term()->blueprint()->handle();
$cascade->set($handle, $content);

You can define this callback in a service provider's boot method. Once in place you’ll be able to use {{ $article->title }} as well as {{ $page->title }} in your templates.

18th May 2022