Functions
On top of the template functions that Twig comes with (opens new window), Craft provides a few of its own:
# ceil( num )
Rounds a number up.
{{ ceil(42.1) }} => 43
# floor( num )
Rounds a number down.
{{ floor(42.9) }} => 42
# getCsrfInput()
Returns a hidden CSRF Token input. All sites that have CSRF Protection (opens new window) enabled must include this in each form that submits via POST.
<form method="post">
{{ getCsrfInput() }}
<!-- ... -->
</form>
# getFootHtml()
Outputs all of the HTML nodes that have been queued up by the includeJsFile and includeJs tags. It should be placed right before your </body>
tag.
<body>
<h1>{{ page.name }}</h1>
{{ page.body }}
{{ getFootHtml() }}
</body>
# getHeadHtml()
Outputs all of the HTML nodes that have been queued up by the includeCssFile, includeCss, and includeHiResCss tags.
<head>
<title>{{ siteName }}</title>
{{ getHeadHtml() }}
</head>
# max( num1, num2, ... )
Returns the largest number of a given set.
{{ max(1, 2, 3) }} => 3
# min( num1, num2, ... )
Returns the smallest number of a given set.
{{ min(1, 2, 3) }} => 1
# round( num )
Rounds off a number to the closest integer.
{{ round(42.1) }} => 42
{{ round(42.9) }} => 43
# shuffle( array )
Randomizes the order of the elements within an array.
{% set promos = shuffle(homepage.promos) %}
{% for promo in promos %}
<div class="promo {{ promo.slug }}">
<h3>{{ promo.title }}</h3>
<p>{{ promo.description }}</p>
<a class="cta" href="{{ promo.ctaUrl }}">{{ promo.ctaLabel }}</a>
</div>
{% endfor %}
# url( path, params, protocol, mustShowScriptName )
Returns a URL to a page on your site.
<a href="{{ url('company/contact') }}">Contact Us</a>
# Arguments
The url()
function has the following arguments:
path
– The path that the resulting URL should point to on your site. It will be appended to your Site URL (opens new window).params
– Any query string parameters that should be appended to the URL. This can be either a string (e.g.'foo=1&bar=2'
) or an object (e.g.{foo:'1', bar:'2'}
).protocol
– Which protocol the URL should use ('http'
or'https'
). The default value depends on whether the current request is served over SSL or not. If not, then the protocol in your Site URL will be used; if so, thenhttps
will be used.mustShowScriptName
– If this is set totrue
, then the URL returned will includeindex.php
, disregarding the omitScriptNameInUrls config setting. (This can be useful if the URL will be used by POST requests over Ajax, where the URL will not be shown in the browser’s address bar, and you want to avoid a possible collision with your site’s .htaccess file redirect.)
You can also use the url()
function for appending query string parameters and/or enforcing a protocol on an absolute URL:
{{ url('http://my-project.tld', 'foo=1', 'https') }}
{# Outputs: "https://my-project.tld?foo=1" #}