AnQiCMS with its flexible and powerful template system makes the presentation of website content and data processing very efficient.During template development, skillfully combining various filters often unlocks unexpected practical effects.wordcountandadd.

wordcountFilter: Smart Insight into Text Length

First, let's briefly review.wordcountFilter.Its core function is to count the number of words in a segment of text, as the name implies.wordcountThe count will be determined based on spaces (or word boundaries in the Chinese context, where a character is also considered a 'word'). The final result will be an integer.

This filter is very useful in many situations.For example, you may want to display an estimated reading time for the article detail page, or quickly understand the word count of a paragraph in content management, or even control and count the length of text in some SEO optimization strategies.

Its basic usage is very intuitive, usually like this:

<p>文章内容字数:{{ article.Content|wordcount }} 字</p>

Whenarticle.ContentWhen it includes text like "Welcome to AnQiCMS (AnQiCMS)",wordcountIt will return 2 (counting “Welcome to AnQi CMS” and “AnQiCMS” as separate words), or the specific number of words if it is a long paragraph.

addFilter: Flexible numeric and string processing

Next isaddFilter.This is a multi-functional tool, which can perform addition operations on numbers and concatenate strings.This means you can use it to accumulate 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 strings to strings, it concatenates.Even if numbers and strings are mixed when added, AnQiCMS will try to perform intelligent conversion to ensure the smooth progress of the operation.If it encounters a situation that cannot be converted, it will gracefully ignore the parts that cannot be processed to avoid interrupting the template rendering.

Example of adding numbers:

<p>结果:{{ 5|add:2 }}</p>

as the output will be7.

Example of string concatenation:

<p>组合文本:{{ "安企"|add:"CMS" }}</p>

as the output will be安企CMS.

Smart combination:wordcountWithaddPractical effects

Now, let's see whenwordcountandaddWhat kind of sparks can be ignited when these two filters work together.

Imagine a scenario like this: You are managing a blog or news website and you want to display the total reading count (word count) of each article on the article list page or in the sidebar of the article detail page.This 'total reading volume' is not just the number of words in the main text of the article, but you also want to add the number of words in the article title, and even a certain value fixed for consistency, such as 50 words, as the estimated number of words for the article abstract, so as to give readers a more comprehensive reading volume reference.

At this time,wordcountandaddcombination becomes particularly efficient. We can first usewordcountto obtain the word count of different parts, and thenaddSum 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 usesetLabel declares several variables, each storing the word count of the article's main text (articleContentWords) of the title (articleTitleWords) and a fixed abstract word count set by ourselves (fixedSummaryWords).

关键在于最后一行:{{ articleContentWords|add:articleTitleWords|add:fixedSummaryWords }} 字。这里,我们链式调用了add过滤器。它首先将articleContentWordsandarticleTitleWords相加,然后将这个中间结果再与fixedSummaryWords相加,最终得出了一个总字数。由于addThe filter can intelligently handle numeric types, and we do not need to worry about variable type conversion issues.

Through this method, we not only obtain an accurate numerical 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's template filter is designed with exquisite, evenwordcountandaddThis seemingly simple function can unleash powerful content processing capabilities once combined.Understanding and utilizing these combinations not only allows your website content to present more dynamic and rich, but also provides visitors with a more considerate and convenient information experience.


Common Questions (FAQ)

  1. wordcountIs there a difference between the filter in counting Chinese characters and English words?in AnQiCMS,wordcountFilter is usually treated uniformly when counting Chinese characters and English words.An English word (separated by spaces) is also counted as a 'word'.This means you don't have to worry about the impact of language differences on basic word count, it will give a count result that is intuitive.

  2. addWhat will be the type of the result when adding a number and a string with a filter?WhenaddThe filter attempts to convert non-numeric types to numbers (if possible) and then perform mathematical addition when it encounters a mix of numbers and strings.If it contains one or more strings that cannot be converted to numbers, and the final sum result is not purely numeric, the final result will usually tend to string concatenation.{{ 10|add:"20" }}It may be30while{{ 10|add:"CMS" }}Then you will get10CMSIn AnQiCMS template environment, any valid string or number,addcan be processed well and give a reasonable result.

  3. ExceptwordcountandaddWhat filters can be used to implement more complex numerical operations or logical judgments?AnQiCMS providescalcArithmetic operation tag, which can be directly used for addition, subtraction, multiplication, and division in the template, and more complex mathematical operations without the need to go throughaddMultiple chained filter calls. In addition, combinedifLogical judgment tag andforLoop tags allow you to implement more complex conditional judgment and data accumulation logic, such as iterating through a list and modifying numbers based on specific conditions in each iteration.These labels and filter combinations are an important manifestation of the powerful functions of the AnQiCMS template system.