Output
Simple output example:
Hello {{name}}Hello {{user.name}}Hello {{ 'tobi' }}
Advanced output: Filters Advanced output: Filter
Output the filter required for marking. A filter is a simple method. The first parameter is the filter input on the left of the filter, that is, the content to be filtered. The Return Value of the filter is the parameter on the left after the filter is run. If no more filters exist, the template will receive the result string.
Sample Code:
Hello {{ 'tobi' | upcase }}Hello tobi has {{ 'tobi' | size }} letters!Hello {{ '*tobi*' | textilize | upcase }}Hello {{ 'now' | date: "%Y %h" }}
Standard Filters Standard filter
date
-Time Format
capitalize
-Set a word in the input *
downcase
-Convert the input string to lowercase *
upcase
-Converts the input string to uppercase.
first
-Obtain the first element of the input array.
last
-Obtain the last element of the input array.
join
-Use an array separator to connect elements in the array
sort
-Sort elements in the array
map
-Filter elements in the array using the specified attribute
size
-Returns the size of an array or string.
escape
-Escape a string
escape_once
-Returns the HTML Escape version without affecting the existing entity escape.
strip_html
-Remove HTML from string
strip_newlines
-Remove all linefeeds (\ n) from the string
newline_to_br
-Use HTML tags to replace each line break (\ n)
replace
-Replace, for example:{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'
replace_first
-Replace the first one, for example:'{{barbar' | replace_first:'bar','foo' }} #=> 'foobar'
remove
-Delete, for example:{{'foobarfoobar' | remove:'foo' }} #=> 'barbar'
remove_first
-Delete the first one, for example:{{ 'barbar' | remove_first:'bar' }} #=> 'bar'
truncate
-Truncates the string to the x character
truncatewords
-Truncate the string to the x word
prepend
-Add a string to the front, for example:{{ 'bar' | prepend:'foo' }} #=> 'foobar'
append
-Append a string to the backend, for example:{{'foo' | append:'bar' }} #=> 'foobar'
minus
-Subtraction, for example:{{ 4 | minus:2 }} #=> 2
plus
-Addition, for example:{{'1' | plus:'1' }} #=> '11', {{ 1 | plus:1 }} #=> 2
times
-Multiplication, for example:{{ 5 | times:4 }} #=> 20
divided_by
-Division, for example:{{ 10 | divided_by:2 }} #=> 5
split
-Use a regular expression to split a string into an array. For example:{{"a~b" | split:"~" }} #=> ['a','b']
modulo
-Modulo, for example:{{ 3 | modulo:2 }} #=> 1
Mark
List of currently supported tags:
- Assign-Assign a value to a variable.
- Capture-Assign a tag text value to a variable.
- Case-Standard case... when code block
- Comment-Block Tag: comment out the text in the block.
- Cycle-Values used in loops are usually alternating, such as colors or DOM classes.
- For-For Loop
- If-Standard if/else code block
- Include-Contains another template.
- Raw-Temporarily disable tag processing to avoid syntax conflicts
- Unless-Antonym of if
Comments
Annotation is the simplest tag, which hides the marked content. For example:
We made 1 million dollars {% comment %} in losses {% endcomment %} this year.
Raw
Raw temporarily disables tag processing. This is used to generate content. It is very useful to use conflicting syntaxes. For example:
{% raw %} In Handlebars, {{ this }} will be HTML-escaped, but {{{ that }}} will not.{% endraw %}
If/Else
if / else
Statements should be well known for any other programming language. Liquid is allowedif
,unless
And optionalelsif
Andelse
For example:
{% if user %} Hello {{ user.name }}{% endif %}
# Same as above{% if user != null %} Hello {{ user.name }}{% endif %}
{% if user.name == 'tobi' %} Hello tobi{% elsif user.name == 'bob' %} Hello bob{% endif %}
{% if user.name == 'tobi' or user.name == 'bob' %} Hello tobi or bob{% endif %}
{% if user.name == 'bob' and user.age > 45 %} Hello old bob{% endif %}
{% if user.name != 'tobi' %} Hello non-tobi{% endif %}
# Same as above{% unless user.name == 'tobi' %} Hello non-tobi{% endunless %}
# Check for the size of an array{% if user.payments == empty %} you never paid !{% endif %}{% if user.payments.size > 0 %} you paid !{% endif %}
{% if user.age > 18 %} Login here{% else %} Sorry, you are too young{% endif %}
# array = 1,2,3{% if array contains 2 %} array includes 2{% endif %}
# string = 'hello world'{% if string contains 'hello' %} string includes 'hello'{% endif %}
Case Statement
If you need more conditions, you can usecase
Statement:
{% case condition %}{% when 1 %}hit 1{% when 2 or 3 %}hit 2 or 3{% else %}... else ...{% endcase %}
For example:
{% case template %}{% when 'label' %} // {{ label.title }}{% when 'product' %} // {{ product.vendor | link_to_vendor }} / {{ product.title }}{% else %} // {{page_title}}{% endcase %}
Cycle
You usually switch between different colors or similar tasks. Liquid has built-in support for such operations.cycle
Mark.
{% cycle 'one', 'two', 'three' %}{% cycle 'one', 'two', 'three' %}{% cycle 'one', 'two', 'three' %}{% cycle 'one', 'two', 'three' %}
Result:
onetwothreeone
If the name of the loop body is not provided, it is assumed that the same loop body is called multiple times with the same parameter.
If you want to fully control the loop body, you can specify the name of the loop body. 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' %}
The result is as follows:
onetwoonetwo
For loops
Liquid can be usedfor
Traverse the set.
{% for item in array %} {{ item }}{% endfor %}
When traversing a set of key-value pairs,item[0]
Is the key value,item[1]
It is the value of value.
{% for item in hash %} {{ item[0] }}: {{ item[1] }}{% endfor %}
In eachfor
In a loop, the following auxiliary variables can be used for additional requirements:
Forloop. length #=> length of the entire for loop forloop. index #=> the index forloop of the current iteration. index0 # => the index of the current iteration (starting from 0) forloop. rindex # => the remaining iterations forloop. rindex0 # => the number of remaining iterations (starting from 0) forloop. first #=> is it the first iteration? Forloop. last #=> is it the last iteration?
You can also use multiple attributes to filter the content in the loop.
limit:int
You can limit the number of projects you have.offset:int
This allows you to traverse from the nth point.
# array = [1,2,3,4,5,6]{% for item in array limit:2 offset:2 %} {{ item }}{% endfor %}# results in 3,4
Inverted Loop
{% for item in collection reversed %} {{item}} {% endfor %}
You can define a series of numbers to replace the existing set loop. The range can be defined by numbers including text and variables:
# item.quantity % i (..item.quantity) %% endfor % ,,,
Variable Assignment
You can store data in your own variables and use the data in the output or other labels at will. The simplest way to create a variable is to useassign
Tag, which has a very simple Syntax:
% assign name = %% t collections.tags %% t == name %!% endif %% endfor %}
Another way to do this is to allocatetrue / false
Value variable:
{% assign freestyle = %% t collections.tags %% t == %% assign freestyle = %% endif %% endfor %% freestyle %!% endif %}
You can usecapture
Mark. The content of this label "capture" is assigned whether it has been implemented or not. Instead of capturing content that already exists on the screen.
{% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %<label =>Color:</label>< id= name=><option value=>Red</option><option value=>Green</option><option value=>Blue</option></>