AnQiCMS with its flexible and powerful template system, makes the presentation and data processing of website content very efficient.During template development, skillfully combining various filters often unlocks unexpected practical effects.Today, let's delve deeply into two seemingly basic filters that can work wonderfully when combined:wordcountandadd.
wordcountFilter: Intelligent insight into text length
First, let's briefly review.wordcountFilter. As the name implies, its core function is to count the number of words in a segment of text.In the AnQiCMS template environment, regardless of whether this text is English or Chinese,wordcountThe number of words is identified and counted based on spaces (or word boundaries in the Chinese context, where a single Chinese character is also considered a 'word'), and the final result is returned as an integer.
This filter is very useful in many scenarios. For example, you may want to display an estimated reading time on the article detail page, or quickly understand the word count of a paragraph in content management, or even in some SEO optimization strategies, to control and count text length.
Its basic usage is very intuitive, usually like this:
<p>文章内容字数:{{ article.Content|wordcount }} 字</p>
Whenarticle.ContentWhen containing text like "Welcome to AnQiCMS (AnQiCMS)"wordcountIt will return 2 (each of "Welcome to AnQiCMS" and "AnQiCMS" counts as one word), or if it is a long paragraph, it will return the specific word count.
addFilter: Flexible number and string processing
Next isaddFilter. It is a multifunctional tool that can perform arithmetic operations on numbers as well as concatenate strings.This means you can use it to add numbers or dynamically combine text, such as appending specific information after a description.
addOne of the major advantages of the filter is its flexibility in handling different data types.When you add numbers to numbers, it performs mathematical addition;When you add a string to another string, it will concatenate.Even when adding numbers and strings together, AnQiCMS will try to perform intelligent conversion to ensure the smooth operation of the operation.If a conversion cannot be made, it will elegantly ignore the unprocessable parts to avoid template rendering interruption.
Example of adding numbers:
<p>结果:{{ 5|add:2 }}</p>
The output will be7.
Example of string concatenation:
<p>组合文本:{{ "安企"|add:"CMS" }}</p>
The output will be安企CMS.
Skillfully combined:wordcountwithaddPractical effects
Now, let's see whenwordcountandaddWhen these two filters work together, what kind of sparks can be ignited.
Imagine a scenario: You are managing a blog or news website and you want to display the total number of reads (word count) for each article on the article list page or in the sidebar of the article detail page.But this 'total reading volume' is not just the number of characters in the main text, you also want to add the number of characters in the article title, even a fixed value for consistency, like 50 characters, as the estimated word count for the article summary, so as to give readers a more comprehensive reading volume reference.
At this time,wordcountandaddThe combination is particularly efficient. We can first usewordcountto get the word count of different parts, then useaddAdd them up.
Here is a specific code example showing how to implement this feature in the AnQiCMS template.
{# 假设我们正在一个文章详情页,且 article 变量代表当前文章对象 #}
{% set articleContentWords = article.Content|wordcount %}
{% set articleTitleWords = article.Title|wordcount %}
{% set fixedSummaryWords = 50 %} {# 假设摘要固定为50字 #}
<div class="article-meta">
<p>文章正文字数:{{ articleContentWords }} 字</p>
<p>文章标题字数:{{ articleTitleWords }} 字</p>
<p>预估摘要字数:{{ fixedSummaryWords }} 字</p>
{# 结合 wordcount 和 add 过滤器计算文章总阅读字数 #}
<p>
<strong>文章总阅读字数:
{{ articleContentWords|add:articleTitleWords|add:fixedSummaryWords }} 字
</strong>
</p>
{# 还可以将这些信息组合成一个动态的文本标签 #}
<p>
预计阅读时长:
{{ ((articleContentWords|add:articleTitleWords|add:fixedSummaryWords)|divisibleby:200)|add:" 分钟" }}
</p>
</div>
In this example, we first usesetTags declare several variables, each storing the number of characters in the article body (articleContentWords) of the title (articleTitleWords) and a fixed summary word count (fixedSummaryWords).
The key is in the last line:{{ articleContentWords|add:articleTitleWords|add:fixedSummaryWords }} 字Here, we chained calledaddThe filter. It first addsarticleContentWordsandarticleTitleWordsThen adds this intermediate result withfixedSummaryWordsFinally, it gives out a total word count. SinceaddThe filter can intelligently handle numeric types, and we do not have to worry about variable type conversion.
In this way, we not only get an accurate number count, but also can dynamically present this information to the user, for example, further calculate the estimated reading time, making it more practical.
Summary
AnQiCMS' template filter is cleverly designed, even likewordcountandaddThis seemingly simple feature, once combined, can unleash powerful content processing capabilities.Understand and make good use of these combinations, not only can it make your website content more dynamic and rich, but also provide visitors with more considerate and convenient information experience.
Frequently Asked Questions (FAQ)
wordcountIs there a difference between the Chinese character count of the filter and English words?In AnQiCMS,wordcountThe filter usually treats Chinese characters and English words uniformly when counting.A Chinese character is counted as a 'word' or 'character', and an English word (separated by spaces) is also counted as a 'word'.This means you don't have to worry about the language differences affecting the basic word count, it will give a count that feels intuitive.addWhat type of result will be obtained when a filter performs addition of numbers and strings mixed together?WhenaddThe filter encounters a situation where numbers and strings are mixed and added together, it will try to convert non-numeric types to numbers (if possible) and then perform mathematical addition.If it contains one or more strings that cannot be converted to numbers, and the final sum result is not purely numeric, then the final result will usually tend to be string concatenation. For example,{{ 10|add:"20" }}we might get30while{{ 10|add:"CMS" }}will get10CMSIn the AnQiCMS template environment, as long as it is a valid string or number,addCan handle well and give reasonable results.except
wordcountandaddWhat filters can realize more complex numerical operations or logical judgments?AnQiCMS providedcalcArithmetic operation tag, which can directly perform complex mathematical operations such as addition, subtraction, multiplication, and division within the template without going throughaddmultiple chained filter calls. In addition, combiningifLogical judgment tags andforLoop tag, you can implement more complex conditional judgment and data accumulation logic, such as traversing a list in each iteration and accumulating or modifying the value according to a specific condition.This flexible combination of tags and filters is an important manifestation of the powerful functions of the AnQiCMS template system.