In the daily operation of websites, we often need to analyze and process the published content, such as counting the number of words in articles to better plan the length of the content, or to meet the requirements of search engine optimization (SEO).When we get the content of an article from a rich text editor, the content often includes a large number of HTML tags. Directly counting the words will also count these tags, leading to inaccurate results.So, does the Anqi CMS template system allow us to remove these HTML tags first and then perform word count?In particular,wordcountThe filter supports likestriptags | wordcountsuch chaining calls?
The AnqiCMS adopts a template engine syntax similar to Django, which is flexible and powerful, making many content processing tasks intuitive and efficient.In the AnQiCMS template, filters are like small but powerful tools that can help us format, convert, or process variable data.For example, we can usedateUse the filter to format time, withtruncatecharsto truncate long strings.
First, let's get to knowwordcountA filter. As the name implies, it is mainly used to count the number of words in a string.This is very helpful for evaluating article length, planning content strategy, and SEO optimization.No matter what the content is, English words or Chinese characters,wordcountCan provide their statistical results, usually by counting words (for English) or character count (for continuous Chinese text). In the template, it can be used in two ways: directly acting on variables, such as{{ obj|wordcount }}or asfilterA part of the tag.
AndstriptagsA filter is a good helper for content cleaning. When we get the article content from the rich text editor on the back end, these contents usually come with things like<p>/<strong>/<a>The HTML tags. If we directly count the number of words in the text with tags, then all the tag characters will be incorrectly counted in the total.striptagsThe filter can efficiently strip all HTML, XML tags, and HTML comments from a string, providing us with a clean text.
Then, let's get back to the most important question: can these filters be chained?
The answer is affirmative! Anqi CMS's template engine fully supports the chained invocation of filters.This means we can chain multiple filters together, so that they process the data in the order we specify, just like a pipeline operation.
The implementation is very intuitive, just use the pipe symbol between different filters.|Connect it and it's done. For example, to achieve the requirement of 'First remove HTML tags and then count the number of pure text characters', we can write the template code like this:
{{ article.Content | striptags | wordcount }}
Let's break down the process of this chained call step by step:
article.ContentFirstly, the template will retrievearticlefrom the objectContentthe original value of the field. This value may contain the complete HTML structure and text.| striptags: Immediately, the original HTML content will be passed tostriptagsfilter.striptagswill act immediately, removing all HTML tags such as<p>,<a>,<img>Remove all (including) and leave only plain text content.| wordcount: Finally, afterstriptagsProcessed plain text content will be passed as input towordcountThe filter. At this point,wordcountThe filter counts the pure text characters without any HTML interference, thus obtaining the accurate result we want.
This chained call capability provides great convenience in content operation.For example, you can easily obtain the number of characters in the plain text of an article, which can be used for content length analysis, SEO content detection, or to display a brief introduction by extracting a specific length of plain text.Moreover, the flexibility of the AnQiCMS template is also reflected in the ability to combine more filters, such as{{ article.Content | truncatechars:100 | striptags | wordcount }}This combination can achieve the complex operation of 'first cutting the first 100 characters (including HTML), then removing the HTML tags, and finally counting the number of characters'. Of course, in actual use, it is usually ensured thatstriptagsPlace it at the beginning to ensure that all subsequent operations are performed on plain text.
AnQi CMS Powerful template engine and rich filters, bringing endless possibilities to content operation.Encourage everyone to explore and combine these filters, and you will find that they can greatly improve work efficiency, making website content management more accurate and intelligent.
Frequently Asked Questions (FAQ)
Question: Do all filters in the Anqi CMS template support chained calls?Answer: Yes, the vast majority of filters in AnqiCMS are designed to support chaining calls. You can use the pipe symbol
|Concatenate them, process the data in order from left to right, just likestriptags | wordcountthat.Question: Is the order of filters important in chained calls?Answer: Very important. The processing logic of different filters is different, and the execution order will directly affect the final result. For example, first
striptagsthenwordcountIt will count the number of pure text characters, but if the order is reversed, firstwordcountthenstriptags, thenwordcountThe count still includes the original text length with HTML tags.Question:
wordcountHow does the filter calculate the count of Chinese content?Answer: For Chinese content,wordcountFilters typically count by character length rather than by the concept of 'word'.This means a Chinese character is counted as a 'word' or a character.And for English content, it will distinguish and count the number of words based on spaces.