Skip to content
Twig extension that implements a way to use native PHP functions and filters in Twig template
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
README.md
composer.json
ravenflux.jpg

README.md

Twig PHP Functions / Filters

Latest Stable Version Latest Unstable Version License Total Downloads

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
parameters:
    ravenflux_functions:
        - 'count'
    ravenflux_filters:
        - 'nl2br'

services:
    ravenflux.twig.extension.php_functions:
        class: RavenFlux\Twig\PhpFunctionsExtension
        arguments:
            - '%ravenflux_functions%'
            - '%ravenflux_filters%'
        tags:
            -
                name: twig.extension

or inline without extra parameters:

# 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_funtions('count', users) }}

Filters:

{{ user.name|raven_filter('nl2br') }}
You can’t perform that action at this time.