DrupalX

Extension
Framework

Decoding filters

By default, Twig has some filters to escape/encode stuff but for some strange reason, all functions to unescape/decode anything have been omitted. DrupalX fixes this:

json_decode

The json_decode filter is the opposite to Twig's built-in json_encode filter and will parse any JSON string to a corresonding array structure:

{%
set fruits_json='{"apple": "red","kiwi": "green", "banana": "yellow"}';
set fruits=fruits_json | json_decode;
%}

A kiwi is usually {{fruits["kiwi"]}}.

This will output - not a big surprise here:

A kiwi is usually green.

url_decode

The url_decode filter is the opposite to Twig's built-in url_encode filter and will decode any URL-encoded string:

{{ "Space (%20) - the final frontier" | url_decode }}

This will, of course, output:

Space ( ) - the final frontier

unescape

The unescape filter is the opposite to Twig's built-in escape filter and will decode any HTML entities:

{{ 'Use the "<b>" tag in HTML to let some text appear in bold' | unescape }}

The result is:

Use the <b> tag in HTML to let some text appear in bold