My favorites | Sign in
Project Home
Search
for
UsingLiquidTemplates  

Howto
Updated Feb 4, 2010 by tobias.l...@gmail.com

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

Here is a simple example of Output:

Hello {{name}}          
Hello {{user.name}}
Hello {{ 'tobi' }}

Advanced output: 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.

Hello {{ 'tobi' | upcase }}
Hello tobi has {{ 'tobi' | length }} letters!
Hello {{ '*tobi*' | textilize | upcase }}
Hello {{ now | date: "%Y %h" }}

Tags

Tags are for the logic in your template. New tags are very easy to code and I hope to get many contributions to the standard tag library after releasing this code.

Here is a list of currently supported tags:

Comments

Comment is the simplest tag. It just swallows content.

Hi tobi {% comment %} you stink {% endcomment %}

If / Else

If else should be well known from any language imaginable. Liquid allows you to write simple expressions in the if.

{% if user %}
  Hi {{ user.name }}
{% endif %}

{% if user.name == 'tobi' %}
  hi tobi
{% endif %}

{% if user.name != 'tobi' %} 
  hi non-tobi
{% endif %}

{% if user.creditcard == null %}
   poor sob
{% endif %}

{% if user.payments == empty %}
   you never paid ! 
{% endif %}

{% if user.age > 18 %}
   Login here
{% else %}
   Sorry, you are too young
{% endif %}

Case Statement

If you need more than one condition you can use the Case Statement

{% case condition %} 
{% when 1 %} 
hit 1 
{% when 2 %} 

hit 2 
{% else %} 
elseblock 
{% endcase %} 

Example:

{% case template %}
	
{% when 'label' %}
     // {{ label.title }}
{% when 'product' %}
     // {{ product.vendor | link_to_vendor }} / {{ product.title }}
{% else %}
     // {{page_title}
{% endcase %}

Cycle

Often you have to alternate between different colors or similar tasks. Liquid has build in support for such operations using the cycle tag.

{% cycle 'one', 'two', 'three' %} 
{% cycle 'one', 'two', 'three' %} 
{% cycle 'one', 'two', 'three' %} 
{% cycle 'one', 'two', 'three' %} 

will result in 

one
two
three
one

If no name is supplied for the cycle group then its assumed that multiple calls with the same parameters are one group.

If you want to have total control over cycle groups you can optionally specify the name of the group. This can even be a variable.

{% cycle 'group 1': 'one', 'two', 'three' %} 
{% cycle 'group 1': 'one', 'two', 'three' %} 
{% cycle 'group 2': 'one', 'two', 'three' %} 
{% cycle 'group 2': 'one', 'two', 'three' %} 

will result in 

one
two 
one
two

For loops

Liquid allows for loops over collections

  {% for item in array %} 
    {{ item }}
  {% endfor %} 

During every for loop there are following helper variables available for extra styling needs:

 forloop.length      # => length of the entire for loop
 forloop.index       # => index of the current iteration 
 forloop.index0	    # => index of the current iteration (zero based) 
 forloop.rindex     # => how many items are still left?
 forloop.rindex0   # => how many items are still left? (zero based)
 forloop.first        # => is this the first iteration?
 forloop.last         # => is this the last iternation? 

There are several attributes you can use to influence which items you receive in your loop

limit lets you restrict how many items you get offset lets you start the collection with the nth item.

  # array = [1,2,3,4,5,6]
  {% for item in array limit:2 offset:2 %} 
    {{ item }}
  {% endfor %} 
  # results in 3,4 

Instead of looping over an existing collection, you can define a range of numbers to loop through. The range can be defined by both literal and variable numbers:

  # if item.quantity is 4...
  {% for i in (1..item.quantity) %}
    {{ i }}
  {% endfor %}
  # results in 1,2,3,4

Variable Assignment

You can store data in your own variables, to be used in output or other tags as desired. The simplest way to create a variable is with the assign tag, which has a pretty straightforward syntax:

{% assign name = 'freestyle' %}

{% for t in collections.tags %}{% if t == name %}
  <p>Freestyle!</p>
{% endif %}{% endfor %}

Another way of doing this would be to assign true/false values to the variable:

{% assign freestyle = false %}

{% for t in collections.tags %}{% if t == 'freestyle' %}
  {% assign freestyle = true %}
{% endif %}{% endfor %}

{% if freestyle %}
  <p>Freestyle!</p>
{% endif %}

If you want to combine a number of strings into a single string and save it to a variable, you can do that with the capture tag. This tag is a block which "captures" whatever is rendered inside it and assigns it to the given variable instead of rendering it to the screen.

Comment by elliottc...@gmail.com, Jul 22, 2007

In the last example, there's a stray apostrophe - currently, it's this: {% for t in collections.tags %}{% if t == 'freestyle %} Should be this: {% for t in collections.tags %}{% if t == freestyle %}

Comment by Igor.Mi...@gmail.com, Jul 22, 2007

It seems that the variables are present only in the current scope ("global", if, loop, ..). I don't see this mentioned in the docs. It might be a good idea to mention it.

Comment by thomas.l...@gmail.com, Aug 22, 2007

I'm just curious. If this is for RoR apps, why not just use rhtml?

Comment by thomas.l...@gmail.com, Aug 22, 2007

I'm just curious. If this is for RoR apps, why not just use rhtml?

Comment by gustavo...@gmail.com, Aug 23, 2007

I like how it is used in Mephisto blog. But for writing RoR apps directly I'd rather stick with rhtml.

Comment by project member codyfau...@gmail.com, Sep 19, 2007

The whole point is that it is non-evaling. This means that the users only have access to the data you specify, and you don't have to worry about them running arbitrary queries or code.

Comment by sgrunber...@gmail.com, Jan 15, 2008

Looping over a range of numbers, as documented above, doesn't appear to work.

  {% for i in (1..5) %}
    {{ i }}
  {% endfor %}

=> Syntax Error in 'for loop' - Valid syntax: for [item] in [collection]
Comment by andrew.s...@gmail.com, Jan 28, 2008

(1..5) is for ruby, not liquid

Comment by project member tobias.l...@gmail.com, Jan 28, 2008

(1..5) works in liquid. You may need a checked out version

Comment by elliottc...@gmail.com, Feb 1, 2008

Still a mismatched apostrophe in the last example up there [-:

Comment by elliottc...@gmail.com, Feb 1, 2008

Also, how unlikely does it seem that all of the first four comments were on the 22nd of a month? d-:

Comment by sgrunber...@gmail.com, Feb 4, 2008

Re: ranges, I was using the latest rubygem, which I hadn't realized is somewhat out of date. As Tobias mentioned, ranges are supported in the latest from source control.

Comment by prideafr...@gmail.com, Feb 9, 2008

What about using regular expressions to find out if a text string contains some pattern? Does this language support RE?

Comment by project member tobias.l...@gmail.com, Feb 9, 2008

Please use the mailing list for such discussion. Liquid does not contain regular expression support at the moment but if it can be implemented in a secure way there is no reason to not accept a patch for it.

Comment by ashish....@gmail.com, Aug 13, 2009

how to save a liquid tags file and how to execute the code

Comment by kbala...@gmail.com, Aug 17, 2009

How do I get the index, first, last, etc variables in a for loop?

Comment by bertrand...@gmail.com, Jul 27, 2011

Is there a way to retrieve all the output markup for a given template?

The best way I've found so far is to do something like:

root.nodelist.select do |node|

node.is_a?(Liquid::Variable
end

There's gotta be a better way to do that...

Comment by petemcke...@servicesidekick.com, Aug 17, 2011

how can I format a number using liquid? I have this field coming in with decimals and I need to eliminate the decimal only show the whole part of the number.


Sign in to add a comment
Powered by Google Project Hosting