A collection of Twig extensions for other fhooe packages used in the Media Technology and Design program at the University of Applied Sciences Upper Austria. It is intended to be used with packages such as fhooe/router or fhooe/router-skeleton respectively.
Integrate the package into your project using Composer:
composer require fhooe/twig-extensions
This package contains the following extensions:
An extension to access the fhooe/router package from within Twig templates. It provides the following functions:
url_for("/some/route")
: Returns the full URL for the given route.get_base_path()
: Returns the base path if the application is not in the server's document root.
Provides the function session("someKey")
, which returns the value of the given key in the session. That way, not every needed entry in $_SESSION
(or even the whole superglobal) must be passed to the template.
Register the extensions with your Twig environment, then use the provided functions in your templates:
Register the extension and provide an instance of Fhooe\Router\Router
.
$twig->addExtension(new Fhooe\Twig\RouterExtension(new Fhooe\Router\Router()));
Use the functions:
{{ url_for("/some/route") }}
{{ get_base_path() }}
This will output the full path for the route or a base path (if you need to prefix) static paths for stylesheets or other files.
Register the extension:
$twig->addExtension(new Fhooe\Twig\SessionExtension());
Use the function:
{{ session("someKey") }}
This will output the value of $_SESSION["someKey"]
. If nothing is stored under that key, sessions are inactive, or the session superglobal is unavailable, an empty string will be returned so that you can use the function safely.
If you'd like to contribute, please refer to CONTRIBUTING for details.
fhooe/twig-extensions is licensed under the MIT license. See LICENSE for more information.