Conversion Filters

Last updated on
31 January 2024

Below are the additional conversion filters Twig Tools provides.

boolean

Returns the boolean value of the passed value or variable. Internally it uses PHP's boolval function.

Example:

{{ 0|boolean }} {# returns false #}

{{ 42|boolean }} {# returns true #}

{{ 0.0|boolean }} {# returns false #}

{{ 4.2|boolean }} {# returns true #}

{{ ''|boolean }} {# returns false #}

{{ 'string'|boolean }} {# returns true #}

{{ 'true'|boolean }} {# returns true #}

{{ 'false'|boolean }} {# returns true #}

{{ [0, 0]|boolean }} {# returns true #}

{{ [0]|boolean }} {# returns true #}

{{ []|boolean }} {# returns false #}

{{ null|boolean }} {# returns false #}

{{ false|boolean }} {# returns false #}

{{ true|boolean }} {# returns true #}

{% set is_true = '0' %}
{{ is_true|boolean }} {# returns false #}

integer

Returns the integer value of the passed value or variable. Internally it uses PHP's intval function.

Example:

{{ 42|integer }} {# returns 42 #}

{{ 4.2|integer }} {# returns 4 #}

{{ '42'|integer }} {# returns 42 #}

{{ '+42'|integer }} {# returns 42 #}

{{ '-42'|integer }} {# returns -42 #}

{{ 042|integer }} {# returns 34 #}

{{ '042'|integer }} {# returns 42 #}

{{ 42000000|integer }} {# returns 42000000 #}

{{ []|integer }} {# returns 0 #}

{{ [0, 0]|integer }} {# returns 1 #}

{{ false|integer }} {# returns 0 #}

{{ true|integer }} {# returns 1 #}

{{ null|integer }} {# returns 0 #}

{{ 0|integer }} {# returns 0 #}

{{ 1|integer }} {# returns 1 #}

{{ 0.0|integer }} {# returns 0 #}

{{ 1.0|integer }} {# returns 1 #}

{% set int_is_what = '0' %}
{{ int_is_what|integer }} {# returns 0 #}

float

Returns the float value of the passed value or variable. Internally it uses PHP's floatval function.

Example:

{{ 42|float }} {# returns 42 #}

{{ 4.2|float }} {# returns 4.2 #}

{{ 0.42|float }} {# returns 0.42 #}

{{ 42000000.00|float }} {# returns 42000000 #}

{{ 42.0000000|float }} {# returns 42 #}

{{ -42.0000000|float }} {# returns -42 #}

{{ +42.0000000|float }} {# returns 42 #}

{{ 42.00000001|float }} {# returns 42.00000001 #}

{{ 0000042.00000001|float }} {# returns 42.00000001 #}

{{ '42.00000001The'|float }} {# returns 42.00000001 #}

{{ The42.00000001'|float }} {# returns 0 #}

{{ '42'|float }} {# returns 42 #}

{{ '+42'|float }} {# returns 42 #}

{{ '-42'|float }} {# returns -42 #}

{{ 042|float }} {# returns 42 #}

{{ '042'|float }} {# returns 42 #}

{{ 42000000|float }} {# returns 42000000 #}

{{ []|float }} {# returns 0 #}

{{ ['foo', 'bar']|float }} {# returns 1 #}

{{ false|float }} {# returns 0 #}

{{ true|float }} {# returns 1 #}

{{ null|float }} {# returns 0 #}

{{ 0|float }} {# returns 0 #}

{{ 1|float }} {# returns 1 #}

{{ '0'|float }} {# returns 0 #}

{{ '1'|float }} {# returns 1 #}

{% set float_is_what = '4.2' %}
{{ float_is_what|float }} {# returns 4.2 #}

string

Returns the string value of the passed value or variable. Internally it uses PHP's strval function.

Example:

{{ 42|string }} {# returns '42' #}

{{ 4.2|string }} {# returns '4.2' #}

{{ '42'|string }} {# returns '42' #}

{{ '+42'|string }} {# returns '+42' #}

{{ '-42'|string }} {# returns '-42' #}

{{ 042|string }} {# returns '34' #}

{{ '042'|string }} {# returns '042' #}

{{ 42000000|string }} {# returns '42000000' #}

{{ false|string }} {# returns '' #}

{{ true|string }} {# returns '1' #}

{{ null|string }} {# returns '' #}

{{ 0|string }} {# returns '0' #}

{{ 1|string }} {# returns '1' #}

{{ 0.0|string }} {# returns '0' #}

{{ 1.0|string }} {# returns '1' #}

{% set string_is_what = 0 %}
{{ string_is_what|string }} {# returns '0' #}

md5

Returns the MD5 hash value of a passed string value or variable. Internally it uses PHP's md5 function. Note: Because the md5 function requires a string to be passed, the filter will attempt to cast values to a string first.

Example:

{{ '42'|md5 }} {# returns 'a1d0c6e83f027327d8461063f4ac58a6' #}

{{ '42'|md5 }} {# returns 'a1d0c6e83f027327d8461063f4ac58a6' #}

{{ '4.2'|md5 }} {# returns '8653d5c7898950016e5d019df6815626' #}

{{ '+42'|md5 }} {# returns 'deda8ddbf790f3682d5cf69d237bb0b2' #}

{{ '-42'|md5 }} {# returns '8dfcb89fd8620e3e7fb6a03a53f307dc' #}

{{ 'Test'|md5 }} {# returns '0cbc6611f5540bd0809a388dc95a615b' #}

{{ 0|md5 }} {# returns 'cfcd208495d565ef66e7dff9f98764da' #}

{{ '0'|md5 }} {# returns 'cfcd208495d565ef66e7dff9f98764da' #}

{{ '1'|md5 }} {# returns 'c4ca4238a0b923820dcc509a6f75849b' #}

{% set md5_is_what = '0' %}
{{ md5_is_what|md5 }} {# returns 'cfcd208495d565ef66e7dff9f98764da' #}

json_decode

Decodes a JSON string into an object or array. Internally it uses PHP's json_decode function. By default the filter will return an object, but you can optionally pass in a boolean value of true to instead return an associative array.

Example:

{% set json = '{
  "a": 1,
  "b": 2,
  "c": 3,
  "d": 4,
  "e": 5
}'|json_decode(true) %}

{# returns: 
array(5) {
  ['a'] => int(1),
  ['b'] => int(2),
  ['c'] => int(3),
  ['d'] => int(4),
  ['e'] => int(5),
}
#}

{% set json = '{
  "aliceblue": "#f0f8ff",
  "antiquewhite": "#faebd7",
  "aqua": "#00ffff",
  "aquamarine": "#7fffd4",
  "azure": "#f0ffff",
  "beige": "#f5f5dc",
  "bisque": "#ffe4c4",
  "black": "#000000",
  "blanchedalmond": "#ffebcd",
  "blue": "#0000ff",
  "blueviolet": "#8a2be2",
  "brown": "#a52a2a"
}'|json_decode %}

{# returns: 
object(stdClass) (12) { 
  ["aliceblue"]=> string(7) "#f0f8ff" 
  ["antiquewhite"]=> string(7) "#faebd7" 
  ["aqua"]=> string(7) "#00ffff" 
  ["aquamarine"]=> string(7) "#7fffd4" 
  ["azure"]=> string(7) "#f0ffff" 
  ["beige"]=> string(7) "#f5f5dc" 
  ["bisque"]=> string(7) "#ffe4c4" 
  ["black"]=> string(7) "#000000" 
  ["blanchedalmond"]=> string(7) "#ffebcd" 
  ["blue"]=> string(7) "#0000ff" 
  ["blueviolet"]=> string(7) "#8a2be2" 
  ["brown"]=> string(7) "#a52a2a" 
}
#}

{% set json = '{
  "string": "string",
  "boolean_true": true,
  "boolean_false": false,
  "integer": 42,
  "float": 4.2
}'|json_decode %}

{# returns: 
object(stdClass)(5) { 
  ["string"]=> string(6) "string" 
  ["boolean_true"]=> bool(true) 
  ["boolean_false"]=> bool(false) 
  ["integer"]=> int(42) 
  ["float"]=> float(4.2)
}
#}

date_from_format

Converts a datetime string between different date formats. Internally it uses PHP's DateTime::createFromFormat and DateTime::format. You can optionally include a from and/or to timezone identifier to convert the date between different timezones.

{{ a_date_string|date_from_format(from_date, to_date, from_timezone, to_timezone) }}

Note: The date format syntax for DateTime::createFromFormat and DateTime::format and mostly the same, but aren't identical

If you need to include literal characters in your date format(s), you must double escape them.

Example: 'Y-m-d\\TH:i:s' not 'Y-m-d\TH:i:s'

Example:

{{ '2009-Feb-15'|date_from_format('Y-M-j', 'm/d/y') }}
{# returns '02/15/09' #}

{{ '2010-04-23 00:00:00'|date_from_format('Y-m-d H:i:s', 'c', 'America/New_York', 'UTC') }}
{# returns '2010-04-23T04:00:00+00:00' #}

{{ '1 1 19'|date_from_format('!j n y', 'Y-m-j\\TH:i:s', 'America/New_York', 'America/Los_Angeles') }}
{# returns '2018-12-31T21:00:00' #}

{{ '2004-12-19 10:19:42'|date_from_format('Y-m-d H:i:s', 'Y-m-d H:i:s e', NULL, 'America/Managua') }}
{# returns '2004-12-18 17:19:42 America/Managua' #}

{{ '2004-11-19 10:25:33'|date_from_format('Y-m-d h:i:s', 'U', 'America/Managua') }}
{# returns '1100881533' #}

base64_encode

Returns the base64 encoded value of the passed value or variable. Internally it uses PHP's base64_encode function.

Example:

{{ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dapibus.'|base64_encode }} {# returns 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTW9yYmkgZGFwaWJ1cy4=' #}

{{ {"array":[1,2,3],"boolean":true,"color":"gold","null":null,"number":123,"object":{"a":"b","c":"d"},"string":"Hello World"}|base64_encode }} {# returns 'eyJhcnJheSI6WzEsMiwzXSwiYm9vbGVhbiI6dHJ1ZSwiY29sb3IiOiJnb2xkIiwibnVsbCI6bnVsbCwibnVtYmVyIjoxMjMsIm9iamVjdCI6eyJhIjoiYiIsImMiOiJkIn0sInN0cmluZyI6IkhlbGxvIFdvcmxkIn0=' #}

base64_decode

Returns the base64 decoded value of the passed value or variable. Internally it uses PHP's base64_decode function. You can optionally pass in a boolean value of true to pass the strict parameter to the function.

Example:

{{ 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gTW9yYmkgZGFwaWJ1cy4='|base64_decode }} {# returns 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi dapibus.' #}

{{ 'eyJhcnJheSI6WzEsMiwzXSwiYm9vbGVhbiI6dHJ1ZSwiY29sb3IiOiJnb2xkIiwibnVsbCI6bnVsbCwibnVtYmVyIjoxMjMsIm9iamVjdCI6eyJhIjoiYiIsImMiOiJkIn0sInN0cmluZyI6IkhlbGxvIFdvcmxkIn0='|base64_decode|raw }} {# returns {"array":[1,2,3],"boolean":true,"color":"gold","null":null,"number":123,"object":{"a":"b","c":"d"},"string":"Hello World"} #}

{{ 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='|base64_decode }} {# returns 'This is an encoded string' #}

{{ '&VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='|base64_decode }} {# returns 'This is an encoded string' #}

{{ '&VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='|base64_decode(true) }} {# returns false #}

Help improve this page

Page status: No known problems

You can: