Extract strings or HTML code and add...

How to cut 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 truncate words, including... in the specified length.

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

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

Usage Instructions

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 splitJoel is a slugExtract 9 characters, and use an ellipsis (...) for the remaining part, it can be written like this:

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

Here is an 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>