Truncate strings or HTML code and add...

How to truncate strings or HTML code and add... in AnQi CMS template?

truncatecharsThe filter can truncate a string and add... This method will truncate words, including the specified length...

truncatechars_htmlThe filter can truncate HTML code and add... This method will truncate words, including the specified length...

truncatewordsThe filter can split strings by words and add...

truncatewords_htmlThe filter can split html code by words and add...

Usage method

truncatecharsHow to use the filter:

{{ obj|urlencode:number }}

truncatechars_htmlHow to use the filter:

{{ obj|truncatechars_html:number }}

truncatewordsHow to use the filter:

{{ obj|truncatewords:number }}

truncatewords_htmlHow to use the filter:

{{ obj|truncatewords_html:number }}

For example, if you need to concatenateJoel is a slugTruncate 9 characters, and use ... to replace the rest, it can be written like this:

{{ "Joel is a slug"|truncatechars:9 }}
# 显示结果
Joel i...

Example Demonstration

truncatecharsFilter

{{ "Joel is a slug"|truncatechars:9 }}
{{ "Joel is a slug"|truncatechars:13 }}
{{ "Joel is a slug"|truncatechars:14 }}
{{ "你好世界"|truncatechars:1 }}
{{ "你好世界"|truncatechars:2 }}
# 显示结果
Joel i...
Joel is a ...
Joel is a slug
你
你好

truncatewordsFilter

{% filter truncatewords:9 %}{% lorem 25 w %}{% endfilter %}
{% filter wordcount %}{% filter truncatewords:9 %}{% lorem 25 w %}{% endfilter %}{% endfilter %}
{{ "你好世界"|truncatewords:0 }}
{{ "你好世界"|truncatewords:1 }}
{{ "你好世界"|truncatewords:2 }}
# 显示结果
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed ...
10

你好世界
你好世界

truncatechars_htmlFilter

{{ "This is a long test which will be cutted after some chars."|truncatechars_html:25 }}
{{ "<div class=\"foo\"><ul class=\"foo\"><li class=\"foo\"><p class=\"foo\">This is a long test which will be cutted after some chars.</p></li></ul></div>"|truncatechars_html:25 }}
{{ "<p class='test' id='foo'>This is a long test which will be cutted after some chars.</p>"|truncatechars_html:25 }}
{{ "<a name='link'><p>This </a>is a long test which will be cutted after some chars.</p>"|truncatechars_html:25 }}
{{ "<p>This </a>is a long test which will be cutted after some chars.</p>"|truncatechars_html:25 }}
{{ "<p>This is a long test which will be cutted after some chars.</p>"|truncatechars_html:7 }}
# 显示结果
This is a long test wh...
<div class="foo"><ul class="foo"><li class="foo"><p class="foo">This is a long test wh...</p></li></ul></div>
<p class='test' id='foo'>This is a long test wh...</p>
<a name='link'><p>This </a>is a long test wh...</p>
<p>This </a>is a long test wh...</p>
<p>This...</p>

truncatewords_htmlFilter

{{ "This is a long test which will be cutted after some words."|truncatewords_html:25|safe }}
{{ "<div class=\"foo\"><ul class=\"foo\"><li class=\"foo\"><p class=\"foo\">This is a long test which will be cutted after some chars.</p></li></ul></div>"|truncatewords_html:5 }}
{{ "<p>This. is. a. long test. Test test, test.</p>"|truncatewords_html:8 }}
{{ "<a name='link' href=\"https://....\"><p class=\"foo\">This </a>is a long test, which will be cutted after some words.</p>"|truncatewords_html:5 }}
{{ "<p>This </a>is a long test, which will be cutted after some words.</p>"|truncatewords_html:5 }}
{{ "<p>This is a long test which will be cutted after some words.</p>"|truncatewords_html:2 }}
{{ "<p>This is a long test which will be cutted after some words.</p>"|truncatewords_html:0 }}
# 显示结果
This is a long test which will be cutted after some words.
<div class="foo"><ul class="foo"><li class="foo"><p class="foo">This is a long test ...</p></li></ul></div>
<p>This. is. a. long test. Test test, test....</p>
<a name='link' href="https://...."><p class="foo">This </a>is a long test,...</p>
<p>This </a>is a long test,...</p>
<p>This is ...</p>