truncatewords 将字符串缩短到特定字数并在后面添加自定义省略号
将字符串缩短到指定数量的单词。
如果字符串中的单词数量超过指定数量,系统会在末尾添加省略号(…)。
输入
{{ "Ground control to Major Tom." | truncatewords: 3 }}
输出
Ground control to...
自定义省略号
truncatewords
可以接受一个可选的第二个参数,用来指定截断后要追加的字符序列。默认是省略号(…),但你可以设置成其他字符。
英文
输入
{{ "Ground control to Major Tom." | truncatewords: 3, "--" }}
输出
Ground control to--
中文+英文
在如果字符串是中文,依然会以空格作为间隔,没有空格会被视作一个词
输入
{{ "我喜欢 JekyllDo 的风格" | truncatewords: 2, "......" }}
输出
我喜欢 JekyllDo......
如果每个字都加上空格,就会每个字视为一个词(似乎对中文并不友好,如果内容是中文推荐使用 tuncate)
输入
{{ "我 喜 欢 JekyllDo 的风格" | truncatewords: 2, "......" }}
输出
我 喜......
不使用省略号
如果不想在结尾添加任何字符,可以将第二个参数设置为空字符串。
输入
{{ "Ground control to Major Tom." | truncatewords: 3, "" }}
输出
Ground control to