POST
Liquid grammar
I recently used Liquid language to design my blog website. The Liquid is an open-source template language written in Ruby. This article is a short study note of Liquid and presented some main knowledge.
My blog website was constructed by Jekyll, Jekyll uses Liquid as its template language and adds many objects, tags, and filters. The new content includes objects representing content pages, tags to introduce content fragments into the page, and filters to manipulate strings and URLs.
I do not know this language previously, so I spent some time learning it.
The Liquid code consists of objects
, tags
, and filters
.
objects
Object tells Liquid where to display content on the page. Object and variable names are identified by double curly braces: {{ }}
{{post.name}}
tags
Tags create the logic and control flow of templates. They are identified by a single bracket with a percent sign: {%
and %}
.
Tags do not produce any visible text output. This means that you can use them to assign values to variables, create conditions and loop logic, and do not display any Liquid logic code on the page.
Tags are divided into three categories:
filters
The filter changes the output of the Liquid object. They are used for output, separated by a |
symbol.
The keywords include :
abs
append
at_least
at_most
capitalize
ceil
compact
concat
date
default
divided_by
downcase
escape
escape_once
first
floor
join
last
lstrip
map
minus
modulo
newline_to_br
plus
prepend
remove
remove_first
replace
replace_first
reverse
round
rstrip
size
slice
sort
sort_natural
split
strip
strip_html
strip_newlines
times
truncate
truncatewords
uniq
upcase
url_decode
url_encode
Operator
Except the 3 basic elements, Liquid contains the logical and comparison operators.
They are
"=",">","<",">=","<=","!=","or","and".
The syntax is similar to other languages, like C
.
Object Type
There are five types of Liquid objects.
- String
- Number
- Boolean
- Nil
- Array
Actually, except Nil
(Nil type represents a null object.), others you can find the same type in other languages (for example C), the usage is quite similar.
Liquid in Jekyll
In Jekyll, Jekyll adds a few handy filters and tags of its own.
Below are some of useful tags and filters, which I have used on my blog website.
filters
- relative_url
- date_to_string
- group_by
- where
- number_of_words
tags
{% highlight ruby linenos %}
{% endhighlight %}
This tag supports syntax highlighting of over 100 languages, in addition, it can also output the line numbers of the code. Check this page Rouge wiki to find the appropriate identifier to use for the language you want to highlight.{% link url.md %}
The link tag will generate the correct permalink URL for the path you specify, you must include the file’s original extension when using the link tag. This tag can also be used to create a link in Markdown.{% post_url post_name %}
The post_url tag will generate the correct permalink URL for the post you specify. Unlike{% link %}
tag, there is no need to include the file extension when using thepost_url
tag. This tag can also be used on Markdown.
The Jekeyll page gives more details.
The above elements constitute the main Liquid grammar, I am trying to avoid writing an excessively long article, so I decided to quit here. For more content, you can find it at Liquid website.