mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-13 11:20:40 -07:00
Compare commits
1
Commits
master
...
docs-operators
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af12665c5c |
@@ -9,6 +9,52 @@ LiquidJS operators are very simple and different. There're 2 types of operators
|
||||
|
||||
Thus numerical operators are not supported and you cannot even plus two numbers like this `{% raw %}{{a + b}}{% endraw %}`, instead we need a filter `{% raw %}{{ a | plus: b}}{% endraw %}`. Actually `+` is a valid variable name in LiquidJS.
|
||||
|
||||
## Logic Operators
|
||||
|
||||
### not
|
||||
|
||||
Negates a condition. Returns `true` if the condition is false, and `false` if the condition is true.
|
||||
|
||||
Input
|
||||
```liquid
|
||||
{% if not user.active %}
|
||||
User is inactive
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
### and
|
||||
|
||||
Returns `true` if both conditions are true.
|
||||
|
||||
Input
|
||||
```liquid
|
||||
{% if user.age >= 18 and user.verified %}
|
||||
Access granted
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
### or
|
||||
|
||||
Returns `true` if at least one condition is true.
|
||||
|
||||
Input
|
||||
```liquid
|
||||
{% if user.isAdmin or user.isModerator %}
|
||||
You have elevated privileges
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
### contains
|
||||
|
||||
Checks if a string contains a substring, or if an array contains an element.
|
||||
|
||||
Input
|
||||
```liquid
|
||||
{% if product.title contains "Pack" %}
|
||||
This is a pack
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
## Precedence
|
||||
|
||||
1. Comparison operators, and `contains`. All comparison operators alongside `contains` have the same (highest) precedence.
|
||||
|
||||
@@ -5,14 +5,61 @@ title: 运算符
|
||||
LiquidJS 运算符非常简单也很特别,只支持两类运算符:
|
||||
|
||||
* 比较运算符:`==`, `!=`, `>`, `<`, `>=`, `<=`
|
||||
* 逻辑运算符:`or`, `and`, `contains`
|
||||
* 逻辑运算符:`not`, `or`, `and`, `contains`
|
||||
|
||||
因此普通的数学运算是不支持的,比如 `{% raw %}{{a + b}}{% endraw %}`。它的替代方案是过滤器 `{% raw %}{{ a | plus: b}}{% endraw %}`。事实上 `+` 在 LiquidJS 中是一个合法的变量名。
|
||||
|
||||
## 逻辑运算符
|
||||
|
||||
### not
|
||||
|
||||
对条件取反。如果条件为假则返回 `true`,如果条件为真则返回 `false`。
|
||||
|
||||
输入
|
||||
```liquid
|
||||
{% if not user.active %}
|
||||
用户未激活
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
### and
|
||||
|
||||
当两个条件都为真时返回 `true`。
|
||||
|
||||
输入
|
||||
```liquid
|
||||
{% if user.age >= 18 and user.verified %}
|
||||
允许访问
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
### or
|
||||
|
||||
当至少一个条件为真时返回 `true`。
|
||||
|
||||
输入
|
||||
```liquid
|
||||
{% if user.isAdmin or user.isModerator %}
|
||||
您拥有提升的权限
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
### contains
|
||||
|
||||
检查字符串是否包含子字符串,或数组是否包含元素。
|
||||
|
||||
输入
|
||||
```liquid
|
||||
{% if product.title contains "Pack" %}
|
||||
这是一个套装
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
## 优先级
|
||||
|
||||
1. 比较运算符。所有比较运算符具有同样的优先级,且高于逻辑运算符。
|
||||
2. 逻辑运算符。所有逻辑运算符具有同样的有衔接。
|
||||
1. 比较运算符和 `contains`。所有比较运算符和 `contains` 具有同样的(最高)优先级。
|
||||
2. `not` 运算符。它的优先级略高于 `or` 和 `and`。
|
||||
3. `or` 和 `and` 运算符。这些逻辑运算符具有同样的(最低)优先级。
|
||||
|
||||
## 结合性
|
||||
|
||||
|
||||
Reference in New Issue
Block a user