Difference between revisions of "Liquid Basics"

From Spiffy Stores Knowledge Base

 
(86 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Liquid Template Syntax Basics =
+
Spiffy Stores gives you 100% control over the HTML and CSS for your online storefront. Our themes use standard HTML and CSS, with dynamic tags from a templating language called "Liquid" to display dynamic data. This helps you transform your design into a dynamic e-commerce web site as quickly as possible.
  
This is an introduction to the Spiffy Stores' Liquid template syntax, and a reference for Spiffy Stores-specific Tags and Filters.
+
This is an introduction to the Spiffy Stores Liquid template syntax, and a reference for Spiffy Stores-specific Tags and Filters.
  
 
Liquid is the templating engine for customizing your store layout. It's a small and fast template language which is quick and easy to learn but contains very powerful features for full customization.
 
Liquid is the templating engine for customizing your store layout. It's a small and fast template language which is quick and easy to learn but contains very powerful features for full customization.
 +
 +
You can use a template language such as Liquid to substitute variable data into a page layout. For example, each product page is defined by the <code>product.liquid</code> template. This file contains the HTML that describes the layout of the various elements on a product page. Within this template, you will find various Liquid tags and variables that help to display different data for each product.
 +
 +
For example, a product template will usually contain a product title, using the <code><nowiki>{{ product.title }}</nowiki></code> Liquid variable. A different title will be used for each different product that is displayed, as the Liquid variable is substituted for the actual product title.
  
 
== Basics ==
 
== Basics ==
  
There are two types of markup in liquid: Output and Tag.
+
There are two types of markup in liquid: '''Output''' and '''Tag'''.
  
* Output is surrounded by <pre> {{ two curly brackets }} </pre>
+
* '''Output''' is surrounded by - <code><nowiki>{{ two curly brackets }}</nowiki></code>
* Tags are surrounded by <pre> {% a curly bracket and a percent %} </pre>
+
* '''Tags''' are surrounded by - <code><nowiki>{% a curly bracket and a percent %}</nowiki></code>
  
 
Output blocks will always be replaced with the data which they reference.
 
Output blocks will always be replaced with the data which they reference.
  
For instance if your liquid template has a product object exposed to it you can print the name of the product to the screen by referencing <pre> {{ product.title }} </pre>
+
For instance if your liquid template has a product object exposed to it you can print the name of the product to the screen by referencing <code><nowiki>{{ product.title }}</nowiki></code>
  
Tags drive the logic of templates. They are responsible for loops and branching logic such as If / Else.
+
Tags allow you to control the logic of templates. They are responsible for loops and branching logic such as <code>If / Else</code>.
  
 
== Output ==
 
== Output ==
  
Here is a simple example of Output:
+
Here is a simple example of '''Output'''. The Liquid code contains variables that will be replaced by the actual values when the template is rendered.
  
 
<pre>
 
<pre>
Hello {{name}}
+
Hello {{ name }}
Hello {{user.name}}
+
Hello {{ user.name }}
 
Hello {{ 'fred' }}
 
Hello {{ 'fred' }}
 
</pre>
 
</pre>
  
=== Acceptable Tags and Comments ===
+
For a full description of all the Liquid variables that can be used in the various templates, please refer to the [[Liquid Variable Reference]].
 
 
We sanitize all templates, so that you may not use javascript or tags that might be harmful to the application. Disallowed tags include, but aren’t limited to:
 
 
 
* HEAD
 
* BODY
 
* SCRIPT
 
 
 
=== Comments ===
 
 
 
HTML comments are automatically sanitized by our system. If you wish to place comments in your code, do it with a liquid comment, like so:
 
 
 
<pre> {{ # This is a comment in liquid, and won't show up on the output }} </pre>
 
 
 
=== 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.
 
 
 
<pre>
 
Hello {{ 'fred' | upcase }}
 
Hello fred has {{ 'fred' | length }} letters!
 
Hello {{ '*fred*' | textilize | upcase }}
 
Hello {{ 'now' | date: "%Y %h" }}
 
</pre>
 
 
 
=== Filter Reference ===
 
 
 
<html><style type="text/css">table.reference th {background-color: #EEEEEE;padding: 0px 4px;text-align: left;vertical-align: top;}</style></html>
 
{| class="reference"
 
!capitalize
 
|Capitalize all words in the input string.
 
|-
 
!date
 
|Reformat a date using an optional format string. Please see below for a list of the valid format characters. **TODO**
 
|-
 
!downcase
 
|Convert a string to lower case.
 
|-
 
!escape
 
|URL encode a string so that all special characters are converted to their % form and can be recognized by browsers.<br/>This may also be aliased as "h".
 
|-
 
!first
 
|Get the first element of an array.<br/><pre>{{ product.images | first | to_img }}</pre>
 
|-
 
!format_date **TODO**
 
|Formats a date in your account&#8217;s selected date format.<br/>(Set from the admin prefs.)
 
|-
 
!format_date_in_english **TODO**
 
|Transforms a date into English of when that event happened.<br/>(Ex 1 day ago, 2 months from now)
 
|-
 
!format_money **TODO**
 
|Transforms money string into your home currency, and possibly your client's home currency if you've selected one for them.
 
|-
 
!format_text **TODO**
 
|Transforms text using Markdown syntax. Automatically inserts <span class="caps">HTML</span> tags like P, BR, etc.
 
|-
 
!image_path **TODO**
 
|Returns the proper image path for your file. Useful if you want to use it in a <span class="caps">CSS</span> document, or perhaps create your own image tags.
 
|-
 
!image_tag **TODO**
 
|Creates an <span class="caps">IMG</span> tag for a file you've uploaded to the server for use inside your document.<br/>Ex: <pre>{{ 'image_name.jpg' | image_tag }}</pre>
 
|-
 
!join
 
|Join elements of an array with an optional join character, which defaults to a space.<br/><pre>{{ names | join(',') }}</pre>
 
|-
 
!last
 
|Get the last element of an array.<br/><pre>{{ product.images | last | to_img }}</pre>
 
|-
 
!make_label **TODO**
 
|Makes an input label, or heading for a table.
 
|-
 
!number_to_percentage **TODO**
 
|Formats number as a percentage.
 
|-
 
!number_to_phone **TODO**
 
|Formats as a phone number.
 
|-
 
!number_with_delimiter **TODO**
 
|Shows a number with delimiter you specify.<br/>Ex: <pre>{{ invoice.sales_tax | number_with_delimiter: ',' }}</pre>
 
|-
 
!size
 
|Return the size of an array or of a string.
 
|-
 
!sort
 
|Sorts the elements in an array.
 
|-
 
!strip_html
 
|Strip out any html tags. This is a simple filter which simply removes any characters in the form of an HTML tag, such as "<...>".
 
|-
 
!truncate
 
|Truncate a string down to x characters. Additionally, a character string can be specified to indicate that truncation has occurred.<br/><pre>{{ 'my long string' | truncate: 50, '...' }}</pre>
 
|-
 
!truncatewords
 
|Truncate a string down to a number of words. This is the same as "truncate", except that the length is specified in words, rather than characters.
 
|-
 
!upcase
 
|Convert a string to upper case.
 
|}
 
  
 
== Tags ==
 
== 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.
+
For a full description on all the Liquid tags, please refer to the [[Liquid Tag Reference]].
 
 
Here is a list of currently supported tags:
 
 
 
=== Comments ===
 
 
 
Comment is the simplest tag. It just swallows content.
 
 
 
<pre>
 
Hi fred {% comment %} you stink {% endcomment %}
 
</pre>
 
 
 
=== If / Else ===
 
  
If else should be well known from any language imaginable. Liquid allows you to write simple expressions
+
== Acceptable Tags and Comments ==
in the if.
 
  
<pre>
+
All templates are sanitized, so you may not use JavaScript or tags that might be harmful to the application. Disallowed tags include, but aren’t limited to:
{% if user %}
 
  Hi {{ user.name }}
 
{% endif %}
 
  
{% if user.name == 'fred' %}
+
* <code><nowiki><HEAD></nowiki></code>
  hi fred
+
* <code><nowiki><BODY></nowiki></code>
{% endif %}
+
* <code><nowiki><SCRIPT></nowiki></code>
  
{% if user.name != 'fred' %}
+
== Comments ==
  hi non-fred
 
{% endif %}
 
  
{% if user.creditcard == null %}
+
HTML comments are automatically sanitized, so if you wish to place comments in your code, do it with a Liquid comment.
  poor sob
 
{% endif %}
 
 
 
{% if user.payments == empty %}
 
  you never paid !
 
{% endif %}
 
 
 
{% if user.age > 18 %}
 
  Login here
 
{% else %}
 
  Sorry, you are too young
 
{% endif %}
 
</pre>
 
 
 
=== Case Statement ===
 
 
 
If you need more than one condition you can use the Case Statement
 
  
 
<pre>
 
<pre>
{% case line_item.quantity %}
+
{{ # This is a comment in Liquid, and won't show up on the output }}
  {% when 0 %}
 
  none
 
  {% when 1 %}
 
  one
 
  {% when 2 %}
 
  two
 
  {% else %}
 
  a few more...
 
{% endcase %}
 
 
</pre>
 
</pre>
  
'''Example:'''
+
== Filters ==
<pre>
 
{% case template %}
 
 
{% when 'label' %}
 
    // {{ label.title }}
 
{% when 'product' %}
 
    // {{ product.vendor | link_to_vendor }} / {{ product.title }}
 
{% else %}
 
    // {{page_title}
 
{% endcase %}
 
</pre>
 
  
=== Cycle ===
+
Within the '''Output''' blocks, you can add filters that modify the results of the output block. The filters can be chained together, with the output from one filter being passed as input to the next. A filter is a small piece of code that performs some simple transformation.
  
Often you have to alternate between different colours for similar tasks.
+
When filters are chained together, 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.
Liquid has build in support for such operations using the cycle tag.
 
  
 
<pre>
 
<pre>
{% cycle 'one', 'two', 'three' %}
+
Hello {{ 'fred' | upcase }}
{% cycle 'one', 'two', 'three' %}
+
Hello fred has {{ 'fred' | length }} letters!
{% cycle 'one', 'two', 'three' %}
+
Hello {{ '*fred*' | textilize | upcase }}
{% cycle 'one', 'two', 'three' %}
+
Hello {{ 'now' | date: "%Y %h" }}
 
 
will result in
 
 
 
one
 
two
 
three
 
one
 
</pre>
 
 
 
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.
 
 
 
<pre>
 
{% 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
 
</pre>
 
 
 
=== For loops ===
 
 
 
Liquid allows for loops over collections
 
 
 
<pre>
 
  {% for item in array %}
 
    {{ item }}
 
  {% endfor %}
 
</pre>
 
 
 
During every for loop there are following helper variables available for extra styling needs:
 
<pre>
 
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 iteration?
 
</pre>
 
 
 
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.
 
 
 
<pre>
 
  # array = [1,2,3,4,5,6]
 
  {% for item in array limit:2 offset:2 %}
 
    {{ item }}
 
  {% endfor %}
 
  # results in 3,4
 
</pre>
 
 
 
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:
 
 
 
<pre>
 
  # if item.quantity is 4...
 
  {% for i in (1..item.quantity) %}
 
    {{ i }}
 
  {% endfor %}
 
  # results in 1,2,3,4
 
 
</pre>
 
</pre>
  
=== Tables ===
+
For a full description of all the Liquid filters, please refer to the [[Liquid Filter Reference]].
 
 
Liquid can create table rows and cells for you (you still need to wrap a table tag around the tablerow instruction):
 
  
 +
== Filtered Expressions ==
  
<pre>
+
Filtered expressions can also be used in expressions in the following tags
  {% tablerow item in items cols: 3 limit: 12 %}
 
    {{ item.variable }}
 
  {% endtablerow %}
 
</pre>
 
  
 +
* Assign
  
You can also find out whether a table cell is the first or last column in a row or directly query the column number:
+
Here is an example:
  
 
<pre>
 
<pre>
tablerowloop.length      # => length of the entire for loop
+
{% assign prefix = product.title | substring: 0, 3 | upcase %}
tablerowloop.index   # => index of the current iteration
 
tablerowloop.index0       # => index of the current iteration (zero based)
 
tablerowloop.rindex      # => how many items are still left?
 
tablerowloop.rindex0      # => how many items are still left? (zero based)
 
tablerowloop.first   # => is this the first iteration?
 
tablerowloop.last   # => is this the last iteration?
 
tablerowloop.col   # => index of column in the current row
 
tablerowloop.col0   # => index of column in the current row (zero based)
 
tablerowloop.col_first    # => is this the first column in the row?
 
tablerowloop.col_last    # => is this the last column in the row?
 
 
</pre>
 
</pre>
  
 +
== Further Reference ==
  
<pre>
+
* [[Liquid Tag Reference]]
  {% tablerow item in items cols: 3 %}
+
* [[Liquid Variable Reference]]
    {% if col_first %}
+
* [[Liquid Filter Reference]]
      First column: {{ item.variable }}
+
* [[Liquid Paginate Tag|Pagination ]]
    {% else %}
 
      Different column: {{ item.variable }}
 
    {% endif %}
 
  {% endtablerow %}
 
</pre>
 
 
 
=== 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:
 
 
 
<pre>
 
{% assign name = 'freestyle' %}
 
{% for t in collections.tags %}{% if t == name %}
 
  <p>Freestyle!</p>
 
{% endif %}{% endfor %}
 
</pre>
 
 
 
 
 
Another way of doing this would be to assign true/false values to the variable:
 
 
 
<pre>
 
{% assign freestyle = false %}
 
{% for t in collections.tags %}{% if t == 'freestyle' %}
 
  {% assign freestyle = true %}
 
{% endif %}{% endfor %}
 
{% if freestyle %}
 
  <p>Freestyle!</p>
 
{% endif %}
 
</pre>
 
 
 
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. Here's how it works:
 
 
 
<pre>
 
  {% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %}
 
 
 
  <label for="{{ attribute_name }}">Color:</label>
 
  <select name="attributes[{{ attribute_name }}]" id="{{ attribute_name }}">
 
    <option value="red">Red</option>
 
    <option value="green">Green</option>
 
    <option value="blue">Blue</option>
 
  </select>
 
</pre>
 

Latest revision as of 16:38, 11 January 2016

Spiffy Stores gives you 100% control over the HTML and CSS for your online storefront. Our themes use standard HTML and CSS, with dynamic tags from a templating language called "Liquid" to display dynamic data. This helps you transform your design into a dynamic e-commerce web site as quickly as possible.

This is an introduction to the Spiffy Stores Liquid template syntax, and a reference for Spiffy Stores-specific Tags and Filters.

Liquid is the templating engine for customizing your store layout. It's a small and fast template language which is quick and easy to learn but contains very powerful features for full customization.

You can use a template language such as Liquid to substitute variable data into a page layout. For example, each product page is defined by the product.liquid template. This file contains the HTML that describes the layout of the various elements on a product page. Within this template, you will find various Liquid tags and variables that help to display different data for each product.

For example, a product template will usually contain a product title, using the {{ product.title }} Liquid variable. A different title will be used for each different product that is displayed, as the Liquid variable is substituted for the actual product title.

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.

For instance if your liquid template has a product object exposed to it you can print the name of the product to the screen by referencing {{ product.title }}

Tags allow you to control the logic of templates. They are responsible for loops and branching logic such as If / Else.

Output

Here is a simple example of Output. The Liquid code contains variables that will be replaced by the actual values when the template is rendered.

Hello {{ name }}
Hello {{ user.name }}
Hello {{ 'fred' }}

For a full description of all the Liquid variables that can be used in the various templates, please refer to the Liquid Variable Reference.

Tags

For a full description on all the Liquid tags, please refer to the Liquid Tag Reference.

Acceptable Tags and Comments

All templates are sanitized, so you may not use JavaScript or tags that might be harmful to the application. Disallowed tags include, but aren’t limited to:

  • <HEAD>
  • <BODY>
  • <SCRIPT>

Comments

HTML comments are automatically sanitized, so if you wish to place comments in your code, do it with a Liquid comment.

{{ # This is a comment in Liquid, and won't show up on the output }}

Filters

Within the Output blocks, you can add filters that modify the results of the output block. The filters can be chained together, with the output from one filter being passed as input to the next. A filter is a small piece of code that performs some simple transformation.

When filters are chained together, 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 {{ 'fred' | upcase }}
Hello fred has {{ 'fred' | length }} letters!
Hello {{ '*fred*' | textilize | upcase }}
Hello {{ 'now' | date: "%Y %h" }}

For a full description of all the Liquid filters, please refer to the Liquid Filter Reference.

Filtered Expressions

Filtered expressions can also be used in expressions in the following tags

  • Assign

Here is an example:

{% assign prefix = product.title | substring: 0, 3 | upcase %}

Further Reference