ViewSetup

Helper class to set up Twig for use in the network.

Registering a Twig directory

Twig uses a file loader system for registering templates. You can register your own "templates directory" if you want to use Twig templates in your theme or plugin. To do this you'll need to provide a path to the templates and a namespace.

ViewSetup::getInstance()->registerTwigFolder('myNamespace', __DIR__ . '/src/templates/');

You'll then be able to use your templates through your namespace using a path relative to the provided path when rendering with View:

# To render the file "/src/templates/page.twig"
View::render('@myNamespace/page');

# or
 
# To render the file "/src/templates/some-path/page.twig"
View::render('@myNamespace/some-path/page');

You can also use the namespace to include and extend templates.

{% raw ‰}

<div data-gb-custom-block data-tag="extends" data-0='@myNamespace/page.twig'></div>

<div data-gb-custom-block data-tag="include" data-0='@myNamespace/some-path/page-part.twig'></div>
{% endraw ‰}

Extending Twig

Twig offers multiple ways to be extended, read more about them in their documentation.

ViewSetup extends this way of extending Twig.

Globals

See Twig documentation here

    ViewSetup::getInstance()->addGlobal(string $name, $value);

Filters

See Twig documentation here

    ViewSetup::getInstance()->addFilter(string $name, $callable, array $options);

We instantiate a \Twig\TwigFilter class for you so you need to provide the data for that.

Functions

See Twig documentation here

    ViewSetup::getInstance()->addFunction(string $name, $callable, array $options);

We instantiate a \Twig\TwigFunction class for you so you need to provide the data for that.

Tests

See Twig documentation here

    ViewSetup::getInstance()->addTest(string $name, $callable, array $options);

We instantiate a \Twig\TwigTest class for you so you need to provide the data for that.

Advanced Extension

We also offer support for the more advanced ways of extending Twig.

Tags

See Twig documentation here

    ViewSetup::getInstance()->addTokenParser(Twig\TokenParser\TokenParserInterface $parser);

Extensions

See Twig documentation here

    ViewSetup::getInstance()->addRuntimeLoader(Twig\RuntimeLoader\RuntimeLoaderInterface $loader);

RuntimeLoader

See Twig documentation here

    ViewSetup::getInstance()->addRuntimeLoader(Twig\RuntimeLoader\RuntimeLoaderInterface $loader);

Last updated