Liquid学习笔记

Liquid中有2种Markups:

  • Output

    Hello {{ title }}

  • Tag 非文本 {\% 对称的大括号加百分号 \%}

输出Output

简单的输出如

Hello 

另外输出还可以使用过滤器 Filter,如:

Hello TOBI
Hello tobi has 4 letters!
Helo 2013 May

标准过滤器 Standard Filters

标准的Filter, 通过名字基本可以猜得出来,filter的用法类似Linux Pipeline,左边是右边的输入;对于Filter可以有输入参数。 例如plus,将左边数与右边数连接或相加, plus:1, 就代表左边数加1。

- date
- capitalize
- downcase
- upcase
- first
- last
- join
- sort
- map
- size
- escape
- escape_once
- strip_html
- strip_newlines
- newline_to_br
- replace
- replace_first
- remove
- remove_first
- truncate
- truncatewords
- prepend
- append
- minus
- plus
- times
- divided_by
- split
- modulo

Example:

{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'
{{ 'barbar' | replace_first:'bar','foo' }} #=> 'foobar'
{{ 'foobarfoobar' | remove:'foo' }} #=> 'barbar'
{{ 'barbar' | remove_first:'bar' }} #=> 'bar'
{{ 'bar' | prepend:'foo' }} #=> 'foobar'
{{ 'foo' | append:'bar' }} #=> 'foobar'
{{ 4 | minus:2 }} #=> 2
{{ '1' | plus:'1' }} #=> '11', {{ 1 | plus:1 }} #=> 2
{{ 5 | times:4 }} #=> 20
{{ 10 | divided_by:2 }} #=> 5
{{ "a~b" | split:~ }} #=> ['a','b']
{{ 3 | modulo:2 }} #=> 1

标签 Tags

Tags是用来表达模板中的页面逻辑。 当前支持的tag包含了: - assign 变量赋值

{% assign var = value %}
  • capture Block tag that captures text into a variable (还没理解。。。)
  • case 类似switch
  • comment 注释
  • cycle 在一组备选值当中循环
  • for 循环
  • if if/else
  • include 包含另外的模板
  • raw 临时停止处理markup
  • unless 条件判断

Case

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

Cycle

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

will result in

one
two
three
one

Comments

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

Raw

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

If / else

{% if user %}
  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 if the user has a credit card
{% if user.creditcard != null %}
   poor sob
{% endif %}

# Same as above
{% if user.creditcard %}
   poor sob
{% endif %}

# Check for an empty array
{% if user.payments == empty %}
   you never 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 %}

For loops

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

在for循环中还包含了很多辅助变量

-   forloop.length # => for的循环总次数
-   forloop.index  # => 当前循环的index,从1开始
-   forloop.index0 # => 当前循环的index,从0开始
-   forloop.rindex # => 当前循环剩余次数
-   forloop.rindex0 # => 当前循环剩余次数,从0开始
-   forloop.first   # => 是否是循环的第一次
-   forloop.last    #=> 是否是循环的最后一次

通过offset:int 控制循环的起始, limit: int控制循环的次数, 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 %}

Python Range重现~

# if item.quantity is 4...
{% for i in (1..item.quantity) %}
  {{ i }}
{% endfor %}
# results in 1,2,3,4
展开阅读全文

New Liquid

08-15

DescriptionnnProfessor W invents a new kind of liquid. The damping of that liquid is high and the density of it is low. To test whether the liquid is qualified, Professor W wants to do an experiment. He pours the liquid into a container, of which the bottom slopes up in an angle of α。Prof. W places a ball at the lower end of the bottom, and launches it at a speed of V cm per second. Once the ball touches the bottom, it stops immediately. Fig.1 shows the experiment in the container. When the ball is moving in the liquid, the liquid applies an acceleration of - fV to the ball(f is a constant). Because the density of the liquid is very low, you can ignore the buoyancy(The upward force of a liquid is called buoyancy). Now it is the problem: how to choose the launch angle θ (with horizon), to make the ball go as far as possible (ie. the distance between the start point and the stop point as large as possible)? You should help Professor W solve this problem. nnnn![](http://poj.org/images/3933_1.png)nFig. 1nnnThe container is large and deep enough, so the ball will never hit the side of the container or get out of the liquid. nInputnnInput consists of T (1 <= T <= 20) test cases. Each test case is one line including three integers: α for slope angle, V for ball speed and f for the constant. (0 <= α < 90, 0 < V < 50, 0 < f < 1000). nOutputnnFor each test case, output a single line containing an integer θ: the best angle. Don’t make the answers too accurate, integers are good enough. nSample Inputnn34 44 41n64 11 500n52 8 478n65 22 464nSample Outputnn53n70n62n69 问答

没有更多推荐了,返回首页