When processing content in the AnQiCMS template, we often need to perform various operations, such as counting the number of words in an article.Word count is a very basic but practical feature for content management, which helps us assess the richness of content and is also an important reference indicator for SEO optimization.Built-in to AnQiCMSwordcountThe filter provides great convenience for this, allowing us to easily obtain the word count of the text.
However, when usingwordcountWhen filtering, there is a detail we may need to pay attention to. If our article content is edited through a rich text editor, it will often contain various HTML tags, such as<b>(bold),<a>(Link),<img>(Image) even<p>(Paragraph) etc. When we directly apply tags to the content that contains themwordcountWhen filtering, you may find that the statistical results are more than the actual number of words visible to the naked eye. This is becausewordcountThe filter, by default, also treats these HTML tags as part of the text for counting, resulting in a final word count that deviates from the pure text content we intended to count.
To ensure the accuracy of word count, we need to obtain the pure text word count bywordcountBefore the filter takes effect, remove the HTML tags from the content. AnQiCMS provides very practical tools for this, mainlystriptagsA filter that can effectively help us complete this task.
striptagsThe filter, as the name implies, is primarily used for "label stripping." No matter how many types and layers of HTML tags your content contains,striptagsThey can be completely removed, leaving only pure text information. In this way, when we pass the processed plain text tostriptagsthewordcountWhen the filter is applied, it can get the exact word count.
Of course, AnQiCMS also providesremovetagsA filter that allows us to control more finely, removing only the specified HTML tags instead of all. But in our pursuit of pure text word count, striptagsIt is often the more direct, more convenient choice.
Let's see how to implement it through a simple code example. Suppose we have an article content variable.archive.ContentIt may contain rich HTML formatting.
If you directly count the words in the content, you may get inaccurate results:
<!-- 这样统计可能会包含HTML标签的字符,导致结果不准确 -->
文章总字数:{{ archive.Content|wordcount }} 个字
To obtain the accurate pure text word count, we should first usestriptagsremove HTML tags, then usewordcountfor counting:
<!-- 先移除HTML标签,再统计纯文本字数 -->
文章纯文本字数:{{ archive.Content|safe|striptags|wordcount }} 个字
Here we see,archive.ContentFirstly, it passes through|safeThe filter processes to ensure that its content is recognized as safe HTML by the template engine (to prevent interference from already escaped HTML entities)striptagswork), thenstriptagsThe filter removes all HTML tags, leaving the lastwordcountThe filter performs an accurate word count on the cleaned plain text. With this combination, we can easily implement an accurate plain text word count in the AnQiCMS template.
This technique is very useful in many practical scenarios. For example, on the article list page, you may want to display the pure text word count below each article summary, so that visitors can see the length of the article at a glance;Or perhaps at the bottom of the article detail page, provide a prompt such as 'This article has XXX characters' to enhance user experience or meet SEO requirements.properly utilizestriptagsandwordcountThe filter can make the content data of your AnQiCMS website more real and transparent, thus better serving your operational strategy.
Frequently Asked Questions (FAQ)
wordcountHow does the filter define a 'word'?AnQiCMS'wordcountThe filter is mainly used to distinguish words by spaces. Any continuous sequence of characters separated by spaces is considered a 'word'.For example, if your content is "AnQiCMS is a content management system", it is usually counted as 6 words.striptagsandremovetagsWhat are the differences between the filters, and how should I choose?striptagsThe filter removes all HTML tags from the string, leaving only plain text content. It handles it more thoroughly. AndremovetagsThe filter allows you to specify one or more HTML tags to remove (such as|removetags:"p,a"Will remove<p>and<a>tags). Typically, pure text word count is used whenstriptagsThe most convenient and thorough. If you have special requirements, such as only wanting to remove specific style tags without affecting other content structures, then consider usingremovetags.**at
striptagsbefore using `