truncatewords

文字列を、引数として渡した語数に短縮します。指定した語数が文字列内の語数よりも少ない場合、文字列に省略記号(...)が付加されます。

入力

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

出力

Ground control to...

カスタム省略記号

truncatewords は、切詰められた文字列に付加する文字のシーケンスを指定する、任意の 2 番目の引数を取ります。デフォルトではこれは省略記号(...)ですが、別のシーケンスを指定できます。

入力

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

出力

Ground control to--

省略記号なし

2 番目の引数として空白文字を渡すことにより、末尾の文字を表示しないようにできます。

入力

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

出力

Ground control to