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 "http://www.domain.com/page/about-us". 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: "http://www.domain.com/page.html#id_to_get".

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 'http://twitter.com/statuses/user_timeline/84357304.json' as json cache 15 %}
<ul>
{% for tweet in 0..5 %}
  <li>{{ timeline[tweet].text }}<br/><a href="http://twitter.com/gethifi/status/{{ timeline[tweet].id }}">{{ timeline[tweet].created_at }}</a></li>
{% endfor %}
</ul>

Caching

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 "http://www.gethifi.com" as html cache 60 %}