Liquid模板语言构建Web页面-学习速查笔记

2019-04-17  本文已影响0人  老哥深蓝

基础知识

入门

Liquid是安全、面向客户的模板语言,用于构建灵活的 web 应用。
由 Shopify 创造并用 Ruby 实现。它是 Shopify主题的骨骼,并且被用于加载店铺系统的动态内容。
GitHub:https://github.com/Shopify/liquid

对象(Object)

告诉 Liquid 在页面的哪个位置展示内容,对象和变量使用{{ }}标识。

标记(Tag)

标记创建模板的逻辑和控制流,使用{% %}标识。

过滤器(Filter)

改变Liquid对象的输出,通过|分隔。

{% comment %}对象{% endcomment %}
{{ page.Title }}

{% comment %}标记{% endcomment %}
{% if page %}
  Hello {{ page.Title }} !
{% endif %}

{% comment %}过滤器{% endcomment %}
{{ "adam!" | capitalize | prepend: "Hello " }}

操作符

== 相等 != 不相等 > 大于 < 小于 >= 大于或等于 <= 小于或等于 or 逻辑或 and 逻辑与

真值(Truthy)与假值(falsy)

数据类型

{% assign my_string = "Hello World!" %}
{% assign my_int = 25 %}
{% assign my_float = 39.756 %}
{% assign foo = true %}

控制输出的空行

使用{{- -}}、{%- -%},删除空行

Tags

判断语句

{% if page.Title=="1" %}
    //条件成立输出内容
    {% elsif page.Title="2" %}
    //输出内容
    {% else %}
    //输出内容
{% endif %}

{% unless page.Title=="1"%}
    //条件不成立输出内容
{% endunless %}

{% assign handle = 'cake' %}
{% case handle %}
  {% when 'cake' %}
     This is a cake
  {% when 'cookie' %}
     This is a cookie
  {% else %}
     This is not a cake nor a cookie
{% endcase %}

循环

{% for product in collection.products %}
    {{ product.title }}
{% endfor %}

{% for i in (1..5) %}
  {% if i == 4 %}
    {% break %}
  {% else %}
    {{ i }}
  {% endif %}
{% endfor %}

{% for i in (1..5) %}
  {% if i == 4 %}
    {% continue %}
  {% else %}
    {{ i }}
  {% endif %}
{% endfor %}
//cols指定列数
{% tablerow product in collection.products cols:2 %}
  {{ product.title }}
{% endtablerow %}

禁止处理内容

raw 标记临时禁止处理其所包围的代码。如果输出的内容与 Liquid 模板语言有冲突时,可以避免冲突。

{% raw %}
  In Handlebars, {{ this }} will be HTML-escaped, but
  {{{ that }}} will not.
{% endraw %}

变量

{% assign my_variable = false %}

{% capture about_me %}
I am {{ age }} and my favorite food is {{ favorite_food }}.
{% endcapture %}

{% increment my_counter %}
{% increment my_counter %}
{% increment my_counter %}

Filter速查

abs

append

at_least

at_most

capitalize

ceil

compact

concat

{% assign furniture = "chairs, tables, shelves" | split: ", " %}
{% assign everything = fruits | concat: vegetables | concat: furniture %}
{% for item in everything %}
- {{ item }}
{% endfor %}

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

{{ "Ground control to Major Tom." | size }}
//输出28
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array | size }}
//输出4
//使用“点标记”:
{% if site.pages.size > 10 %}
  This is a big website!
{% endif %}

slice

sort

sort_natural

split

strip

strip_html

strip_newlines

times

truncate

truncatewords

uniq

upcase

url_decode

url_encode

where

All products:
{% for product in products %}
- {{ product.title }}
{% endfor %} 

{% assign kitchen_products = products | where: "type", "kitchen" %}

Kitchen products:
{% for product in kitchen_products %}
- {{ product.title }}
{% endfor %} 
上一篇 下一篇

猜你喜欢

热点阅读