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

AnQiCMS's 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 to distinguish words by spaces.This means that for languages like Chinese, which do not have natural spaces to separate words, it usually treats the entire continuous block of Chinese text as a 'word'.Understanding this and quickly verifying it in actual debugging is the key to ensuring accurate content display.

Then, how can we efficiently view during template debugging processwordcountThe output result 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 can inindex.htmlor any commonly usedpartialfile (such aspartial/header.htmlorpartial/footer.html)Add 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 it, and refresh the page. You will be able to see each string processed on the page.wordcountFilter processed actual result.This method is simple and quick, very suitable for immediate and temporary testing.However, after debugging is completed, please remember to remove these test codes from the production environment to maintain code cleanliness and page loading efficiency.

Method two: use{% set %}Define a test variable with the tag.

When the test string is long, or you need to frequently modify the test content, use{% set %}Define a variable for the label will be more convenient. This helps to maintain the clarity of the 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 onlongTextorchineseTextVariable content modification without the need to rewrite the entire filter expression each time. This is particularly efficient when performing multiple groups of tests.

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

AnQiCMS template also supports{% filter %}Tags, which can pass the content of a code block as input to the filter. When you want to test dynamically generated content, or large blocks of text,wordcountIt will be very useful. For example, you can combine{% lorem %}Label to generate random text 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 %}Labels can generate a specified number (w represents words, p represents paragraphs) of random Latin sample data, which is very practical when there is no real content but a large text area needs to be tested.

Debug Tips

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

By the above method, you can quickly and effectively view the debugging of AnQiCMS templates.wordcountFilter outputs for different string results to ensure the accuracy and stability of the template function.


Common Questions and Answers (FAQ)

Q1:wordcountWhat is the calculation method for Chinese character strings in the filter?A1:wordcountFilter primarily distinguishes words based on spaces.For languages like Chinese where characters are typically separated by no spaces, it treats continuous, spaceless Chinese text blocks as a whole, counting them as a single word.For example, "Security 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 in AnQiCMS templates?A2: AnQiCMS provides many practical string processing filters. For example:“

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

Q3: If you do not want to directly modify the template file, are there other ways 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 environment:In local environment setup of AnQiCMS, directly modify the template locally and view the effect, ensure that all test code is completed locally, without affecting the online environment.
  2. Create a temporary test templateCreate a temporary one in the template directorytest.htmlFile, place all test code in it. Then in the background "Page Management", create a new single page, and set the "Single Page Template" totest.html。Visit this single page to view the test results. After the test is completed, simply delete this single page andtest.htmlthe file.
  3. Use the browser's developer toolsFor certain simple variable output verification, if AnQiCMS allows you to output JSON data on the front-end page (such as through API or other methods), you can view or further process these data in the console of the browser developer tools (F12), but this does not apply to the direct testing of template filter rendering effects.It is usually still necessary to put the filter expression into the template for rendering.