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 choose
index.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 understand
wordcountThe 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:
- 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.
- Create a temporary test templateCreate a temporary one in the template directory
test.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. - 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.