Retrieving External Content

You can pull content from an external website directly into your templates using the retrieve tag:

{% retrieve [varname] from '[url]' %}

The content of the link will be assigned to a variable, which you can display or use in other tags as needed.

Specifying the url's data-type

There are currently three types of data that you can get with the retrieve tag. These are specified by adding as '[type]' after the url. By default, or with 'text' specified, the literal contents of the url are used.

With the type set to html, relative urls in the retrieved content will be "fixed" to point back to the original domain. In other words, if there is a link in the html with the href "/page/about-us", it will be modified to "". Typically, only the contents of the <body> tag will be returned. You can also specify that only a portion of the page should be returned, based on an element's ID. To do this, use the standard hash syntax with the url: "".

Finally, you can get the remote content as 'json', which will parse the string into a native object. This is very useful for incorporating data from an API into your website. For instance, you might want to get the five most recent tweets from a company's Twitter timeline and display them in the sidebar:

{% retrieve timeline from '' as json cache 15 %}
{% for tweet in 0..5 %}
  <li>{{ timeline[tweet].text }}<br/><a href="{{ timeline[tweet].id }}">{{ timeline[tweet].created_at }}</a></li>
{% endfor %}


In general, you won't want to hit the remote server on every page view. In particular, many APIs (including Twitter) will cut you off after a certain number of requests in an hour. For that reason, and for overall performance, you should use caching. To do that, add cache [minutes] to the end of the tag, with [minutes] being the number of minutes to wait before requesting fresh data from the remote server.

Additionally, HiFi implements page-level http caching using Varnish for a maximum of 3 hours.

{% retrieve hifi from "" as html cache 60 %}


You can set variables on the fly using the 'set' variable:

{% set var as 'value' %}

Note that HiFi currently uses 'as' rather than the '=' sign that the current Twig documentation shows.


It is possible to get around variables in twig just as you would in any programming language:

{{ }}
{{ }}
{{ var.array['val'].property }}


Variables in twig are displayed using double curly braces:

{{ var }} - {{ }} - {{ array['key'] }}

When used in control structures, the synax is the same:

{% if > 5 %}BIG{% endif %}

In any place that a variable can be be used, it can also be filtered. See the filtering section for which filters are available.

{{ var|filter('val') }}