3 date: 'Sat 2012-06-09 18:26:11 +0800'
15 https://github.com/shopify/liquid/wiki/liquid-for-designers
17 There are two types of markup in Liquid: Output and Tag.
19 * Output markup (which may resolve to text) is surrounded by
23 {{ matched pairs of curly brackets (ie, braces) }}
27 * Tag markup (which cannot resolve to text) is surrounded by
31 {% matched pairs of curly brackets and percent signs %}
37 Here is a simple example of Output:
47 ### Advanced output: Filters
49 Output markup takes filters. Filters are simple methods. The first parameter
50 is always the output of the left side of the filter. The return value of the
51 filter will be the new left value when the next filter is run. When there are
52 no more filters, the template will receive the resulting string.
56 Hello {{ 'tobi' | upcase }}
57 Hello tobi has {{ 'tobi' | size }} letters!
58 Hello {{ '*tobi*' | textilize | upcase }}
59 Hello {{ 'now' | date: "%Y %h" }}
65 * `date` - reformat a date [syntax reference](http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000012)
66 * `capitalize` - capitalize words in the input sentence
67 * `downcase` - convert an input string to lowercase
68 * `upcase` - convert an input string to uppercase
69 * `first` - get the first element of the passed in array
70 * `last` - get the last element of the passed in array
71 * `join` - join elements of the array with certain character between them
72 * `sort` - sort elements of the array
73 * `map` - map/collect an array on a given property
74 * `size` - return the size of an array or string
75 * `escape` - escape a string
76 * `escape_once` - returns an escaped version of html without affecting existing escaped entities
77 * `strip_html` - strip html from string
78 * `strip_newlines` - strip all newlines (\n) from string
79 * `newline_to_br` - replace each newline (\n) with html break
80 * `replace` - replace each occurrence *e.g.* `{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'`
81 * `replace_first` - replace the first occurrence *e.g.* `{{ 'barbar' | replace_first:'bar','foo' }} #=> 'foobar'`
82 * `remove` - remove each occurrence *e.g.* `{{ 'foobarfoobar' | remove:'foo' }} #=> 'barbar'`
83 * `remove_first` - remove the first occurrence *e.g.* `{{ 'barbar' | remove_first:'bar' }} #=> 'bar'`
84 * `truncate` - truncate a string down to x characters
85 * `truncatewords` - truncate a string down to x words
86 * `prepend` - prepend a string *e.g.* `{{ 'bar' | prepend:'foo' }} #=> 'foobar'`
87 * `append` - append a string *e.g.* `{{ 'foo' | append:'bar' }} #=> 'foobar'`
88 * `minus` - subtraction *e.g.* `{{ 4 | minus:2 }} #=> 2`
89 * `plus` - addition *e.g.* `{{ '1' | plus:'1' }} #=> '11'`, `{{ 1 | plus:1 }} #=> 2`
90 * `times` - multiplication *e.g* `{{ 5 | times:4 }} #=> 20`
91 * `divided_by` - division *e.g.* `{{ 10 | divided_by:2 }} #=> 5`
92 * `split` - split a string on a matching pattern *e.g.* `{{ "a~b" | split:~ }} #=> ['a','b']`
93 * `modulo` - remainder, *e.g.* `{{ 3 | modulo:2 }} #=> 1`
97 Tags are used for the logic in your template. New tags are very easy to code,
98 so I hope to get many contributions to the standard tag library after releasing
101 Here is a list of currently supported tags:
103 * **assign** - Assigns some value to a variable
104 * **capture** - Block tag that captures text into a variable
105 * **case** - Block tag, its the standard case...when block
106 * **comment** - Block tag, comments out the text in the block
107 * **cycle** - Cycle is usually used within a loop to alternate between values, like colors or DOM classes.
109 * **if** - Standard if/else block
110 * **include** - Includes another template; useful for partials
111 * **raw** - temporarily disable tag processing to avoid syntax conflicts.
112 * **unless** - Mirror of if statement
116 Comment is the simplest tag. It just swallows content.
120 We made 1 million dollars {% comment %} in losses {% endcomment %} this year
126 Raw temporarily disables tag processing.
127 This is useful for generating content (eg, Mustache, Handlebars) which uses conflicting syntax.
129 <div class="highlight"><code class="ruby"><span class="nb">
130 {% raw %}<br>
131 In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.<br>
132 {% endraw %}
139 `if / else` should be well-known from any other programming language.
140 Liquid allows you to write simple expressions in the `if` or `unless` (and
141 optionally, `elsif` and `else`) clause:
146 Hello {{ user.name }}
149 {% if user.name == 'tobi' %}
151 {% elsif user.name == 'bob' %}
155 {% if user.name == 'tobi' or user.name == 'bob' %}
159 {% if user.name == 'bob' and user.age > 45 %}
163 {% if user.name != 'tobi' %}
168 {% unless user.name == 'tobi' %}
172 # Check if the user has a credit card
173 {% if user.creditcard != null %}
178 {% if user.creditcard %}
182 # Check for an empty array
183 {% if user.payments == empty %}
187 {% if user.age > 18 %}
190 Sorry, you are too young
194 {% if array contains 2 %}
198 # string = 'hello world'
199 {% if string contains 'hello' %}
200 string includes 'hello'
207 If you need more conditions, you can use the `case` statement:
231 // {{ product.vendor | link_to_vendor }} / {{ product.title }}
240 Often you have to alternate between different colors or similar tasks. Liquid
241 has built-in support for such operations, using the `cycle` tag.
245 {% cycle 'one', 'two', 'three' %}
246 {% cycle 'one', 'two', 'three' %}
247 {% cycle 'one', 'two', 'three' %}
248 {% cycle 'one', 'two', 'three' %}
259 If no name is supplied for the cycle group, then it's assumed that multiple
260 calls with the same parameters are one group.
262 If you want to have total control over cycle groups, you can optionally specify
263 the name of the group. This can even be a variable.
267 {% cycle 'group 1': 'one', 'two', 'three' %}
268 {% cycle 'group 1': 'one', 'two', 'three' %}
269 {% cycle 'group 2': 'one', 'two', 'three' %}
270 {% cycle 'group 2': 'one', 'two', 'three' %}
283 Liquid allows `for` loops over collections:
287 {% for item in array %}
293 During every `for` loop, the following helper variables are available for extra
298 forloop.length # => length of the entire for loop
299 forloop.index # => index of the current iteration
300 forloop.index0 # => index of the current iteration (zero based)
301 forloop.rindex # => how many items are still left?
302 forloop.rindex0 # => how many items are still left? (zero based)
303 forloop.first # => is this the first iteration?
304 forloop.last # => is this the last iteration?
308 There are several attributes you can use to influence which items you receive in
311 `limit:int` lets you restrict how many items you get.
312 `offset:int` lets you start the collection with the nth item.
316 # array = [1,2,3,4,5,6]
317 {% for item in array limit:2 offset:2 %}
328 {% for item in collection reversed %} {{item}} {% endfor %}
332 Instead of looping over an existing collection, you can define a range of
333 numbers to loop through. The range can be defined by both literal and variable
338 # if item.quantity is 4...
339 {% for i in (1..item.quantity) %}
346 ### Variable Assignment
348 You can store data in your own variables, to be used in output or other tags as
349 desired. The simplest way to create a variable is with the `assign` tag, which
350 has a pretty straightforward syntax:
354 {% assign name = 'freestyle' %}
356 {% for t in collections.tags %}{% if t == name %}
358 {% endif %}{% endfor %}
362 Another way of doing this would be to assign `true / false` values to the
367 {% assign freestyle = false %}
369 {% for t in collections.tags %}{% if t == 'freestyle' %}
370 {% assign freestyle = true %}
371 {% endif %}{% endfor %}
379 If you want to combine a number of strings into a single string and save it to
380 a variable, you can do that with the `capture` tag. This tag is a block which
381 "captures" whatever is rendered inside it, then assigns the captured value to
382 the given variable instead of rendering it to the screen.
386 {% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %}
388 <label for="{{ attribute_name }}">Color:</label>
389 <select name="attributes[{{ attribute_name }}]" id="{{ attribute_name }}">
390 <option value="red">Red</option>
391 <option value="green">Green</option>
392 <option value="blue">Blue</option>