Download the PHP package susina/twig-extensions without Composer
On this page you can find all versions of the php package susina/twig-extensions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download susina/twig-extensions
More information about susina/twig-extensions
Files in susina/twig-extensions
Package twig-extensions
Short Description Twig extension with useful functions, filters and tests
License Apache-2.0
Informations about the package twig-extensions
Susina Twig Extensions
Susina Twig Extensions is a set of extensions for Twig template engine. It contains some useful functions, tests and filters missing in the original library.
Installation
Firstly, install the package via Composer:
Then add the extension you want to Twig Engine. Suppose you want to load VariablesExtension:
Symfony Framework
If you are working with Symfony, after installing the library with composer, register the extensions you want as services
and tag them as twig.extension:
VariablesExtension
VariablesExtension contains some tests and functions useful for manipulating variables.
Tests
When you register this extension you can use the following type tests:
arrayto test if a variable is an array; Twig also hasIterabletest but it returns true also for iterable objects.booleanto test if a variable is boolean.floatanddoubleto test if a variable is a floating point number.integerto test if a variable is integer.objectto test if a variable is an object.scalarto test if a variable is a scalar (see https://www.php.net/manual/en/function.is-scalar.php).stringto test if a variable is a string.instanceOf(class_name)to test if an object is an instance of _classname class
and you can use them in your templates:
Functions
get_type
get_type function returns the variable type:
var_export
var_export function is a wrapper for PHP var_export and it
behaves in the same way. It can be useful if you want to generate some valid php code from a variable.
Filters
bool_to_string
bool_to_string filter returns the string 'true' if the variable filtered can be evaluated as true, otherwise
it returns the string false:
it returns The "boolVariable" is true.
You can customize the true/false strings by passing two variables to the filter: the first one represents the true value, the second one the false value, i.e.:
it returns The "boolVariable" is yes.
StringExtension
Filters
quote
You can apply quote filter to a string, if you want to surround it with quotes:
it returns the quoted string 'Donald Duck'.
By default, the filter applies single quotes ' but you can pass any character you want, as the argument of the filter:
then it returns "Donald Duck".
to_kb
to_kb filter transform a number from bytes to kilobytes:
it returns: The file size is 2 Kb.
By default, this filter uses the English decimal and thousands separator: . for decimal and , form thousands.
You can change this behavior by passing different separators:
it returns:
to_mb
to_mb filter transform a number from bytes to megabytes. The behavior is the same as to_kb.
Gravatar Extension
Gravatar extension contain a filter to retrieve the Gravatar image from a given email.
gravatar filter returns the uri for the avatar so that you can easily use it in your html:
You can also pass some options to the filter, i.e.:
For a full options description, please see https://en.gravatar.com/site/implement/images/.
Issues
We manage issues and feature requests via Github repository issues.
Contributing
Feel free to fork and submit pull requests: all contributions are welcome!
This library includes some useful composer scripts for developers:
composer testto run the test suitecomposer analyticsto run Psalm static analysis toolcomposer cs:fixto fix coding standardcomposer cs:checkto check the coding standard (see https://github.com/susina/coding-standard for details)composer coverage:htmlto generate code coverage report in html format (into/coveragedirectory)composer coverage:cloverto generate code coverage report in xml formatcomposer checkruns the first three commands
Before submitting a pull request, please run composer check and fix all errors.
License
This library is released under Apache 2.0 license.