In addition to the template functions that Twig comes with, Craft provides a few of its own:

alias( string )

Passes a string through Craft::getAlias(), which will check if the string begins with an alias. (See Configuration for more info.)

<img src="{{ alias('@assetBaseUrl/images/logo.png') }}">


Outputs any scripts and styles that were registered for the “begin body” position. It should be placed right after your <body> tag.

    {{ beginBody() }}

    <h1>{{ }}</h1>
    {{ page.body }}

ceil( num )

Rounds a number up.

{{ ceil(42.1) }} => 43

className( object )

Returns the fully qualified class name of a given object.

clone( object )

Clones a given object.

{% set query = craft.entries.section('news') %}
{% set articles = clone(query).type('articles') %}


Returns a hidden CSRF Token input. All sites that have CSRF Protection enabled must include this in each form that submits via POST.

<form method="post">
    {{ csrfInput() }}
    <!-- ... -->


Outputs any scripts and styles that were registered for the “end body” position. It should be placed right before your </body> tag.

    <h1>{{ }}</h1>
    {{ page.body }}

    {{ endBody() }}

floor( num )

Rounds a number down.

{{ floor(42.9) }} => 42

getenv( name )

Returns the value of an environment variable.

{{ getenv('MAPS_API_KEY') }}

Outputs any scripts and styles that were registered for the “head” position. It should be placed right before your </head> tag.

    <title>{{ siteName }}</title>
    {{ head() }}

redirectInput( url )

Shortcut for typing <input type="hidden" name="redirect" value="{{ url|hash }}">.

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>
{% endfor %}

svg( svg, sanitize )

Returns the contents of a given SVG file.


The svg() function has the following arguments:

  • svg – The SVG file path, an SVG file’s contents, or an craft\elements\Asset object that represents an SVG file.
  • sanitize – Whether the SVG should be sanitized of any potentially malicious scripts (true by default).

url( path, params, protocol, mustShowScriptName )

Returns a URL to a page on your site.

<a href="{{ url('company/contact') }}">Contact Us</a>


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 base site URL.
  • 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, then https will be used.
  • mustShowScriptName – If this is set to true, then the URL returned will include “index.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 use the url() function for appending query string parameters and/or enforcing a protocol on an absolute URL:

{{ url('', 'foo=1', 'https') }}
{# Outputs: "" #}