Download the PHP package mediagone/twig-powerpack without Composer
On this page you can find all versions of the php package mediagone/twig-powerpack. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package twig-powerpack
Twig PowerPack
⚠️ This project is in experimental phase.
This package provides code-quality features to your Twig templates:
- Type-safety checks for template context variables.
- Register global data/resources from any template.
- Instantiate classes in templates.
Along with new functionalities:
|json_decode
filter
Installation
This package requires PHP 7.4+ and Twig 2+.
Add it as Composer dependency:
If you're using Symfony, enable the extension in services.yaml
:
Introduction
Twig templating engine is seriously lacking types....
Features
1) Context Variables type-checking
Templates usually require specific external data, but there is no native way to check the type of supplied variables. The expect
tag allows you to declare required variables in your Twig files, making them also self-documenting. If the data is invalid, an exception will be thrown.
Primitive types
Supported scalar types are: bool, float, int and string.
Note: TITLE, ENABLED, AMOUNT and COUNT represent the names of required variables.
Objects
Because they don't guarantee any data structure, anonymous objects (stdClass) are not supported. However, usage of named classes is strongly encouraged to expose data in your templates. Therefore, a Fully Qualified Class Name (FQCN) can also be supplied:
Nullable
Sometimes, you may want to ensure that a variable is defined while making it optional by using the nullable
keyword:
Arrays
You can also check if a variable is an array of a given type by using the array of
keywords:
Arrays can also be nullable:
Or contain nullable elements:
And even nullable array of nullable elements!
Note: Checking array's items type might induce a slight overhead, but unless you have thousands of elements it should be negligible.
2) Register global data from any template
You may occasionally declare specific data in your templates, used in the global scope. For example if your templates dynamically add CSS classes to HTML body, or if they require optional CSS or JavaScript resources you only want to include on demand.
String Data
Short string data can be registered from anywhere in your templates using the {% register <data> in <registry> %}
tag:
And retrieved elsewhere through the registry()
function:
Optional registry clause
For convenience, the registry name can be automatically inferred from the data when it represents a path with an extension, making usage of in <registry>
optional. The following lines are equivalent:
Body Data
Because you may need longer or dynamically generated data, the tag also supports a block syntax to allow a content body to be provided. In this case you cannot define data in the opening tag and the registry clause is mandatory:
{% register in <registry> %} <body data> {% endregister %}
For example if you want to declare inline scripts from a template:
And include it at the end of the html page:
Unicity
Data can be declared as unique, so if multiple templates register the same value, it will be included only once. It's required most of the time, just add the once
keyword to the tag:
It also works with body data:
However, unicity is only enforced within the same registry, so both following statements will be taken into account:
Priority
As you cannot always predict in which order data will be registered, you'll sometime need to ensure a data comes first, for example in the case of a script library required by others. Then, add the
priority
keyword at the end of your tag followed by a priority number (lower values come first*).
Tags without priority always come after prioritized ones.
Note: the order of data with the same priority (or undefined) is not guaranteed.
3) Instantiate classes in templates
Although it's better to do it in the controller when possible, you may need to create class instances directly in a template. The new(string $fqcn, ...$args)
function allows you to call the constructor of a given class:
Given the following View Model class:
License
Twig PowerPack is licensed under MIT license. See LICENSE file.