truncatewords

将字符串缩短为作为参数传递的单词数。如果指定的单词数少于字符串中的单词数,则会在字符串末尾追加省略号(…)。

输入

{{ "Ground control to Major Tom." | truncatewords: 3 }}

输出

Ground control to...

自定义省略号

truncatewords 接受一个可选的第二个参数,该参数指定要附加到截断字符串的字符序列。默认情况下,这是省略号(…),但您可以指定不同的序列。

输入

{{ "Ground control to Major Tom." | truncatewords: 3, "--" }}

输出

Ground control to--

无省略号

您可以通过传递一个空字符串作为第二个参数来避免显示尾随字符。

输入

{{ "Ground control to Major Tom." | truncatewords: 3, "" }}

输出

Ground control to