ravenflux/php-functions

Twig extension that implements a way to use native PHP functions and filters in Twig template

1.1.0 2019-04-03 22:39 UTC

This package is auto-updated.

Last update: 2019-12-08 20:40:58 UTC


README

Latest Stable Version Latest Unstable Version License Total Downloads

ravenflux.jpg

Twig extension that implements a way to use native PHP functions and filters in Twig template.

Medium article

Installation

The recommended way to install is via Composer:

composer require ravenflux/php-functions

For Symfony usage add it as a service and tag it:

# config/services.yaml
services:
    ravenflux.twig.extension.php_functions:
        class: RavenFlux\Twig\PhpFunctionsExtension
        arguments:
            - #first argument are functions
                - 'count'
            - #second argument are filters
                - 'nl2br'
        tags:
            -
                name: twig.extension

or without any arguments if you want to use dynamic function / filters:

# config/services.yaml
services:
    ravenflux.twig.extension.php_functions:
        class: RavenFlux\Twig\PhpFunctionsExtension
        tags:
            -
                name: twig.extension

Function VS Filter | source

A function is used when you need to compute things to render the result.
A filter is a way to transform the displayed data.

Usage

Functions:

{{ count(users) }}

Filters:

{{ user.name|nl2br }}

or dynamically any php function:

Functions:

{{ raven_function('count', users) }}

Filters:

{{ user.name|raven_filter('nl2br') }}