Intercept the string or html code and add...

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

truncatecharsThe filter can truncate and add strings ..., this method will truncate words, including the specified length ....

truncatechars_htmlThe filter can截取and add to html code... This method will截断words, including the specified length...

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

truncatewords_htmlThe filter can split HTML code into words and add...

How to use

truncatecharsHow to use filters:

{{ obj|urlencode:number }}

truncatechars_htmlHow to use filters:

{{ obj|truncatechars_html:number }}

truncatewordsHow to use filters:

{{ obj|truncatewords:number }}

truncatewords_htmlHow to use filters:

{{ obj|truncatewords_html:number }}

For example, it is necessary toJoel is a slugExtract 9 characters, and use an ellipsis (...) for the remainder, it can be written like this:

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

Sample Demo

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>