Edit this page on Github

List of Spress events. Even's arguments are located at Yosymfony\Spress\Core\Plugin\Event namespace. All events inherits from Symfony\Component\EventDispatcher\Event.

Event name Argument Description
spress.start EnvironmentEvent The spress.start is thrown when start to generate a project. With this event you can: When this event is thrown, the site configuration was loaded.
spress.before_convert ContentEvent

The spress.before_convert is thrown before convert the content of each page.

getContent() method returns the original content in source format.

spress.after_convert ContentEvent

The spress.after_convert is thrown after convert the content of each page.

getContent() method returns the content transformed by converter. In this step renderizer tags, like Twig tags, are not resolved.

spress.before_render_blocks Spress >= 2.0.0 RenderEvent

The spress.before_render_blocks is thrown before render content without layouts.

spress.after_render_blocks Spress >= 2.0.0 RenderEvent

The spress.after_render_blocks is thrown after render content without layouts.

getContent() method returns the content renderized without layouts applied.

spress.before_render_page Spress >= 2.0.0 RenderEvent

The spress.before_render_page is thrown before render with layouts.

spress.after_render_page Spress >= 2.0.0 RenderEvent

The spress.after_render_page is thrown after render content with layouts.

getContent() method returns the content renderized with layouts applied.

spress.finish FinishEvent The spress.finish is thrown when the site was generated. All files are saved in builder folder.

EnvironmentEvent

This class lets you:

  • modify the configuration values.
  • add data sources.
  • change the data writer.
  • add a converters.
  • change the renderizer.
  • get an access to IO API.

Modifying configuration values

If you want to alter site's configuration you need to get the configuration values using getConfigValues method (returns an array). The method setConfigValues lets you save your changes:

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)
    {
        // Configuration values at config.yml file:
        $configValues = $event->getConfigValues();
        $configValues['url'] = 'http://your-domain.local:4000';

        $event->setConfigValues($configValues);
    }
}

Managing data sources

Each site would have one or more data sources. Data sources can load data (items, layouts and includes) from certain locations like filesystem or database. Additionally data sources lets you create dynamic content using an special data source called MemoryDataSource.

The below example show you how to add a new data source. The first argument of addDataSource method is the name for the new data source:

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)
    {
         $dsm = $event->getDataSourceManager();
         $dsm->addDataSource('my-custom-data-source', new MyDataSource());
    }
}

DataSourceManager has methods to add, edit and delete data sources. More details about how to manage data sources.

Changing the data writer

The data writer is responsible for provinding the persistence layer to items. By defatult Spress uses a filesystem data writer implementation but is easy to create a custom data writer for persisting items in a data base for example.

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)
    {
        $event->setDataWriter(new MyDataWriter());
    }
}

To get the current instance of the data writer invokes the method $event->getDataWriter().

More details about data writer.

Adds a new converter

Converter can extend Spress to support a new markup language.

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)
    {
         $event->getConverterManager()->addConverter(new MyConverter());
    }
}

More details about how to create a Converter.

Managing generators

Each site would have one or more generators. Generators are used for generating new items of content.

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)
    {
         $generatorManager = $event->getGeneratorManager();
         $generatorManager->addGenerator('myGenerator', new MyGenerator());
    }
}

More details about how to create a generators.

Changing the renderizer

A renderizer can be considered as a template engine.

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)
    {
        $myRenderizer = new MyRenderizer();
        $event->setRenderizer($myRenderizer);
    }
}

More details about renderizer.

ContentEvent

This is a event base for content-related events. Generators can be used to create a tag or category index page dynamically.

$subscriber->addEventListener('spress.before_convert', 
    function(ContentEvent $event)
    {
        // Gets the identifier of the item (string):
        $v = $event->getId();

        // Gets the content without Front-matter (string):
        $v = $event->getContent();

        // Sets the content of the item:
        $event->setContent('New content');

        // Gets the attributes of the item (array):
        $v = $event->getAttributes();

        // Sets the attributes of the item (array):
        $event->setAttributes([`author_name` => 'Victor']);
    });

RenderEvent

This event extends from ContentEvent.

$subscriber->addEventListener('spress.before_render_blocks', 
    function(RenderEvent $event)
    {
        // Gets the relative URL (string):
        $url = $event->getRelativeUrl();

        // Changes the URL (string):
        $event->setRelativeUrl('/about/me/index.html');
    });

FinishEvent

Information about the site performed.

$subscriber->addEventListener('spress.finish', 
    function(FinishEvent $event)
    {
        // Gets the items (array)
        $items = $event->getItems();

        // (array)
        $siteAttributes = $event->getSiteAttributes();
    });