Search
Site Essentials for Statamic

Site Essentials for Statamic

Resolving Variables

Site Essentials for Statamic attempts to locate data from various locations within your template's context, depending on the precise tag being built. Almost all builder methods can have their values customized.

The simplest way to customize the data made available to the builder methods is to specify a callback function when calling the method. For example, the following would return Stillat when on the homepage, or add a specific entry's title to the beginning of the generated title:

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5Metadata::general()->title(function (array $context) {
6 if ($context['is_homepage']) {
7 return 'Stillat';
8 }
9 
10 $title = $context['title'] ?? null;
11 
12 if (! $title || $title === 'Stillat') {
13 
14 return 'Stillat';
15 }
16 
17 return "{$title} | Stillat";
18});

Callback functions will receive the se_meta tag's context at the point it was evaluated in your template. Some built-in providers define default variables. This is done by providing a string value starting with a $ symbol. For example, the title builder method is implemented like this:

1<?php
2 
3// ...
4 
5public function title($title = '$title'): self
6{
7 $this->getManager()->queue([
8 '_template' => '<title>{{ title }}</title>',
9 'title' => $this->value($title),
10 ]);
11 
12 return $this;
13}

We can tap into the $title variable by using the Metadata facade's resolve method:

1<?php
2 
3use Stillat\StatamicSiteEssentials\Support\Facades\Metadata;
4 
5Metadata::resolve('title', function (array $context) {
6 if ($context['is_homepage']) {
7 return 'Stillat';
8 }
9 
10 $title = $context['title'] ?? null;
11 
12 if (! $title || $title === 'Stillat') {
13 return 'Stillat';
14 }
15 
16 return "{$title} | Stillat";
17});

Using the Metadata::resolve method is much simpler when interacting with metadata builder methods that register multiple meta/link tags at once, such as the withDefaults method.

#Built-in Variable Names

The following table lists the various builder methods that have default variables:

Builder

Method

Variable Name

General

title

$title

General

self

$current_full_url

General

description

$meta_description

General

keywords

$meta_keywords

General

author

$meta_author

General

canonical

$meta_canonical

General

first

$meta_link_first

General

last

$meta_link_last

General

prev

$meta_link_prev

General

next

$meta_link_next

Open Graph

title

$title

Open Graph

url

$current_full_url

Open Graph

description

$meta_description

Open Graph

siteName

$meta_site_name

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.