Cross-framework module for Twig
This package integrates Twig in any container-interop/service-provider compatible framework/container.
composer require thecodingmachine/twig-universal-module
Once installed, you need to register the
TheCodingMachine\TwigServiceProvider into your container.
If your container supports thecodingmachine/discovery integration, you have nothing to do. Otherwise, refer to your framework or container's documentation to learn how to register service providers.
This service provider is meant to create a base
Out of the box, the instance should be usable with sensible defaults. We tried to keep the defaults usable for most of the developer, while still providing caching for good performances. If you are looking for the best performances, you will need to tweak the settings.
$twig = $container->get('Twig_Environement'); echo $twig->render('views/my.twig', [ 'foo' => 'bar' ]);
- Caching is enabled, in a directory under the temporary system directory. In production, if you are running a multi-user environment, you might want to change that to a directory only readable by you.
autoreload = true: You can safely modify any Twig file without needing to purge the cache. In production, if you are looking for best performance, put this to
- Twig files will be loaded from the root of your project (the directory where the
composer.jsonfile is). You can change that by overloading the
Twig_LoaderInterfaceentry or the
- By default,
debug = trueunless your provide a value in the
DEBUGentry of your container.
Important: when this service provider looks for a service, it will first look for the service prefixed with the package name, then for the service directly.
So if this documentation states that the
DEBUG entry is used, the service provider will first look into
thecodingmachine.twig-universal-module.DEBUG and then into
This allows you to keep your container clean (with only one
DEBUG entry), and in case there are several service providers using that
DEBUG entry and you want to pass different values, you can still edit
thecodingmachine.twig-universal-module.DEBUG for this service provider only.
This service provider expects the following configuration / services to be available:
||no||The debug mode of Twig|
This service provider provides the following services:
||The Twig_Environment instance|
||An alias to the loader chain (by default, the Twig_Environment is using a loader chain)|
||Instance of the loader chain.|
||An array containing options passed to Twig (see default values in previous chapter).|
||An array of loaders. Contains by default a single instance of the
||The default instance of the
||The base directory storing the Twig files. Defaults to root directory of the project. Used by
||Default directory that stores Twig compiled templates.|
This service provider does not extend any service.