In the Aanqi CMS, achieving dynamic content display is an important aspect in enhancing user experience and website professionalism.Sometimes, we may wish to display different prompts based on the length of the article content, such as prompting 'Quick Read' for short articles, and estimating reading time for long content.Auto CMS's powerful template engine and built-in filters make all this easy.
This article will delve into how to巧妙ly utilize AnQiCMSwordcountCombine filters with conditional judgments to implement a dynamic hint function based on the number of words in the content.
UnderstandingwordcountThe role of the filter
In the AnQiCMS template system,wordcountIt is a very practical filter, as the name implies, its main function is to count the number of words in the given string. According to the system documentation,wordcountThe words are counted by spaces, and a final integer value is returned.This means that it can provide the corresponding "word" count for both English articles and Chinese phrases separated by spaces.
For example, if we have a content variablearchiveContentand we want to get the number of words in it, we can use it like this:{{ archiveContent|wordcount }}This line of code will directly outputarchiveContentthe total number of words contained.
Why do we need to make conditional judgments based on the number of words?
In the operation of actual websites, the length of content often determines users' reading expectations and our strategy for providing auxiliary information.
- Enhance user experience:For content that is not very long, we can directly prompt users to quickly browse it, reducing their reading pressure; for long content, we can estimate the reading time to help users arrange their time appropriately.
- Optimize SEO strategy:Sometimes, search engines evaluate page quality based on the richness of the content.For articles that are too short, you can display prompts to encourage authors to expand the content, or guide users to visit more related articles.
- Differential display:On the list page, display different 'Read More' button styles based on the length of the article abstract, or add an 'Expand' feature for particularly long abstracts.
PasswordcountCombine condition judgment, we can implement these refined content operation strategies.
towordcountThe result is used for condition judgment
AnQiCMS's template engine supports conditional judgment similar to Django syntax, mainly through{% if ... %}tags. We can usewordcountThe result of the filter is stored in a variable, and various logical judgments are made on this variable.
Firstly, we need to obtain the content to be counted. Usually, this will be the article detail page.archive.ContentOr the list page.archive.Description(Document summary).
Suppose we want to count the words in the article detail page of the.detail.htmlTemplate, based on the content of the articlearchive.Contentthe number of words is displayed differently. We can do it like this:
Step 1: Get the article content and calculate the number of words
{%- archiveDetail articleContent with name="Content" -%} {# 获取文章内容 #}
{%- set contentWordCount = articleContent|wordcount -%} {# 将单词数量存储在 contentWordCount 变量中 #}
Here, we use-Remove blank lines that may be generated by template tags to make the code cleaner.
Step two: Set conditions and prompts based on the number of words.
Now, we havecontentWordCountThis variable can be used{% if %}/{% elif %}and{% else %}for judgment.
{%- archiveDetail articleContent with name="Content" -%}
{%- set contentWordCount = articleContent|wordcount -%}
<div class="content-tips">
{% if contentWordCount < 100 %}
<p>💡 这篇文章内容精炼,预计只需1分钟即可快速阅读完毕!</p>
{% elif contentWordCount >= 100 and contentWordCount < 500 %}
<p>🕒 预计阅读时间约3分钟,为您快速了解主题提供帮助。</p>
{% else %}
<p>📚 这是一篇深度好文,预计阅读时间较长,请您静心品读。</p>
{% endif %}
</div>
<div class="article-content">
{{ articleContent|safe }} {# 输出文章内容,注意使用safe过滤器 #}
</div>
In this example:
- If the number of words in the article is less than 100, a "quick reading" prompt will be displayed.
- If the word count is between 100 and 499 (inclusive), a "approximately 3 minutes reading" prompt will be displayed.
- If the word count reaches or exceeds 500, a prompt 'A deep and excellent article, please read it with calm' will be displayed.
More practical scenario examples
1. Dynamic prompts for the summary of articles on list pages
In the article list page, we may want to adjust the display based on the length of each article's summary.
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<div class="article-card">
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
{%- set descriptionWordCount = item.Description|wordcount -%}
<p class="description-text">
{{item.Description}}
{% if descriptionWordCount > 80 %}
<span class="read-more-hint">... (摘要较长,建议点击查看详情)</span>
{% else %}
<span class="read-more-hint">... (了解更多)</span>
{% endif %}
</p>
<a href="{{item.Link}}" class="read-more-btn">继续阅读</a>
</div>
{% endfor %}
{% endarchiveList %}
This example shows that if the number of words in the abstract exceeds 80, a more specific reading prompt will be displayed.
2. SEO suggestions based on content length.
Although this is usually the responsibility of the backend or editing, it can also provide friendly hints at the template level.
{%- archiveDetail articleContent with name="Content" -%}
{%- set contentWordCount = articleContent|wordcount -%}
{% if contentWordCount < 300 %}
<div class="seo-alert">
<p>⚠️ 注意:文章内容字数较少(当前{{ contentWordCount }}词),可能不利于搜索引擎收录和排名,建议扩充内容。</p>
</div>
{% endif %}
<div class="article-content">
{{ articleContent|safe }}
</div>
**Practical Tips and Considerations
- Code clarity:Even though it can be calculated directly in
ifconditionswordcount, but using{% set %}Store the result in a temporary variable makes the template code clearer and more readable, especially when you need to refer to this value multiple times. - Calculation base:
wordcountThe number of words is separated by spaces. For pure Chinese content without spaces,wordcountit may return 1. If you need the exact number of characters, consider usinglengthfilters (for example{{ archiveContent|length }}),and adjust the judgment threshold according to actual needs. - Performance considerations:Keep the logic within the template as concise as possible.
wordcountThe filter itself is very efficient, but when performing calculations frequently on extremely complex loops or a large amount of content, one should be aware of the potential performance impact.For the vast majority of websites, this is usually not a problem. - User Experience:The prompt information should be natural and friendly, and should not interfere with the user's reading. The use of color should also conform to the overall style of the website.
By using the above method, you can implement flexible and diverse conditional judgments and dynamic prompts in AnQiCMS based on the number of words in the content, making the website content more interactive and intelligent, thereby effectively improving user experience and content operation efficiency.
Common Questions (FAQ)
Q1:wordcountThe filter counts the number of words or characters?
A1:wordcountThe filter is mainly used to count the number of words in a string.It will separate strings by spaces and treat each separated part as a word.For example, “AnQiCMS is great” will be counted as 3 words.“Auto CMS is great” without spaces would be counted as 1 word.lengthFilter.
Q2: If I want to judge based on the number of characters in the content, which filter should I use?
A2: If you need to judge based on the character count of the content, you can use the AnQiCMS providedlengthFilter. It returns the actual character count of a string, including Chinese, English, symbols, etc. For example,{% set charCount = articleContent|length %}you can get the character count, and then you can in{% if charCount > 200 %}Judging conditions such as this.
Q3: Can I loop through the abstracts of each article on the article list page?wordcountWill it affect performance?
A3: Yes, you can in the article list page,{% for item in archives %}loop through, the abstract(item.Description) of each article,wordcountfor judgment. For example{% set descWords = item.Description|wordcount %}For most websites, this operation has a negligible impact on performance.wordcountThe filter itself has a high efficiency.If your list contains thousands of articles and you execute a lot of complex template logic in a loop, it may slightly increase the page rendering time.In most cases, there is no need to worry too much.