3 date: 'Sat 2012-06-09 17:54:04 +0800'
11 <li><a href="#liquid_extensions">Jekyll Liquid Extensions</a></li>
12 <li><a href="#liquid_for_designers">Liquid for Designers</a></li>
13 <li><a href="/pages/Help.html#syntax_cheatsheet">On Markdown</a></li>
19 https://github.com/mojombo/jekyll/wiki/Liquid-Extensions
21 Jekyll uses [Liquid] [L] to process templates. Along with the [standard liquid tags and filters] [s], Jekyll adds a few of its own:
23 [L]: http://www.liquidmarkup.org/
24 [s]: http://wiki.github.com/shopify/liquid/liquid-for-designers
28 ### Date to XML Schema
29 Convert a Time into XML Schema format.
31 {{ site.time | date_to_xmlschema }} => 2008-11-17T13:07:54-08:00
35 Convert a date in short format, e.g. “27 Jan 2011”.
37 {{ site.time | date_to_string }} => 17 Nov 2008
40 ### Date to Long String
41 Format a date in long format e.g. “27 January 2011”.
43 {{ site.time | date_to_long_string }} => 17 November 2008
47 Escape some text for use in XML.
49 {{ page.content | xml_escape }}
53 CGI escape a string for use in a URL. Replaces any special characters with appropriate %XX replacements.
55 {{ "foo,bar;baz?" | cgi_escape }} => foo%2Cbar%3Bbaz%3F
59 Count the number of words in some text.
61 {{ page.content | number_of_words }} => 1337
65 Convert an array into a sentence.
67 {{ page.tags | array_to_sentence_string }} => foo, bar, and baz
71 Convert a Textile-formatted string into HTML, formatted via RedCloth
73 {{ page.excerpt | textilize }}
77 Convert a Markdown-formatted string into HTML.
79 {{ page.excerpt | markdownify }}
87 If you have small page fragments that you wish to include in multiple places
88 on your site, you can use the include tag.
90 {% include sig.textile %}
92 Jekyll expects all include files to be placed in an `_includes` directory at the root of your source dir. So this will embed the contents of `/path/to/proto/site/_includes/sig.textile` into the calling file.
96 Jekyll has built in support for syntax highlighting of over [100 languages] [100] via [Pygments] [P]. In order to take advantage of this you’ll need to have Pygments installed, and the pygmentize binary must be in your path. When you run Jekyll, make sure you run it with [Pygments support] [Ps]
98 [100]: http://pygments.org/languages/
99 [P]: http://pygments.org/
100 [Ps]: https://github.com/mojombo/jekyll/wiki/Configuration
102 To denote a code block that should be highlighted:
111 The argument to `highlight` is the language identifier. To find the appropriate identifier to use for your favorite language, look for the “short name” on the [Lexers](http://pygments.org/docs/lexers/) page.
115 There is a second argument to `highlight` called `linenos` that is optional. Including the `linenos` argument will force the highlighted code to include line numbers. For instance, the following code block would include line numbers next to each line:
117 {% highlight ruby linenos %}
124 In order for the highlighting to show up, you’ll need to include a highlighting stylesheet. For an example stylesheet you can look at [syntax.css](http://github.com/mojombo/tpw/tree/master/css/syntax.css). These are the same styles as used by GitHub and you are free to use them for your own site. If you use `linenos`, you might want to include an additional CSS class definition for lineno in syntax.css to distinguish the line numbers from the highlighted code.
128 If you would like to include a link to a post on your site, you can use the `post_url` tag.
130 {% post_url 2010-07-21-name-of-post %}
133 There is no need to include the extension.
135 To create a link, do:
137 [Name of Link]({% post_url 2010-07-21-name-of-post %})
145 https://github.com/shopify/liquid/wiki/liquid-for-designers
147 There are two types of markup in Liquid: Output and Tag.
149 * Output markup (which may resolve to text) is surrounded by
153 {{ matched pairs of curly brackets (ie, braces) }}
157 * Tag markup (which cannot resolve to text) is surrounded by
161 {% matched pairs of curly brackets and percent signs %}
167 Here is a simple example of Output:
177 ### Advanced output: Filters
179 Output markup takes filters. Filters are simple methods. The first parameter
180 is always the output of the left side of the filter. The return value of the
181 filter will be the new left value when the next filter is run. When there are
182 no more filters, the template will receive the resulting string.
186 Hello {{ 'tobi' | upcase }}
187 Hello tobi has {{ 'tobi' | size }} letters!
188 Hello {{ '*tobi*' | textilize | upcase }}
189 Hello {{ 'now' | date: "%Y %h" }}
195 * `date` - reformat a date [syntax reference](http://liquid.rubyforge.org/classes/Liquid/StandardFilters.html#M000012)
196 * `capitalize` - capitalize words in the input sentence
197 * `downcase` - convert an input string to lowercase
198 * `upcase` - convert an input string to uppercase
199 * `first` - get the first element of the passed in array
200 * `last` - get the last element of the passed in array
201 * `join` - join elements of the array with certain character between them
202 * `sort` - sort elements of the array
203 * `map` - map/collect an array on a given property
204 * `size` - return the size of an array or string
205 * `escape` - escape a string
206 * `escape_once` - returns an escaped version of html without affecting existing escaped entities
207 * `strip_html` - strip html from string
208 * `strip_newlines` - strip all newlines (\n) from string
209 * `newline_to_br` - replace each newline (\n) with html break
210 * `replace` - replace each occurrence *e.g.* `{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'`
211 * `replace_first` - replace the first occurrence *e.g.* `{{ 'barbar' | replace_first:'bar','foo' }} #=> 'foobar'`
212 * `remove` - remove each occurrence *e.g.* `{{ 'foobarfoobar' | remove:'foo' }} #=> 'barbar'`
213 * `remove_first` - remove the first occurrence *e.g.* `{{ 'barbar' | remove_first:'bar' }} #=> 'bar'`
214 * `truncate` - truncate a string down to x characters
215 * `truncatewords` - truncate a string down to x words
216 * `prepend` - prepend a string *e.g.* `{{ 'bar' | prepend:'foo' }} #=> 'foobar'`
217 * `append` - append a string *e.g.* `{{ 'foo' | append:'bar' }} #=> 'foobar'`
218 * `minus` - subtraction *e.g.* `{{ 4 | minus:2 }} #=> 2`
219 * `plus` - addition *e.g.* `{{ '1' | plus:'1' }} #=> '11'`, `{{ 1 | plus:1 }} #=> 2`
220 * `times` - multiplication *e.g* `{{ 5 | times:4 }} #=> 20`
221 * `divided_by` - division *e.g.* `{{ 10 | divided_by:2 }} #=> 5`
222 * `split` - split a string on a matching pattern *e.g.* `{{ "a~b" | split:~ }} #=> ['a','b']`
223 * `modulo` - remainder, *e.g.* `{{ 3 | modulo:2 }} #=> 1`
227 Tags are used for the logic in your template. New tags are very easy to code,
228 so I hope to get many contributions to the standard tag library after releasing
231 Here is a list of currently supported tags:
233 * **assign** - Assigns some value to a variable
234 * **capture** - Block tag that captures text into a variable
235 * **case** - Block tag, its the standard case...when block
236 * **comment** - Block tag, comments out the text in the block
237 * **cycle** - Cycle is usually used within a loop to alternate between values, like colors or DOM classes.
239 * **if** - Standard if/else block
240 * **include** - Includes another template; useful for partials
241 * **raw** - temporarily disable tag processing to avoid syntax conflicts.
242 * **unless** - Mirror of if statement
246 Comment is the simplest tag. It just swallows content.
250 We made 1 million dollars {% comment %} in losses {% endcomment %} this year
256 Raw temporarily disables tag processing.
257 This is useful for generating content (eg, Mustache, Handlebars) which uses conflicting syntax.
259 <div class="highlight"><code class="ruby"><span class="nb">
260 {% raw %}<br>
261 In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.<br>
262 {% endraw %}
269 `if / else` should be well-known from any other programming language.
270 Liquid allows you to write simple expressions in the `if` or `unless` (and
271 optionally, `elsif` and `else`) clause:
276 Hello {{ user.name }}
279 {% if user.name == 'tobi' %}
281 {% elsif user.name == 'bob' %}
285 {% if user.name == 'tobi' or user.name == 'bob' %}
289 {% if user.name == 'bob' and user.age > 45 %}
293 {% if user.name != 'tobi' %}
298 {% unless user.name == 'tobi' %}
302 # Check if the user has a credit card
303 {% if user.creditcard != null %}
308 {% if user.creditcard %}
312 # Check for an empty array
313 {% if user.payments == empty %}
317 {% if user.age > 18 %}
320 Sorry, you are too young
324 {% if array contains 2 %}
328 # string = 'hello world'
329 {% if string contains 'hello' %}
330 string includes 'hello'
337 If you need more conditions, you can use the `case` statement:
361 // {{ product.vendor | link_to_vendor }} / {{ product.title }}
370 Often you have to alternate between different colors or similar tasks. Liquid
371 has built-in support for such operations, using the `cycle` tag.
375 {% cycle 'one', 'two', 'three' %}
376 {% cycle 'one', 'two', 'three' %}
377 {% cycle 'one', 'two', 'three' %}
378 {% cycle 'one', 'two', 'three' %}
389 If no name is supplied for the cycle group, then it's assumed that multiple
390 calls with the same parameters are one group.
392 If you want to have total control over cycle groups, you can optionally specify
393 the name of the group. This can even be a variable.
397 {% cycle 'group 1': 'one', 'two', 'three' %}
398 {% cycle 'group 1': 'one', 'two', 'three' %}
399 {% cycle 'group 2': 'one', 'two', 'three' %}
400 {% cycle 'group 2': 'one', 'two', 'three' %}
413 Liquid allows `for` loops over collections:
417 {% for item in array %}
423 During every `for` loop, the following helper variables are available for extra
428 forloop.length # => length of the entire for loop
429 forloop.index # => index of the current iteration
430 forloop.index0 # => index of the current iteration (zero based)
431 forloop.rindex # => how many items are still left?
432 forloop.rindex0 # => how many items are still left? (zero based)
433 forloop.first # => is this the first iteration?
434 forloop.last # => is this the last iteration?
438 There are several attributes you can use to influence which items you receive in
441 `limit:int` lets you restrict how many items you get.
442 `offset:int` lets you start the collection with the nth item.
446 # array = [1,2,3,4,5,6]
447 {% for item in array limit:2 offset:2 %}
458 {% for item in collection reversed %} {{item}} {% endfor %}
462 Instead of looping over an existing collection, you can define a range of
463 numbers to loop through. The range can be defined by both literal and variable
468 # if item.quantity is 4...
469 {% for i in (1..item.quantity) %}
476 ### Variable Assignment
478 You can store data in your own variables, to be used in output or other tags as
479 desired. The simplest way to create a variable is with the `assign` tag, which
480 has a pretty straightforward syntax:
484 {% assign name = 'freestyle' %}
486 {% for t in collections.tags %}{% if t == name %}
488 {% endif %}{% endfor %}
492 Another way of doing this would be to assign `true / false` values to the
497 {% assign freestyle = false %}
499 {% for t in collections.tags %}{% if t == 'freestyle' %}
500 {% assign freestyle = true %}
501 {% endif %}{% endfor %}
509 If you want to combine a number of strings into a single string and save it to
510 a variable, you can do that with the `capture` tag. This tag is a block which
511 "captures" whatever is rendered inside it, then assigns the captured value to
512 the given variable instead of rendering it to the screen.
516 {% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %}
518 <label for="{{ attribute_name }}">Color:</label>
519 <select name="attributes[{{ attribute_name }}]" id="{{ attribute_name }}">
520 <option value="red">Red</option>
521 <option value="green">Green</option>
522 <option value="blue">Blue</option>