Get support for mnapoli/TwigModule

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

mnapoli/TwigModule

Twig module for PHP-DI

Pre-configuration for Twig to work with PHP-DI and Puli.

Experimental: this module relies on an work-in-progress feature that is not merged in the master branch of PHP-DI yet.

Installation

To use this module, you need to be using Puli and PHP-DI. To install it, simply require the package with Composer:

composer require mnapoli/twig-module

PHP-DI will automatically detect and activate the module using Puli (note: this relies on an experimental feature that is not merged in the master branch of PHP-DI).

Usage

The Twig_Environment instance is now injectable with PHP-DI.

Templates are loaded through Puli, so you need to use Puli paths to render a template (or extend another one).

Example of a Slim controller:

function (ResponseInterface $response, Twig_Environment $twig) {
    $response->getBody()->write($twig->render('/app/views/home.twig'));

    return $response;
}

Note the /app/views/home.twig Puli path: this expects a file in res/views/home.twig and the res/ directory of your package to be mapped in Puli. Read Puli's getting started guide if you are lost.

Configuration

The following configuration options can be overridden in your PHP-DI config:

  • twig.options: Twig options

    `twig.options` => [
        'strict_variables' => true,
    ],
    
  • twig.extensions: array of Twig extensions

    `twig.extensions` => add([
        get(Twig_Extension_Profiler::class),
        get(MyTwigExtension::class),
    ]),
    

    Don't forget to add items to the array (by using DI\add()) to avoid removing the base extensions registered by this module.

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2