Functions

Functions in Twig can be used to generate content. They are called by there name followed by parentheses and within the parentheses they may have one or more arguments.

List of available functions

attribute

Used to access a "dynamic" attribute of a variable. It's useful if the attribute has punctuation in it like a dash as that can cause issues with using the . notation of getting attributes.

{{ attribute(array, key) }}

Learn more about the attribute function

compare_modified_date

Allows you compare the modified on date for the page with the "If-Modified-Since" header in the request to see if a "304 Not Modified" http status code should be returned. If the "If-Modified-Since" date sent from the browser in the request is the same as the "last modified" date of the page then a 304 http status code is automatically set, no content is returned and the response is sent. 

{% do compare_modified_date() %}

Learn more about the compare_modified_date function

cycle

Cycles through an array of values. Useful for alternating values within a loop.

{% for item in items %}
    <p class="{{ cycle(['odd', 'even'], loop.index0) }}">{{ item.itemName }}</p>
{% endfor %}

Learn more about the cycle function

dump

Outputs the contents of a variable for debugging. The data is formatted and styled in a BranchCMS HTML element.   If not variable is passed then all variables in the current context will be outputted.

{# Output one variable #}
{{ debug(variable) }}

{# Output multiple variables #}
{{ debug(variable, anotherVar, yetAnotherVar) }}

{# Output all variables #}
{{ debug() }}

header

Allows you to set an HTTP header for the response. 

{% do header('Pragma', 'cache') %}

Learn more about the header function

http_response_code

Sets an HTTP response status code.

{% do http_response_code(404) %}

Learn more about the http_response_code function

parse_url

Parses a URL and returns either an array of the different URL parts or a single value if a specific URL part is specified.

{% set url = parse_url(item.website) %}

Learn more about the parse_url function

path_info

Parses a file path and returns either an array of the different file path parts or a single value if a specific file path part is specified.

{% set fileParts = path_info(item.image.value) %}

Learn more about the path_info function

random

Returns a random value depending on the supplied parameter type.

{{ random(['bike', 'train', 'car']) }}
{{ random('ZYX') }}
{{ random() }}
{{ random(5) }}

Learn more about the random function

random_letters

Returns a random string of letters for the specified length. The letters could be uppercase and lowercase.

{{ random_letters(10) }}
{# Outputs a random string of letters 10 characters long #}

Learn more about the random_letters function

random_number

Returns a random string of numbers for the specified length.

{{ random_number(4) }}
{# Outputs a random number 4 characters long #}

Learn more about the random_number function

random_string

Returns a random string of letters and/or numbers for the specified length. The letters could be uppercase and lowercase.

{{ random_string(6) }}
{# Outputs a random string of letters and/or numbers 6 characters long #}

Learn more about the random_string function

range

Returns an array containing an arithmetic progression of integers.

{% for i in range(0, 3) %}
{{ i }},
{% endfor %}

Learn more about the range function

redirect

Allows you to redirect to another URL.

{% do redirect('http://www.branchcms.com') %}

Learn more about the redirect function

set_api_attribute_value

Only used when setting values for an API attribute such as

  • Multi-Select Box using data from an App API call
  • Multiple Checkboxes using data from an App API call
  • Select Menu using data from an App API call

{% for post in posts %}
{% set_api_attribute_value(post.id, post.postTitle) %}
{% endfor %}

Learn more about the set_api_attribute_value function