truncatewords
string | truncatewords: number
returns string
Truncates a string down to a given number of words.
If the specified number of words is less than the number of words in the string, then an ellipsis (...
) is appended to
the truncated string.
Caution
HTML tags are treated as words, so you should strip any HTML from truncated content. If you don't strip HTML, then closing HTML tags can be removed, which can result in unexpected behavior.
{{ article.content | strip_html | truncatewords: 15 }}
{{ article.content | strip_html | truncatewords: 15 }}
{
"article": {
"content": "<p>We've all had this problem before: we peek into the potions vault to determine which potions we are running low on, and the invisibility potion bottle looks completely empty.</p>\n<p>...</p>\n<p>Â </p>"
}
}
Output
We've all had this problem before: we peek into the potions vault to determine which...
Anchor to Specify a custom ellipsis
Specify a custom ellipsis
string | truncatewords: number, string
You can provide a second parameter to specify a custom ellipsis. If you don't want an ellipsis, then you can supply an empty string.
{{ article.content | strip_html | truncatewords: 15, '--' }}
{{ article.content | strip_html | truncatewords: 15, '' }}
{{ article.content | strip_html | truncatewords: 15, '--' }}
{{ article.content | strip_html | truncatewords: 15, '' }}
{
"article": {
"content": "<p>We've all had this problem before: we peek into the potions vault to determine which potions we are running low on, and the invisibility potion bottle looks completely empty.</p>\n<p>...</p>\n<p>Â </p>"
}
}
Output
We've all had this problem before: we peek into the potions vault to determine which--
We've all had this problem before: we peek into the potions vault to determine which
Was this page helpful?