Intercept the string or html code and add...

How to intercept and add strings or html code in Anqi CMS template...?

truncatecharsFilters can truncate and add..., which truncates words, specifying the length including...

truncatechars_htmlThe filter can intercept and add the html code..., which truncates the word, specifying the length including...

truncatewordsFilters can intercept strings by word and add...

truncatewords_htmlFilters can intercept and add html code by word...

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 slugTo intercept 9 characters, use the extra part... instead, you can write it like this:

{{ "Joel is a slug"|truncatechars:9 }}
# Show results
Joel i...

Sample Demo

truncatecharsFilter

{{ "Joel is a slug"|truncatechars:9 }}
{{ "Joel is a slug"|truncatechars:13 }}
{{ "Joel is a slug"|truncatechars:14 }}
{{ "Hello world"|truncatechars:1 }}
{{ "Hello world"|truncatechars:2 }}
# Show results
Joel is a ...
Joel is a slug
Hello
Hello

truncatewordsFilter

{% filter truncatewords:9 %}{% lorem 25 w %}{% endfilter %}
{% filter wordcount %}{% filter truncatewords:9 %}{% lorem 25 w %}{% endfilter %}{% endfilter %}
{{ "Hello World"|truncatewords:0 }}
{{ "Hello World"|truncatewords:1 }}
{{ "Hello World"|truncatewords:2 }}
# Show results
Lorem ipsum dolor sit amet, consectetur adipisici elit, sed ...
10

Hello world
Hello world

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>