Twig Functions

Twig functions are called directly with any parameters being passed in via parenthesis.


Cast a value to array

{% set value = array(value) %}


Computes the difference of arrays.

{% set diff = array_diff(array1, array2...) %}


The array_key_value function allows you to add a key/value pair to an associate array

{% set my_array = {fruit: 'apple'} %}
{% set my_array = array_key_value('meat','steak', my_array) %}
{{ print_r(my_array)}}

outputs: Array ( [fruit] => apple [meat] => steak )


Wrapper for PHP's array_key_exists function that returns whether or not a key exists in an associative array.

{% set my_array = {fruit: 'apple', meat: 'steak'} %}
{{ array_key_exists('meat', my_array) }}

outputs: 1


The array_intersect function provides the intersection of two arrays or Grav collections.

{% set array_1 = {fruit: 'apple', meat: 'steak'} %}
{% set array_2 = {fish: 'tuna', meat: 'steak'} %}
{{ print_r(array_intersect(array_1, array_2)) }}

outputs: Array ( [meat] => steak )


Wrapper for PHP array_unique() that removes duplicates from an array.

array_unique(['foo', 'bar', 'foo', 'baz']) Array ( [0] => foo [1] => bar [3] => baz )


Authorizes an authenticated user to see a resource. Accepts a single permission string or an array of permission strings.

authorize(['admin.statistics', 'admin.super'])


Takes a valid Twig variable and dumps it out into the Grav debugger panel. The debugger must be enabled to see the values in the messages tab.



Same as dump()


The evaluate function can be used to evaluate a string as Twig:



Similar to evaluate, but will evaluate and process with Twig

evaluate_twig('This is a twig variable: {{ foo }}', {foo: 'bar'})) This is a twig variable: bar


Output the EXIF data from an image based on its filepath. This requires that media: auto_metadata_exif: true is set in system.yaml. For example, in a Twig-template:

{% set image =['sample-image.jpg'] %}
{% set exif = exif(image.filepath, true) %}
{{ exif.MaxApertureValue }}

This would write the MaxApertureValue-value set in the camera, for example "40/10". You can always use {{ dump(exif) }} to show all the available data in the debugger.

Retrieve the value of a cookie with this function:



Takes a Github Gist ID and creates appropriate Gist embed code

gist('bc448ff158df4bc56217') <script src=""></script>


header_var($variable, $pages = null)

Returns page.header.<variable>.

NOTE: Deprecated since Grav 1.7. theme_var should be used.

The logic of finding the variable has changed, which might lead to unexptected results:

  • If an array of lookup pages is provided as second parameter, only the first page will be used.
  • If <variable> is not defined in het header of the page, Grav will search for the variable in the tree of parents of the page.
  • If still not found, Grav will search for the variable in the config file of the theme

Given frontmatter of

title: Home

header_var('title') Home


the isajaxrequest() function can be used to check if HTTP_X_REQUESTED_WITH header option is set:


You can decode JSON by simply applying this filter:

json_decode({"first_name": "Guido", "last_name":"Rossum"})


Returns a media object for an arbitrary directory. Once obtained you can manipulate images in a similar fashion to pages.



Generate a Grav security nonce field for a form with a required action:

nonce_field('action') <input type="hidden" name="nonce" value="849ef3f4096ae9b53ba1d5f8149a4632" />


Parses a path into an array.

{% set parts = pathinfo('/www/htdocs/inc/') %}
{{ print_r(parts) }}

outputs: Array ( [dirname] => /www/htdocs/inc [basename] => [extension] => php [filename] => )


Will generate a random string of the required number of characters. Particularly useful in creating a unique id or key.

random_string(10) uPakQRx8fi


Generates a random string with configurable length, prefix and suffix. Unlike the built-in PHP uniqid() function and the random_string utils, this string will be generated truly unique and non-conflicting.

unique_id(9) d198a331c unique_id(11, { prefix: 'user_' }) uniqueid(11, { prefix: 'user' }) }} unique_id(13, { suffix: '.json' }) unique_id(13, { suffix: '.json' }) }}


Generates an array containing a range of elements, optionally stepped

range(25, 300, 50) Array ( [0] => 25 [1] => 75 [2] => 125 [3] => 175 [4] => 225 [5] => 275 )


Redirects to a URL of your choosing

redirect_me('', 304)


Will repeat whatever is passed in a certain amount of times.

repeat('blah ', 10) blah blah blah blah blah blah blah blah blah blah


Returns a string from a value. If the value is array, return it json encoded

string(23) => "23"

string(['test' => 'x']) => {"test":"x"}


Translate a string, as the |t filter.

t('SITE_NAME') Site Name


Functions the same way the |ta filter does.


Translates a string in a specific language. For more details check out the multi-language documentation.

tl('SIMPLE_TEXT', ['fr'])


Will create a URL and convert any PHP URL streams into a valid HTML resources. A default value can be passed in in case the URL cannot be resolved.



The vardump() function outputs the current variable to the screen (rather than in the debugger as with dump())

{% set my_array = {foo: 'bar', baz: 'qux'} %}
{{ vardump(my_array) }}

  "foo" => "bar"
  "baz" => "qux"

Found errors? Think you can improve this documentation? Simply click the Edit link at the top of the page, and then the icon on Github to make your changes.