Output输出

简单输出示例:

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

Advanced output: Filters 高级输出:过滤器

输出标记需要的过滤器。过滤器是简单的方法。第一个参数在过滤器的左侧就是过滤器的输入,即需要过滤的内容。过滤器的返回值将是过滤器运行时过滤后的左侧的参数。当没有更多的过滤器,模板会收到结果字符串。

代码示例:

Hello {{ 'tobi' | upcase }}
Hello tobi has {{ 'tobi' | size }} letters!
Hello {{ '*tobi*' | textilize | upcase }}
Hello {{ 'now' | date: "%Y %h" }}

Standard Filters标准过滤器

  • date -时间格式化
  • capitalize-设置输入中的某个单词*
  • downcase-将输入的字符串转换为小写*
  • upcase-将输入的字符串转换为大写
  • first-获得传入的数组的第一个元素
  • last-获得传入的数组的最后一个元素
  • join-用数组的分隔符连接数组中的元素
  • sort-数组中的元素排序
  • map-通过指定的属性过滤数组中的元素
  • size-返回一个数组或字符串的大小
  • escape-转义一个字符串
  • escape_once-返回HTML的转义版本,而不会影响现有的实体转义
  • strip_html-从字符串去除HTML
  • strip_newlines -从字符串中去除所有换行符(\ n)的
  • newline_to_br-用HTML标记替换每个换行符(\ n)
  • replace-替换,例如:{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'
  • replace_first-替换第一个,例如: '{{barbar' | replace_first:'bar','foo' }} #=> 'foobar'
  • remove-删除,例如:{{'foobarfoobar' | remove:'foo' }} #=> 'barbar'
  • remove_first-删除第一个,例如:{{ 'barbar' | remove_first:'bar' }} #=> 'bar'
  • truncate-截取字符串到第x个字符
  • truncatewords-截取字符串到第x个词
  • prepend-前置添加字符串,例如:{{ 'bar' | prepend:'foo' }} #=> 'foobar'
  • append-后置追加字符串,例如:{{'foo' | append:'bar' }} #=> 'foobar'
  • minus-减法,例如:{{ 4 | minus:2 }} #=> 2
  • plus-加法,例如:{{'1' | plus:'1' }} #=> '11', {{ 1 | plus:1 }} #=> 2
  • times-乘法,例如:{{ 5 | times:4 }} #=> 20
  • divided_by-除法,例如:{{ 10 | divided_by:2 }} #=> 5
  • split-通过正则表达式切分字符串为数组,例如:{{"a~b" | split:"~" }} #=> ['a','b']
  • modulo-取模,例如:{{ 3 | modulo:2 }} #=> 1

标记

目前支持的标记的列表:

  • assign -将某个值赋给一个变量
  • capture-标记文本赋值给一个变量
  • case-标准的case...when代码块
  • comment-块标记,注释掉该块中的文本
  • cycle-通常在循环中使用的值之间交替,如颜色或DOM类。
  • for-for循环
  • if-标准的if/else代码块
  • include -包含另外一个模版
  • raw-暂时停用标签处理以避免出现语法冲突
  • unless-if的反义词

Comments

注释是最简单的标签,它会隐藏标记的内容。例如:

We made 1 million dollars {% comment %} in losses {% endcomment %} this year.

Raw

Raw暂时禁用标签处理。这是用于生成内容,它使用相互矛盾的语法非常有用。例如:

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

If / Else

if / else语句对任何其他编程语言都应该是众所周知的。Liquid允许使用if,unless,以及可选的elsifelse,例如:

{% 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

如果您需要更多的条件,您可以使用case语句:

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

例如:

{% case template %}
{% when 'label' %}
// {{ label.title }}
{% when 'product' %}
// {{ product.vendor | link_to_vendor }} / {{ product.title }}
{% else %}
// {{page_title}}
{% endcase %}

Cycle

通常你有不同的颜色或类似的任务之间切换。 Liquid已经内置了对此类操作的支持,使用cycle标记。

{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}

结果为:

one
two
three
one

如果未提供循环体的名称,那么它假设用同样的参数多次调用同一个循环体。

如果你想完全控制循环体,您可以选择指定循环体的名称。这甚至可以是一个变量。

{% cycle 'group 1': 'one', 'two', 'three' %}
{% cycle 'group 1': 'one', 'two', 'three' %}
{% cycle 'group 2': 'one', 'two', 'three' %}
{% cycle 'group 2': 'one', 'two', 'three' %}

得到结果为:

one
two
one
two

For loops

Liquid 可以使用for遍历集合。

{% for item in array %}
{{ item }}
{% endfor %}

当遍历一个键值对集合时,item[0]是key的值,item[1]则是value的值。

{% for item in hash %}
{{ item[0] }}: {{ item[1] }}
{% endfor %}

在每次for循环中,下面的辅助变量可用于额外的需求:

forloop.length      # => 整个for循环的长度
forloop.index # => 当前迭代的索引
forloop.index0 # => 当前迭代的索引(从0开始)
forloop.rindex # => 剩余的迭代次数
forloop.rindex0 # => 剩余的迭代次数(从0开始)
forloop.first # => 是否是第一次迭代?
forloop.last # => 是否是最后一次迭代?

你还可以使用多个属性来过滤循环中的内容。

limit:int可以限制你有多少个项目获得 offset:int可以让你从第n项开始遍历。

# 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 %}

你可以通过定义一系列的数字来代替现有的集合循环。范围可以通过包括文本和变量的数字来定义:

# if item.quantity is ...
{% for i in (..item.quantity) %}
{{ i }}
{% endfor %}
# results in ,,,

Variable Assignment

您可以将数据存储在自己的变量,在输出或其他标记随意使用。创建一个变量最简单的方法是使用assign标签,它有一个非常简单的语法:

{% assign name = 'freestyle' %}

{% for t in collections.tags %}
{% if t == name %}
Freestyle!
{% endif %}
{% endfor %}

这样做的另一种方法是将分配true / false值的变量:

{% assign freestyle = false %}

{% for t in collections.tags %}
{% if t == 'freestyle' %}
{% assign freestyle = true %}
{% endif %}
{% endfor %} {% if freestyle %}
Freestyle!
{% endif %}
如果你想将多个字符串合并成一个单一的字符串,并将其保存到变量中,你可以使用capture标记。这个标签“捕获”内容无论它是否已经实现,然后分配捕获的值。而不是只能捕获屏幕上已经存在的内容。
{% capture attribute_name %}{{ item.title | handleize }}-{{ i }}-color{% endcapture %}

<label for="{{ attribute_name }}">Color:</label>

<select id="{{ attribute_name }}" name="attributes[{{ attribute_name }}]">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>

Liquid基础语法的更多相关文章

  1. Swift与C#的基础语法比较

    背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...

  2. iOS-----正则表达式的基础语法

    正则表达式简单语法总结 一.什么是正则表达式 从概念上来说,正则表达式也是一门小巧而精炼的语言,它可以用来简化检索特定的字符串,替换特定字符等功能,有许多开发语言工具,都内嵌支持正则表达式.那么一个正 ...

  3. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  4. emmet 系列(1)基础语法

    emmet 系列(1)基础语法 emmet 是一个能显著提升开发html和css开发效率的web开发者工具 emmet基本上目前已知的编辑器都有相应的插件,各个编辑器的emmet插件的下载地址:点我下 ...

  5. Scala基础语法 (一)

    如果你之前是一名 Java 程序员,并了解 Java 语言的基础知识,那么你能很快学会 Scala 的基础语法. Scala 与 Java 的最大区别是:Scala 语句末尾的分号 ; 是可选的. 我 ...

  6. Java基础语法

    java基础学习总结——基础语法1 一.标识符

  7. javascript中正则表达式的基础语法

    × 目录 [1]定义 [2]特点 [3]元字符[4]转义字符[5]字符组[6]量词[7]括号[8]选择[9]断言[10]模式[11]优先级[12]局限性 前面的话 正则表达式在人们的印象中可能是一堆无 ...

  8. Swift基础语法学习总结(转)

    Swift基础语法学习总结 1.基础  1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...

  9. 黑马程序员——OC语言基础语法 面向对象的思想

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结)(一)基础语法 1)关键字 @interface.@implementati ...

随机推荐

  1. Gym 100500B

    题目给了四个轮子,每个轮子上有按顺序排列的n个数,要求适当旋转每个轮子,使得四个轮子相同行数相加和相同. 首先,可以计算出每一行的和应该是多少,记为Sum.然后固定第一个轮子,二重循环枚举2.3轮子, ...

  2. create thread的时候发生core dump

    #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h& ...

  3. 转 C# 装箱和拆箱[整理]

    1.      装箱和拆箱是一个抽象的概念 2.      装箱是将值类型转换为引用类型 :拆箱是将引用类型转换为值类型       利用装箱和拆箱功能,可通过允许值类型的任何值与Object 类型的 ...

  4. css3实现轮播

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Spring整合Hibernate。。。。

    环境搭建,在eclipse中导入spring和hibernate框架的插件,和导入所有使用到的架包 首先,hibernate的创建: 建立两个封装类,其中封装了数据库中表的属性,这儿只写属性,gett ...

  6. zigbee学习之路(一):zigbee介绍

    一.前言 大家好,我是一名在校的大学生,最近对zigbee非常感兴趣,于是自己从网上买了一款秉火cc2530的zigbee开发板,想通过这个平台来和大家分享自己学习和研究的经历,下面就来简单的介绍下z ...

  7. asp.net 微信支付 错误解决方案

    asp.net 微信支付 错误解决方案 在网上看到有人解决方案为: 解决方法 出现这种错误网上查出现有的原因是: 订阅号没有相关的权限 账号没有认证,没有相关的权限 那么这里遇到问题两种都不是.开发账 ...

  8. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

  9. URL地址传参乱码

    1.页面使用javascript的方法encodeURIComponent对需要转码的字符进行两次转码,如:encodeURIComponent(encodeURIComponent("** ...

  10. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...