Template Resolver for Statamic

Template Resolver for Statamic

Installation and Overview

Template Resolver for Statamic is a simple utility addon, intended to be used by other addons. It provides a simple utility for selecting and rendering a template based on an entry's blueprint and collection, with support for a fallback default template.

Example use-cases:

  • Selecting templates dynamically to generate social media images from HTML,

  • Generating HTML documents on-the-fly, without requiring network requests to the site,

  • Anything where you need to support customizable templates based on an entry's blueprint/collection details

#Installation

Template Resolver for Statamic may be installed into a Statamic site by running the following command from the root of the project:

1composer require stillat/statamic-template-resolver

#How to Use

You need to create an instance of StringTemplateManager, and supply the directory to search for templates in.

1<?php
2 
3use Stillat\StatamicTemplateResolver\StringTemplateManager;
4 
5$manager = new StringTemplateManager(
6 resource_path('views/social_media_images')
7);

Once you have a StringTemplateManager instance, you can check if a template exists for a given collection/blueprint combination:

1<?php
2 
3// ...
4 
5if ($manager->hasTemplate($collection, $blueprint)) {
6 // The template exists.
7}

The hasTemplate method will return true if a specific template or the default template exists. To create a default template, create a file named _default.antlers.html or _default.blade.php at the root of the template folder.

In our example, the default template would need to be placed here:

1views/social_media_images/_default.antlers.html

Specific collection/blueprint templates are stored within a nested directory structure using the following format:

1<template_directory><collection_handle>/<blueprint_handle>.<extension>

For example, if we had a blog collection, with a post blueprint, we could create a specific template at the following location:

1views/social_media_images/blog/post.antlers.html

This library supports the following extensions:

  • .antlers.html: Renders the template using Statamic's Antlers templating engine

  • .blade.php: Renders the template using Laravel's Blade templating engine

To render a template with data, we may use the render method:

1<?php
2 
3// ...
4 
5$results = $manager->render(
6 'colllection_handle',
7 'blueprint_handle',
8 $data
9);

The render method will return null if a template could not be found; $data is provided as an array, and is required.

We may also optionally modify the template before rendering it by supplying an optional callable as the fourth argument:

1<?php
2 
3// Modify the template before its rendered.
4$results = $manager->render(
5 'collection_handle',
6 'blueprint_handle',
7 $data,
8 function ($template, $data) {
9 return mb_strtoupper($template);
10 }
11);

The modifying callable will receive the unmodified template contents as its first argument, and the original data array as the second.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.