Improved Laravel Blade support in Statamic 3.3

March 10th, 2022

Statamic 3.3 has added some additional improvements to their Blade templates support, which makes using Statamic together with Laravel's native Blade templates truly a joy to work with.

In this post I'll refer to my previous blogpost on how to use Statamic with Laravel Blade, and highlight where new improvements have been made in 3.3.

Modifying values

First up, using Statamic's extensive list of modifiers.

Before 3.3, you would have had to do the following to modify a value:

{!! \Statamic\Modifiers\Modify::value('Some text')->slugify()->upper() !!} <!-- SOME-TEXT -->

In my previous post I suggested making a modify() helper for this. Since 3.3 however you can do the following:

{!! Statamic::modify('Some text')->slugify()->upper() !!} <!-- SOME-TEXT -->

Which is simply beautiful!

Displaying page content

Not much has changed here, except when displaying page content it is now strongly recommended to use $page->field instead of $field (which is also available in your templates).

This is because the $page variable is an Entry which now has magic property access. Any Replicator, Bard or Grid fields will have their values wrapped in a Values class that allows you to use property & array access to get to the underlying augmented values.

<ul>
   @foreach ($list->value() as $value)
   @foreach ($page->list as $value)
        <li>{{ $value }}</li>
    @endforeach
</ul>

This is especially important when using Bard sets, Replicator sets or Grid fields.

Fetching content

Fetching content has improved as well and even though you're still working with an Entry object in the case below, augmented property access should just be available.

@foreach (\Statamic\Facades\Entry::query()->where('collection', 'blog')->where('status', 'published')->limit(3)->get() as $entry)
    <div>
       <a href="{{ $entry->url() }}">
           {{ $entry->augmentedValue('title') }} <!-- Note: we're working with an Entry object here, not an array, so we have to augment the values manually -->
        <a href="{{ $entry->url }}">
           {{ $entry->title }} <!-- No longer needs augmentation -->
         </a>
    </div>
@endforeach

Using tags

My previous blogpost also suggested creating a tag() helper that allows you to fetch the content of any tag in Statamic. This has been added to the core as well, which means you no longer need custom code for this, it even works fluently!

@foreach (tag('collection:blog', ['limit' => 3]) as $entry)
@foreach (Statamic::tag('collection:blog')->limit(3) as $entry)
    ...
@endforeach

A note on performance

If you were looking to use Blade templates purely for the improved performance compared to Antlers, this shouldn't be a deciding factor anymore, as 3.3 ships with a completely new Antlers engine that has greatly improved performance and functionality. Do give it a try before deciding which template engine you'd like to use.

References

If you want to look at the PRs that added improvements to the developer UX for Blade support, take a look at these:

You can like or retweet this Tweet
MENU