Learn how to add a Twig global variable to your Craft plugin or module via a Twig Extension.
In this video, we will learn how to create a Twig Global Variable in Craft CMS. The goal is to provide a variable referencing data that we can use in any Twig template in our project.
For this project, I want to build a variable that will always tell me if someone is a premium CraftQuest member or not. Since I do a lot of permission work in the templates repeatedly, I want to centralize that as a Twig Global Variable.
A Twig global variable is like any other variable in Twig except that it is available in all templates in your project. Common global variables you’ve probably used are currentUser
, currentSite
, or devMode
.
We will build a simple Twig global variable with static data for this video and then create one for the CraftQuest project that gets its data from a service in a module.
twigextensions
<?php
namespace craftquest\twigextensions;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
We import the AbstractExtension
extension class, which we’ll extend in our class definition. We also want to import the GlobalsInterface
from Twig. This will define the [[PHP interface]] we’ll implement in the class. This interface specifies how we must implement our class for globals.
Now to define the class:
namespace craftquest\twigextensions;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
class CraftQuestTwigGlobals extends AbstractExtension implements GlobalsInterface
{
}
If you’re using PhpStorm as the IDE, you will see that the class defintion is underlined in a red squigly line. This is because we haven’t fulfilled the requirements of the interface we’re implementing. This interface, the GlobalsInterface, requires that we implement a method called getGlobals();
Let’s add that now:
namespace craftquest\twigextensions;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
class CraftQuestTwigGlobals extends AbstractExtension implements GlobalsInterface
{
public function getGlobals(): array
{
}
}
Now we can begin to think about the globals that we want to return and the data we want to use to populate those globals.
Remember, a global is something that will be available is every Twig template in our project. So, it needs to be something globally available and not dependent on data the user just inputted, etc.
Let’s just set it to something static right now so we can test.
The getGlobals()
method returns an array so we need to structure our global data as such. Each global variable will be its own item in the array. The key will of the array item will be the variable name when referenced in a Twig template, and the value the value.
Let’s do something simple right now.
namespace craftquest\twigextensions;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
class CraftQuestTwigGlobals extends AbstractExtension implements GlobalsInterface
{
public function getGlobals(): array
{
return [
'courseName' => 'Extending Twig in Craft CMS'
];
}
}
And then we return the array out of the method. This is a very simple example and if we were doing this as part of a real project, we could just return the array directly rather than assigning it to a variable we don’t use.
Before we can do this, we need to load our new Twig extension in the module class file and then rebuild the Composer autload files so it picks up the new class we created.
$ composer dump-autoload
In the module class file – in my example this is the CQControlPanel.php
class file – we already have the basics in place from our earlier work in the course. What we need to do now is add our new Twig extension class file to the array inside the private _registerTwigExtensions
method and then it’ll be loaded in via registerTwigExtension()
as it loops over the array of extensions to register.
The method is called up in the init()
when the module is first loaded.
private function _registerTwigExtensions()
{
$extensions = [
PlantifyExtension::class,
CraftquestTwigExtension::class,
];
foreach ($extensions as $extension) {
Craft::$app->view->registerTwigExtension(new $extension);
}
}
With our basic class ready, let’s Let’s test this by adding the new global variable to the index.twig
template via {{ courseName }}
.
{{ courseName }}
And, it should work!
Of course, this isn’t a very pracitcal use of a global but we have the basics set up and then only we need to do now is decide what kind of variables we need to populate. Continue watching the rest of the video as I create a Twig global variable for the CraftQuest site and populate it with data from a module service.
Extending Twig in Craft CMS is made up of the following videos: