Liquid Markup Language

Liquid is the language used to build custom SandwichBoard themes. It is a small and fast template language, which is quick to learn and yet is very powerful.

Basics

There are two types of markup in liquid: Output and Tag.

  • Output is surrounded by {{ two curly brackets }}
  • Tags are surrounded by {% a curly bracket and a percent %}

Output blocks will always be replaced with the data which they reference. If your Liquid template has a menu object exposed to it, you can render the name of the menu by referencing {{ menu.name }}. Output blocks may be manipulated by Liquid filters.

Tags drive the logic of templates. They are responsible for loops and branching logic such as If / Else.

It is often necessary to access SandwichBoard-created objects like menus and restaurant locations by using their Liquid drops in Output and Tags.

Sometimes you have to use string literals, which are constructed inside single-quotes.

Example

'tobi'

Output

Here is a simple example of Output:

Hello {{ name }}
Hello {{ menu.name }}
Hello {{ 'tobi' }}

Filters

Output markup takes filters. Filters are simple methods. The first parameter is always the output of the left side of the filter. The return value of the filter will be the new left value when the next filter is run. When there are no more filters the template will receive the resulting string. This is very similar to the concept of piping in *NIX.

Hello {{ 'tobi' | upcase }}                    # => Hello TOBI
Hello tobi has {{ 'tobi' | length }} letters!  # => Hello tobi has 4 letters!
Hello {{ 'to_bi' | handle | upcase }}          # => Hello TO_BI

Some filters take more than one input value. date(input, format) for example takes both a date and format string as parameters. The first parameter, input, is passed from left to right as usual. The second parameter, format is passed after the colon immediately following the name of the function, date.

Hello {{ article.created_at | date: '%Y %H' }} # => 2009 03

Any additional parameters also come after the colon and are separated from each other with commas.

This is a hypothetical link {{ menu | link_to_menu: '_blank', 'blink' }}

Tags

Tags are for the logic in your template.

Here is a list of currently supported tags:

  • Comments
  • If / Else
  • Case Statement
  • Cycle
  • For Loops
  • Tables
  • Variable Assignment
  • Include

Global Variables

Several Liquid variables are accessible from any template or page:

  • title returns the title for the current template or page. If a template, the title is set to the title or name of the object being displayed. For example, if the template is menu.liquid, title is set to menu.name.
  • path returns the relative path to the current template or page.
  • articles returns the entire collection of published restaurant article drops. You may wish to use the for loop limit property.
  • events returns the entire collection of published restaurant event drops. You may wish to use the for loop limit property.
  • pages returns the entire collection of published web site page drops.
  • restaurant returns the current restaurant as a drop.

Articles and events can be tagged in SandwichBoard with keywords. To limit the collections to a specific tag, simply suffix articles or events with _by_keyword.

Example

{% for article in articles_by_press %}
  ...
{% endfor %}


You can read more about Liquid at the project's official web site.

 
liquid.txt · Last modified: 2008/07/23 18:16 by ianlotinsky