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) }}
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() %}
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 %}
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') %}
http_response_code
Sets an HTTP response status code.
{% do http_response_code(404) %}
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) %}
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) %}
random
Returns a random value depending on the supplied parameter type.
{{ random(['bike', 'train', 'car']) }}
{{ random('ZYX') }}
{{ random() }}
{{ random(5) }}
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 #}
random_number
Returns a random string of numbers for the specified length.
{{ random_number(4) }}
{# Outputs a random number 4 characters long #}
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 #}
range
Returns an array containing an arithmetic progression of integers.
{% for i in range(0, 3) %}
{{ i }},
{% endfor %}
redirect
Allows you to redirect to another URL.
{% do redirect('http://www.branchcms.com') %}
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 %}