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