Download the PHP package zenstruck/twig-service-bundle without Composer
On this page you can find all versions of the php package zenstruck/twig-service-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zenstruck/twig-service-bundle
More information about zenstruck/twig-service-bundle
Files in zenstruck/twig-service-bundle
Package twig-service-bundle
Short Description Make functions, static methods, Symfony service methods available in your twig templates.
License MIT
Homepage https://github.com/zenstruck/twig-service-bundle
Informations about the package twig-service-bundle
zenstruck/twig-service-bundle
Making data from your app's services available in twig templates can be done by either:
- Injecting the service/data into the template when rendering.
- Creating a twig extension that has access to the service/data.
For #1, this isn't always a viable option (ie you need this data in your layout). With #2, there is a bit of boilerplate and if done incorrectly (ie not using a runtime or service proxy for heavy services), it could lead to performance issues.
This bundle provides an easy way to make functions, static methods, service methods, and even full service objects available in your twig templates.
Installation
[!NOTE] If not added automatically by
symfony/flex
, enableZenstruckTwigServiceBundle
.
Usage
[!NOTE] The output for the following functions/filters will be escaped. If your function\filter returns html that you don't want escaped, use the
|raw
filter.
Service Methods as Functions/Filters
You can mark any public method in your configured services with the #[AsTwigFunction]
attribute to make them available within your twig templates with the fn()
twig
function/filter:
In your twig template, use the fn()
function/filter to call:
Dynamic functions/filters are made available. The following is equivalent to above:
User Defined as Functions/Filters
You can mark any of your custom functions with the #[AsTwigFunction]
attribute
to make them available within your twig templates with the fn()
twig function\filter:
In your twig template, use the fn()
function/filter to call:
Dynamic functions/filters are made available. The following is equivalent to above:
3rd-Party Functions/Filters
If you need to make functions, static/service methods available in your twig templates for code you do not control (ie internal PHP functions/3rd party package), you can configure these in the bundle config:
Service Function
Mark any service you'd like to make available in twig templates with the #[AsTwigService]
.
[!NOTE] While you can mark any service as a twig service, it is not recommended to mark services that have nothing to do with templating (ie repositories) as such. You can think of twig services as lightweight-lazy-twig-extension-functions whose purpose is to break up/simplify large custom twig extensions.
You're now ready to access the service in any twig template:
Each service alias is made available as a dynamic function. The following is equivalent to above:
Invokable Service Filters
You can turn any twig service into a twig filter by having it implement __invoke()
:
In your template, use the service
twig filter:
Each service alias is made available as a dynamic filter. The following is equivalent to above:
Parameter Function
You can access any service container parameter with the provided parameter()
twig function:
zenstruck:twig-service:list
Command
Use this command to list all functions/filters/services configured by this bundle and available in your twig templates.
[!NOTE] This command is only available when
debug: true
.
Full Default Bundle Configuration
All versions of twig-service-bundle with dependencies
symfony/framework-bundle Version ^5.4|^6.0|^7.0
symfony/twig-bundle Version ^5.4|^6.0|^7.0