Skip to content

truncatewords

把文本截断到词数上限。

语法

knap
{{ value | truncatewords:20 }}
{{ value | truncatewords:(20, "...") }}

用法

  • 必填的第一个参数是非负的词数上限。
  • 后缀默认为 。传入第二个参数可以替换它,或传 "" 以省略它。
  • 后缀会追加在指定数量的词之后。
  • 保留下来的文本中的空白会被保留。
  • 对于数组和对象(包括来自其他过滤器、已序列化的集合),其中的字符串取值会被递归格式化,而键和非字符串取值保持不变。

示例

默认后缀

Data
json
{
  "text": "A concise introduction to Knap"
}
Template
knap
{{ text | truncatewords:3 }}
Output
md
A concise introduction…

无后缀

Data
json
{
  "text": "A concise introduction to Knap"
}
Template
knap
{{ text | truncatewords:(3, "") }}
Output
md
A concise introduction