一、什么是 Liquid

Liquid 是一款专为特定需求而打造的模板引擎。

Liquid 中有两种类型的标记:Output 和 Tag

  • Output 通常用来显示文本

    {{ 两个花括号 }}
  • Tag 通常用于执行逻辑命令
    {% 花括号加百分号 %}

shopify filter:https://shopify.dev/docs/api/liquid/filters

Liquid for Designers:https://github.com/Shopify/liquid/wiki/Liquid-for-Designers

二、Output

简单示例

Hello {{name}}
Hello {{user.name}}
Hello {{ 'tobi' }}

如果想要改变输出的结果,我们可以使用过滤器,例如将输出的字符串转为大写

Hello {{ 'tobi' | upcase }}

还可以添加多个过滤器,例如

Hello {{ '*tobi*' | textilize | upcase }}

下面说一下常用的一些过滤器

1、计算(加、减、乘、除、取余、取整等)

  • plus——加

    {{ 1 | plus:1 }} #=> 2
  • minus——减
    {{ 4 | minus:2 }} #=> 2
  • times——乘
    {{ 5 | times:4 }} #=> 20
  • divided_by——除
    {{ 10 | divided_by:3 }} #=> 3
  • modulo——取余
    {{ 3 | modulo:2 }} #=> 1
  • floor——向下取整
    {{ 4.6 | floor }} #=> 4
  • ceil——向上取整
    {{ 4.6 | ceil }} #=> 5
  • round——四舍五入
    {{ 4.5612 | round: 2 }} #=> 4.56

2、字符串处理

  • append——后拼接

    {{ 'foo' | append:'bar' }} #=> 'foobar'
  • prepend——前拼接
    {{ 'bar' | prepend:'foo' }} #=> 'foobar'
  • slice——字符串分割
    {{ "hello" | slice: -3, 3 }} #=> llo
  • split——-在匹配模式上分割字符串
    {{ "a~b" | split:"~" }} #=> ['a','b']
  • remove——删除每个出现的情况
    {{ 'foobarfoobar' | remove:'foo' }} #=> 'barbar'
  • replace——替换每个出现的地方
    {{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'
  • truncate——将字符串截断为 x 个字符。它还接受第二个参数,该参数将附加到字符串
    {{ 'foobarfoobar' | truncate: 5, '.' }} #=> 'foob.'
  • downcase—— 将输入字符串转换为小写
  • upcase- 将输入字符串转换为大写
  • strip_newlines- 从字符串中删除所有换行符 (\n)
  • strip- 去除字符串两端的所有空格
  • lstrip- 去除字符串开头的所有空格
  • rstrip- 去除字符串末尾的所有空格

3、数组处理

  • first- 获取传入数组的第一个元素
  • last- 获取传入数组的最后一个元素
  • sort- 对数组的元素进行排序
  • join- 将数组的元素与它们之间的特定字符连接起来
  • reverse- 反转传入的数组
  • uniq- 从数组中删除重复元素,可以选择使用给定属性来测试唯一性
  • map- 映射/收集给定属性的数组

4、其他

  • data——日期格式化
  • default——设置默认值
    {{ undefined_variable | default: "Default value" }} #=> "Default value"
  • size- 返回数组或字符串的大小
  • capitalize- 将输入句子中的单词大写
  • escape_once- 返回 html 的转义版本,而不影响现有的转义实体
  • escape- html 转义字符串
  • newline_to_br- 用 html 中断替换每个换行符 (\n)
  • remove_first- 删除第一次出现的情况,例如 {{ 'barbar' | remove_first:'bar' }} #=> 'bar'
  • replace_first- 替换第一个出现的地方,例如 {{ 'barbar' | replace_first:'bar','foo' }} #=> 'foobar'
  • strip_html- 从字符串中剥离 html
  • truncatewords- 将字符串截断为 x 个单词
  • url_encode- url 编码字符串

三、Tag

  • assign - Assigns some value to a variable
  • capture - Block tag that captures text into a variable
  • case - Block tag, its the standard case...when block
  • comment - Block tag, comments out the text in the block
  • cycle - Cycle is usually used within a loop to alternate between values, like colors or DOM classes.
  • for - For loop
  • break - Exits a for loop
  • continue Skips the remaining code in the current for loop and continues with the next loop
  • if - Standard if/else block
  • include - Includes another template; useful for partials
  • raw - temporarily disable tag processing to avoid syntax conflicts.
  • unless - Mirror of if statement

1、变量赋值

  • assign - 将值赋给变量

    {% assign name = 'freestyle' %}
  • capture - 用于捕获一段Liquid代码块的输出,并将其保存到一个变量中
    {% capture variable_name %}
    <!-- 这里是要捕获的内容,可以包含任意Liquid代码 -->
    {{ some_variable | filter }}
    {% if condition %}
    Some content
    {% endif %}
    {% endcapture %}

    在上面的例子中,capture标签将some_variable | filter{% if condition %} Some content {% endif %}的输出捕获到名为variable_name的变量中。

2、if/else

liquid 用以下标签来实现 if 判断:

  • {% if <CONDITION> %} ... {% 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 %}
  • {% elsif <CONDITION> %}— 可以选择在块内使用if ... endif。指定另一个条件;如果最初的“if”失败,Liquid 会尝试“elsif”,如果成功则运行模板的以下部分。您可以在一个块中使用任意数量的 elsif if
    {% if user.name == 'tobi' %}
    Hello tobi
    {% elsif user.name == 'bob' %}
    Hello bob
    {% endif %}
  • {% else %}— 可以选择在if ... endif块内的任何“elsif”标签之后使用。如果上述所有条件均失败,Liquid 将运行“else”标签后面的模板部分。
    {% if user.age > 18 %}
    Login here
    {% else %}
    Sorry, you are too young
    {% endif %}
  • {% unless <CONDITION> %} ... {% endunless %}——“if”语句的反面。不要将“elsif”或“else”与 except 语句一起使用。
    {% unless user.name == 'tobi' %}
    Hello non-tobi
    {% endunless %}

3、for 循环

{% for item in array %}
{{ item }}
{% endfor %}
{% for item in hash %}
{{ item[0] }}: {{ item[1] }}
{% endfor %}
# if item.quantity is 4...
{% for i in (1..item.quantity) %}
{{ i }}
{% endfor %}
# results in 1,2,3,4
  • 辅助变量

在每个for循环期间,以下辅助变量可满足额外的样式需求:

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?
  • 可选参数

limit:<INTEGER>让您限制获得的物品数量。

offset:<INTEGER>让您从第 n 个项目开始收集。

reversed:从最后一个到第一个迭代集合。

# array = [1,2,3,4,5,6]
{% for item in array limit:2 offset:2 %}
{{ item }}
{% endfor %}
# results in 3,4
{% for item in collection reversed %} {{item}} {% endfor %}
# items => []
{% for item in items %}
{{ item.title }}
{% else %}
There are no items!
{% endfor %}

4、case 语句

{% case condition %}
{% when 1 %}
hit 1
{% when 2 or 3 %}
hit 2 or 3
{% else %}
... else ...
{% endcase %}

END--------------------------------------

Liquid 常用语法记录的更多相关文章

  1. SQL SERVER常用语法记录

    用于记录SQL SERVER常用语法,以及内置函数. 以下语句包含: WITH 临时表语法 ROW_NUMBER()内置函数,我一般主要是用来分页.针对于查出来的所有数据做一个数字排序 分页的BETW ...

  2. MarkDown常用语法记录

    目录 1. 斜体和粗体 2. 分级标题 3. 超链接 3.1 行内式(推荐) 3.2 行外式 3.3 自动链接 4. 锚点 5. 列表 5.1无序列表 5.2有序列表 6. 引用 7. 插入图像 8. ...

  3. SQL 常用语法记录

    SQL语法 注意:SQL 对大小写不敏感 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). 数据操作语言 (DML) SQL (结构化查询语言)是用于执行查询的语 ...

  4. Markdown简介以及常用语法

    Markdown简介以及常用语法 最近发现用markdown记录东西很方便,感觉和emacs的org mode很类似,但是windows下使用emacs不是很方便.特此记录一下markdown常用的语 ...

  5. Sql常用语法以及名词解释

    Sql常用语法以及名词解释 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) D ...

  6. 2 hive的使用 + hive的常用语法

    本博文的主要内容有: .hive的常用语法 .内部表 .外部表 .内部表,被drop掉,会发生什么? .外部表,被drop掉,会发生什么? .内部表和外部表的,保存的路径在哪? .用于创建一些临时表存 ...

  7. sql 常用语法汇总

    Sql常用语法 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控 ...

  8. Hbase常用操作记录

    Hbase常用操作记录 Hbase 创建表 查看表结构 修改表结构 删除表 创建表 语法:create <table>, {NAME => <family>, VERSI ...

  9. JQuery插件之【jqGrid】常用语法整理

    jqGrid常用语法整理,包含数据获取.常用函数.触发事件等 jqGrid表格数据获取相关语法 获取表格所有数据 $("#grid").jqGrid("getRowDat ...

  10. Oracle 左连接(+)加号用法及常用语法之间的关系

    本文目的: 通过分析左连接(+)加号的写法和一些常用语法之间的联系,了解到Oracle 加号(+)的用法 分析步骤: 1.首先创建测试表的结构: create table test_left_a (a ...

随机推荐

  1. Spring框架中的设计模式(重点学习!!!)

    Spring中的设计模式 Spring框架中用到的设计模式有很多,以下是一些常见的设计模式: 依赖注入(DI)和控制反转(IoC):这是Spring框架最核心的设计模式,它允许开发人员将对象之间的依赖 ...

  2. oracle RAC redhat

    RAC比较严格,如果操作系统不纯净,容易失败: 装备第一台VM:chkconfig sendmail offchkconfig iptables offchkconfig ip6talbes offs ...

  3. 创建vue项目并搭建JSONSERVER

    1.该前提是你已经搭建好vue-cli脚手架,开始创建一个新项目,输入 vue init webpack demo(demo是自定义项目名). 2.cd demo 进入项目安装依赖 3.在已经创建的项 ...

  4. Vue之自定义过滤器

    使用Vue.filter('过滤器名称',方法); 1. <!DOCTYPE html> <html lang="en"> <head> < ...

  5. 洛谷P1990

    这是一道dp的题,好像也不算dp.需要递推,感觉能训练思维!!!很棒的一道题. 覆盖墙壁 关于这道题的分析 状态表示:f[i][0]表示前i列全部填满的所有方案,f[i][1]表示前i列全部填满缺一个 ...

  6. tailwindcss 选型,以及vue配置使用

    一.为什么选择tailwindcss? Tailwind CSS 是一个受欢迎的.功能丰富的CSS框架,它与传统的CSS框架(如Bootstrap)有些不同.以下是一些人们通常对于Tailwind C ...

  7. [Python急救站课程]太阳花的绘制

    太阳花的绘制 from turtle import * color('red', 'yellow') # 分别定义填充颜色 begin_fill() while True: forward(200) ...

  8. PostgreSQL 序列(Sequence)

    基本操作 --新增序列 CREATE SEQUENCE xxx_id_seq INCREMENT 1 -- 一次加多少 MINVALUE 1 -- 最小值 START 1 --从多少开始 CACHE ...

  9. uni-app小程序项目发布流程

    uni-app小程序项目发布流程开发工具:HbuilderX编辑器.微信小程序开发工具1.小程序开发工具就可以点击发行版本了 2.登录开发者平台配置域名白名单 在开发者设置里完成服务器域名配置(域名白 ...

  10. ClickHouse(16)ClickHouse日志引擎Log详细解析

    日志引擎系列 这些引擎是为了需要写入许多小数据量(少于一百万行)的表的场景而开发的. 这系列的引擎有: StripeLog Log TinyLog 共同属性 引擎: 数据存储在磁盘上. 写入时将数据追 ...