For package developersAdvanced topics

Renderers for template developers

If you are developing a template, you might want, just like a package developer to offer objects using the rendering engine, or you might also want to overload default rendering of some packages your template is using.

Templates renderer are a bit special. Unlike package renderers, template renderers are not automatically picked by the "default" renderer. Instead, they need to be activated manually. Why? Because you might want to have many templates in the project, and we don't want a template renderer to interfere with another template renderer.

As a template developer, you will therefore have to register your renderer in the default renderer.

Registering your template

If you are developing your own Mouf template, it is likely you are extending the BaseTemplate class for your template class. The BaseTemplate class comes with 2 properties:

  • defaultRenderer: the default renderer (main renderer used by the rendering engine)
  • templateRendererInstanceName: the container identifier of the instance of the renderer for this template

Basically, in your template instance, you should end up with something looking like this (this is a snapshot from the BootstrapTemplate):

Now, when the toHtml() method of your template is called, you should register your template. To do this, you just need to add one line at the top of your toHtml() method.

public function toHtml(){
    // Let's register the template renderer in the default renderer.
    $this->getDefaultRenderer()->setTemplateRendererInstanceName($this->getTemplateRendererInstanceName());

    // Here goes the rest of your code.
    ... 
}

Writing the template installer

Most of the time, you will want to create a default template instance when your package is installed.

Assuming you are using container-interop/service-provider and thecodingmachine/funky, your service provider will look like this:

class BootstrapTemplateServiceProvider extends ServiceProvider
{
    /**
     * @Factory(aliases={TemplateInterface::class})
     */
    public function createBootstrapTemplate(CanSetTemplateRendererInterface $templateRenderer, ContainerInterface $container, WebLibraryManager $webLibraryManager): BootstrapTemplate
    {
        $bootstrapTemplate = new BootstrapTemplate($templateRenderer, "bootstrapTemplateRenderer");
        // Some init stuff
        // ...
        return $bootstrapTemplate;
    }

    /**
     * @Factory(name="bootstrapTemplateRenderer")
     */
    public function createTemplateRenderer(CacheInterface $cache, ContainerInterface $container, \Twig_Environment $twig): FileBasedRenderer
    {
        return new FileBasedRenderer(__DIR__.'/../../../templates/', $cache, $container, $twig);
    }
}
For package developersAdvanced topics

Found a typo? Something is wrong in this documentation? Just fork and edit it!