Edit this page on Github

Spress >= 2.0

Renderizer is responsible for formatting items. This can be considered as a template engine. TwigRenderizer is the default implementation and is based on Twig.

A renderizer must implement RenderizerInterface. To change the current renderizer implementation see EnvironmentEvent class at spress.start event.

Extending Twig on TwigRenderizer

The spress.start event enable to extend Twig with new functions, tags and tests.

Adds a filter

use Yosymfony\Spress\Core\Plugin\PluginInterface;
use Yosymfony\Spress\Core\Plugin\EventSubscriber;
use Yosymfony\Spress\Core\Plugin\Event\EnvironmentEvent;

class TestPlugin implements PluginInterface
{
    public function getMetas()
    {
        return [
            'name' => 'Test plugin',
        ];
    }

    public function initialize(EventSubscriber $subscriber)
    {
        $subscriber->addEventListener('spress.start', 'onStart');
    }

    public function onStart(EnvironmentEvent $event)
    {
        $renderizer = $event->getRenderizer();
        $renderizer->addTwigFilter('dummy', function($string){
            return $string . ' (is dymmy) ';
        });
    }
}

How to use?

{{ 'The text' | dummy }}

Adds a function

use Yosymfony\Spress\Core\Plugin\PluginInterface;
use Yosymfony\Spress\Core\Plugin\EventSubscriber;
use Yosymfony\Spress\Core\Plugin\Event\EnvironmentEvent;

class TestPlugin implements PluginInterface
{
    public function getMetas()
    {
        return [
            'name' => 'Test plugin',
        ];
    }

    public function initialize(EventSubscriber $subscriber)
    {
        $subscriber->addEventListener('spress.start', 'onStart');
    }

    public function onStart(EnvironmentEvent $event)
    {
        $renderizer = $event->getRenderizer();
        $renderizer->addTwigFunction('superf', function($string){
            return strtoupper($string);
        });
    }
}

How to use?

{{ superf('the text') }}

Adds a test

use Yosymfony\Spress\Core\Plugin\PluginInterface;
use Yosymfony\Spress\Core\Plugin\EventSubscriber;
use Yosymfony\Spress\Core\Plugin\Event\EnvironmentEvent;

class TestPlugin implements PluginInterface
{
    public function getMetas()
    {
        return [
            'name' => 'Test plugin',
        ];
    }

    public function initialize(EventSubscriber $subscriber)
    {
        $subscriber->addEventListener('spress.start', 'onStart');
    }

    public function onStart(EnvironmentEvent $event)
    {
        $renderizer = $event->getRenderizer();
        $renderizer->addTwigTest('red', function($val){
            return $val == 'red';
        });
    }
}

How to use?

{% if my_value is red %}
...
{% endif %}