How to quickly view the output results of the `wordcount` filter for different strings while debugging AnQiCMS templates?

Calendar 👁️ 57

When developing or debugging templates in AnQiCMS, we often need to verify the output effects of specific data or filters (filter). Especially likewordcountThis string processing filter, it may have different counting logic for different languages and formats. To ensure the template works as expected, quickly viewwordcountThe filter is particularly important for the output of various strings.

The AnQiCMS template system is based on syntax similar to Django, providing rich and powerful tags and filters.wordcountThe filter can calculate the number of words in a string, with its core mechanism being the distinction of words based on spaces.This means that for languages like Chinese, which do not have natural spaces for separation, it usually treats the entire continuous Chinese text block as a 'word'.Understand this and quickly verify it in actual debugging to ensure accurate content display.

How can we efficiently view during template debuggingwordcountWhat is the output of the filter? There are several practical methods that you can flexibly choose according to your specific needs and debugging environment.

Method one: Embed test code directly in the template.

The most direct and simplest way is to temporarily embed the test code in the template file you are debugging. For example, you canindex.htmlor any commonly usedpartialfile (such aspartial/header.htmlorpartial/footer.htmlAdd a line of code to testwordcount.

Suppose you want to test several different strings:

{# 测试空字符串 #}
<p>空字符串的单词数:{{ ""|wordcount }}</p>

{# 测试单个英文单词 #}
<p>“AnQiCMS”的单词数:{{ "AnQiCMS"|wordcount }}</p>

{# 测试多个英文单词 #}
<p>“Hello world, AnQiCMS is great!”的单词数:{{ "Hello world, AnQiCMS is great!"|wordcount }}</p>

{# 测试纯中文文本 #}
<p>“安企内容管理系统”的单词数:{{ "安企内容管理系统"|wordcount }}</p>

{# 测试中英文混合文本 #}
<p>“欢迎使用 AnQiCMS!”的单词数:{{ "欢迎使用 AnQiCMS!"|wordcount }}</p>

Add the above code snippet to the template, save and refresh the page. You will see each string processed on the page.wordcountThe actual result after the filter processing. This method is simple and quick, very suitable for immediate and temporary tests.However, after debugging is complete, please remember to remove these test codes from the production environment to maintain code cleanliness and page loading efficiency.

Method two: use{% set %}Label defines a test variable

When the test string is long, or you need to repeatedly modify the test content, use{% set %}Labeling variables first makes it more convenient. This helps maintain the clarity of template code and makes it easy to quickly modify test data.

{# 定义一个要测试的长字符串变量 #}
{% set longText = "AnQiCMS provides a highly customizable, multilingual and efficient content management solution. It aims to help small and medium-sized enterprises and content operation teams improve content management efficiency and reduce operation costs." %}

{# 定义一个包含中文的长字符串变量 #}
{% set chineseText = "安企内容管理系统为企业提供了一个稳定、灵活和高效的内容管理解决方案。它致力于帮助中小企业和内容运营团队提高内容管理效率、降低运营成本。" %}

{# 对变量应用 wordcount 过滤器 #}
<p>长英文文本的单词数:{{ longText|wordcount }}</p>
<p>长中文文本的单词数:{{ chineseText|wordcount }}</p>

{# 测试混合内容 #}
{% set mixedContent = "这是 AnQiCMS 的一个内容片段,包含中文和 English words。" %}
<p>混合内容的单词数:{{ mixedContent|wordcount }}</p>

In this way, you can focus more onlongTextorchineseTextModifying variable content without having to rewrite the entire filter expression each time. This is particularly efficient when performing multiple sets of tests.

Method three: combine{% filter %}Tag processing code block content

AnQiCMS template also supports{% filter %}Tags can pass the content of a code block as input to the filter. When you want to test dynamically generated content, or long text sections,wordcountIt will be very useful when. For example, you can combine{% lorem %}Generate random text with tags for testing.

<p>随机生成英文文本的单词数:{% filter wordcount %}{% lorem 25 w %}{% endfilter %}</p>

{# 也可以对一个已经存在的变量内容进行 block 过滤 #}
{% set dynamicContent = "这里是一些动态生成的文本内容。This is dynamic text." %}
<p>动态内容的单词数(通过 filter 标签):{% filter wordcount %}{{ dynamicContent }}{% endfilter %}</p>

{% lorem %}The label can generate a specified number (w represents word, p represents paragraph) of random Latin sample data, which is very useful when there is no real content but a large text area needs to be tested.

Debugging Tips

  • Choose an appropriate test page: Usually, you can chooseindex.htmlOr a rarely used single page for testing, or create a dedicateddebug.htmlTemplate file, and configure a temporary single-page to load it in the background. This can avoid disturbing the normal display of the website.
  • Clean up test codeAfter debugging is completed, be sure to remove all temporary test code added. Develop good habits to avoid releasing debugging code into the production environment.
  • Multi-scenario testingConsider various edge cases when testing, such as: empty strings, strings containing only numbers, strings containing only punctuation marks, strings containing special characters, multiline text, etc., to fully understandwordcountThe behavior.

By using the above method, you can quickly and effectively view during AnQiCMS template debugging.wordcountThe filter outputs the results for different strings to ensure the accuracy and stability of the template function.


Frequently Asked Questions (FAQ)

Q1:wordcountWhat is the calculation method of the filter for Chinese character strings?A1:wordcountThe filter mainly uses spaces to distinguish words. For Chinese, a language where spaces are typically not present between characters, it treats continuous, spaceless Chinese text blocks as a whole, counting them as a single word.For example, "AnQi Content Management System" is counted as 1 word.If a Chinese string is mixed with English words or numbers and there are spaces between them, then these English words and numbers will be calculated separately from the Chinese text.

Q2: BesideswordcountWhat are some commonly used string processing filters available in AnQiCMS templates?A2: AnQiCMS provides many practical string processing filters. For example:

  • length: Calculate the length of a string (number of characters).
  • truncatechars:N: Truncate the string to N characters and add “…” at the end.
  • truncatewords:N: Truncate the string to N words and add “…” at the end.
  • replace:"old,new"Replace the specified content in the string.
  • split:"分隔符": Split the string into an array according to the specified delimiter.
  • join:"连接符"Concatenate array elements with a specified separator to form a string. These filters are very useful in scenarios such as processing article summaries, title truncation, etc.

Q3: If you don't want to directly modify the template file, is there another way to test quickly?A3: If you do not want to directly modify the template files in the release environment, you can adopt the following strategies:

  1. Local development environmentSet up the AnQiCMS development environment locally, directly modify the template locally and view the effect, ensure that all test code is completed locally, and it does not affect the online environment.
  2. Create a temporary test templateCreate a temporary one in the template directorytest.htmlFile, put all the test code in it. Then, in the background "Page Management", create a new single page, and set the "Single Page Template" totest.html. Access this single page to view the test results. After the test is completed, directly delete this single page andtest.htmlthe file.
  3. Use the browser developer toolsFor some simple variable output verification, if AnQiCMS allows you to output JSON data on the front-end page (for example, through API or other means), you can view or further process these data in the browser developer tools (F12) in the console, but this does not apply to direct testing of template filter rendering effects.It is usually still necessary to put the filter expression into the template for rendering.

Related articles

Does the `wordcount` filter support counting the frequency of specific "word" like the `count` filter?

During the template development and content management process of AnQi CMS, we often need to perform various text analyses, including counting the vocabulary of text or the frequency of specific words.AnQi CMS provides us with a variety of practical template filters to handle such requirements.Today, let's discuss in detail the similarities and differences between the `wordcount` filter and the `count` filter in terms of word frequency.### Understanding the `wordcount` filter's function When you need to understand the overall length of a text, or how many 'words' it contains

2025-11-09

How to add a `wordcount` statistic for a specific field in a custom content model and display it on the frontend?

## Easily implemented: Add word count and display on the front end for article content in the Anqi CMS custom content model Word count is a seemingly simple but very useful feature in content management.In order to help readers quickly assess reading time, optimize content length to meet SEO strategies, or to control article quality for internal operations, displaying the number of words in the article can provide a direct reference.AnQi CMS with its flexible content model and powerful template tag system makes this requirement accessible. Next

2025-11-09

Can the `wordcount` filter distinguish text blocks embedded in the text and exclude them from the count?

In Anqi CMS, managing and displaying content is the core work of daily operations, among which, the statistics of the number of words in articles is a seemingly simple but may involve details problem.Today, let's delve into whether the `wordcount` filter can intelligently identify and exclude code blocks embedded in the content. ### The working principle of the `wordcount` filter Firstly, let's understand how the `wordcount` filter works in the AnQi CMS.Based on the document description

2025-11-09

How to display the total word count of all Tag tags in a document within AnQiCMS?

In the daily operation of AnQiCMS, we often need to analyze and optimize content in detail.For SEO and content strategy, it is a fundamental and practical requirement to understand the word count and word number of various elements in the document.Today, we will discuss a specific scenario: how to display the total word count of all associated Tag tags in an AnQiCMS document.This can not only help us better evaluate the quality and relevance of tags, but it can also be used in some specific content display needs.AnQiCMS provides a powerful and flexible template tag and filter mechanism

2025-11-09

How to handle the impact of punctuation marks at the beginning and end of strings when using the `wordcount` filter?

In content operations, accurately counting the number of characters or words in an article is an important link in measuring the length of content, estimating reading time, and even conducting SEO optimization.AnQiCMS (AnQiCMS) provides a convenient `wordcount` filter to help us quickly implement this feature.However, during use, we may encounter a common detail problem: whether the punctuation marks at the beginning and end of strings, or attached to words, will affect the `wordcount` statistics result??Understand this and master the corresponding handling method

2025-11-09

How to use the `wordwrap` filter in AnQiCMS to implement automatic line breaks for long text?

When using AnQiCMS to build and manage websites, we often need to handle text content of various lengths.Especially in today's increasingly popular responsive design, long text can lead to layout chaos on different devices, such as text overflowing the container, destroying the beauty of the page, and even affecting user experience.Fortunately, AnQiCMS's powerful template engine provides a variety of practical filters to solve such problems, including the `wordwrap` filter that can automatically wrap long text.The `wordwrap` filter is self-explanatory

2025-11-09

What is the specific syntax of the `wordwrap` filter in the AnQiCMS template?

When displaying content on a website, we often encounter issues with long text displaying poorly on different devices, especially on mobile devices, where an overly long line width can reduce the reading experience and even damage the page layout.AnQi CMS to help us better control text display, provides a series of practical filters, among which the `wordwrap` filter is a powerful tool to solve this problem.It ensures that the text wraps automatically when it reaches a specified length, thereby improving the readability and aesthetic appearance of the content.What is the `wordwrap` filter?

2025-11-09

How to set the character length limit for automatic line break of long text in AnQiCMS?

In content operations, we often need to manage long text content on websites in a fine manner, whether it is for layout aesthetics, improving user reading experience, or optimizing search engine display, controlling the display length of text is very important.AnQiCMS is a powerful content management system that provides a very flexible template mechanism, allowing us to easily implement automatic line breaks or truncation of long text through built-in filters, and set character length limits.## Understanding the Core of Long Text Processing: Template Filters In AnQiCMS

2025-11-09