一、什么是 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. Redis 命令工具

    --- Redis 命令工具 --- redis-server Redis 服务器启动命令 redis-cli shutdown 停止服务 redis-benchmark:性能测试工具,用于检测 Re ...

  2. Python面向对象——封装

    文章目录 内容回顾 封装 为何要隐藏? 作业 内容回顾 上节课复习: 1.编程范式/思想 面向过程 介绍: 核心是"过程"二字 过程就是"流水线" 过程终极奥义 ...

  3. Android利用广播监听按下HOME和电源键

    package cc.testhome; import cc.testhome.HomeKeyObserver.OnHomeKeyListener; import cc.testhome.PowerK ...

  4. ChatGPT 是如何产生心智的?

    一.前言 - ChatGPT真的产生心智了吗? 来自斯坦福大学的最新研究结论,一经发出就造成了学术圈的轰动,"原本认为是人类独有的心智理论(Theory of Mind,ToM),已经出现在 ...

  5. 今天的第二道tarjan:受欢迎的牛

    原题来自:USACO 2003 Fall 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的"喜欢&quo ...

  6. 可观测性数据收集集大成者 Vector 介绍

    如果企业提供 IT 在线服务,那么可观测性能力是必不可少的."可观测性" 这个词近来也越发火爆,不懂 "可观测性" 都不好意思出门了.但是可观测性能力的构建却着 ...

  7. CF1336A

    题目简化和分析: 明确一点这是一棵树. 为了保证每个工业城市的设置效益最大,应该设在最深的节点. 从深到浅,可以使用优先队列去实现. 设置一个的价值为 \(dep_u-siz_u-1\). 关于作者一 ...

  8. mybatis 操作 mysql 大批量插入,数据分页处理

    /** * 大批量插入,数据分页处理 * * @param tableName 临时表名称 * @param mapHead 临时表属性 Map,key = 临时表字段属性, value = 具体值. ...

  9. Java SPI机制总结系列之万字详细图解SPI源码分析

    原创/朱季谦 我在<Java SPI机制总结系列之开发入门实例>一文当中,分享了Java SPI的玩法,但是这只是基于表面的应用.若要明白其中的原理实现,还需深入到底层源码,分析一番. 这 ...

  10. 手撕Vuex-安装模块数据

    前言 根据上一篇,[手写Vuex]-提取模块信息,我们已经可以获取到模块的信息了,将模块信息变成了我们想要的数据结构,接下来我们就要根据模块的信息,来安装模块的数据. 在上一篇当中我们定义了一个 Mo ...