Statistics
909
2
0
56d
Badges
Tags
latte
Dependencies

Template Factory

Build Status Coverage Status Downloads this Month Latest stable

Installation

Via Composer:

$ composer require nepada/template-factory

Register the extension in config.neon:

extensions:
    templateFactory: Nepada\Bridges\TemplateFactoryDI\TemplateFactoryExtension

Usage

Translator autowiring

Who would want to call setTranslator() manually on every template? With this template factory all you need is to define ITranslator service in your configuration and it gets automatically injected into created templates.

Custom Latte filters

Do you need custom Latte filters in templates? Their definition is pretty straightforward:

templateFactory:
    filters:
        doStuff: [@someService, doStuff]

Template parameters

This is the ultimate answer to the question "How do I get parameter / service from DI container into template?"

templateFactory:
    parameters:
        foo: bar
        service: @anotherService
        containerParam: %param%

Template providers

Latte 2.4 has improved support for global/system parameters in templates - they are called "providers". You can set them similarly to parameters:

templateFactory:
    providers:
        foo: bar
        service: @anotherService
        containerParam: %param%

Advanced template configuration

If you need to do something a bit more complex with every created template, make use of onCreateTemplate event of TemplateFactory:

services:
    templateFactory.templateFactory:
        setup:
            - '?->onCreateTemplate[] = ?'(@self, [@customConfigurator, callback])

Configuration from another CompilerExtension

Some extensions may need to install a Latte filter, or inject a parameter / service into template. This can be done in beforeCompile() phase by customizing setup of TemplateConfigurator.

$templateConfigurator = $containerBuilder->getByType(Nepada\TemplateFactory\TemplateConfigurator::class);
$containerBuilder->getDefinition($templateConfigurator)
    ->addSetup('addFilter', ['filterName', $callback])
    ->addSetup('addProvider', ['provider', $value])
    ->addSetup('addParameter', ['parameter', $value])
    ->addSetup('addParameter', ['parameter', '@someService']);
  • v1.2.0 1.2.0

    • Requires PHP 7.1.
    • Uses declare(strict_types = 1).
    • Uses scalar and return type hints.
    • Compatible with PHP 7.2.
  • v1.1.2 1.1.2

    • Code style improvements.
  • v1.0.2 1.0.2

    • Code style improvements.
  • v1.0.0 1.0.0

    • Initial release.
  • v1.0.1

    • Bugfixes.
  • v1.1.1

    • Bugfixes.
  • v1.1.0 v1.1.0

    • Requires Nette 2.4.
    • Added support for providers from Latte 2.4.

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com