Download the PHP package christophersmith262/twig_override without Composer
On this page you can find all versions of the php package christophersmith262/twig_override. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download christophersmith262/twig_override
More information about christophersmith262/twig_override
Files in christophersmith262/twig_override
Package twig_override
Short Description Allows twig templates and arguments to be overridden at run-time.
License MIT
Informations about the package twig_override
TwigOverride
TwigOverride provides a twig extension that can help with:
- Preprocessing the arguments passed to a twig template anywhere that template is referenced.
- Dynamically rewriting
include
/embed
/extends
to reference a different template based on run-time context.
Installation
TwigOverride supports twig 1.x and twig 2.x.
Performing Simple Template Overrides
A basic override provider TwigOverride\Providers\SimpleRewriteProvider
is
included to perform template file swapping. For example, if I wanted to replace
every occurrence of:
with
By Manually Adding the Extension
If you are manully creating the twig environment, you can call the
addExtension
method to directly add the plugin:
Using Twig as a Symfony Service
If you are using the standard symfony services.yaml, then you can simply add the extension as a service:
Advanced Rewrites with a Custom Provider
In the example below we'll create a provider that dynamically resolves a custom 'profile' template for a user based on the user id, and decorates the arguments passed to any template that includes a 'user_id' with a 'user_name' argument.
To do this we'll first creat a provider class that will need access to the twig loader:
Assuming we are using a twig in a symfony container environment, we can expose the provider in the services.yaml. Note that in this example the provider needs the twig loader service as an argument.