In daily website operations, we often need to analyze and process the published content, such as counting the number of words in articles to better plan content length or meet the requirements of search engine optimization (SEO).When we retrieve the article content from a rich text editor, the content often includes a large number of HTML tags. Directly counting the number of characters will include these tags, leading to inaccurate results.Then, does the template system of Anqi CMS allow us to remove these HTML tags first before counting the number of words?wordcountFilter supports calls likestriptags | wordcountsuch as this chaining?

The Anqi CMS adopts a template engine syntax similar to Django, which is flexible and powerful, making many content handling tasks intuitive and efficient.In AnQiCMS templates, filters are like small yet powerful tools that can help us format, convert, or process data of variables.dateWe can use the filter to format time, withtruncatecharsto truncate long strings.

Firstly, let's get to knowwordcountFilter.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 strategies, and performing SEO optimization.wordcountAll can provide their statistical results, usually calculated by spaces-separated words (for English) or character count (for continuous Chinese text). In the template, it can be used in two ways: directly on variables, such as{{ obj|wordcount }}or asfilterpart of the tag.

whilestriptagsFilter, it is a good helper for content cleaning. When we get the article content from the backend rich text editor, these contents usually come with things like<p>/<strong>/<a>HTML tags. If we directly count the words in the text with these tags, all the tag characters will be incorrectly counted in the total.striptagsThe filter can efficiently strip all HTML, XML tags, and HTML comments from strings, providing us with a clean text.

Then, let's go back to the most pressing issue we care about: Can these two filters be chained?

The answer is affirmative!The template engine of Anqi CMS fully supports the chained invocation of filters.This means we can chain multiple filters together, so 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 and it's done. For example, to achieve the requirement of 'remove HTML tags and then count the number of words in plain text', 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:

  1. article.ContentFirst, the template will getarticlethe original value of the field in the object.ContentThis value may contain the complete HTML structure and text.
  2. | striptags[en]:Following this, the original HTML content will be passed tostriptagsFilter.striptagsIt will act immediately, removing all HTML tags (such as<p>,<a>,<img>All) remove, leaving only the plain text content.
  3. | wordcount: Finally, after)striptagsThe plain text content after processing will be passed as input to)wordcountThe filter. At this point, wordcountThe count of the filter will be the pure text word count without any HTML interference, thus obtaining the accurate result we want.

This chain call capability provides great convenience in actual content operation.For example, you can easily obtain the pure text word count of an article, which can be used for content length analysis, SEO content volume detection, or to first extract a specific length of pure text when displaying a brief introduction.{{ article.Content | truncatechars:100 | striptags | wordcount }}Such a combination can achieve the complex operation ofstriptagsPlace it at the beginning to ensure that all subsequent operations are performed on plain text.

A powerful template engine and rich filters of AnQi CMS, bringing infinite possibilities to content operation.Encourage everyone to explore and combine these filters, and you will find that they can greatly improve work efficiency and make website content management more accurate and intelligent.


Common Questions (FAQ)

  1. Question: Do all filters in the Anqi CMS template support chained calls?Answer: Yes, the vast majority of filters in Anqi CMS are designed to support chained calls. You can use the pipe character|Join them together, process the data in order from left to right, just likestriptags | wordcountthis.

  2. 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, firststriptagsthenwordcountit will count the number of text characters, but if the order is reversed, firstwordcountthenstriptags, thenwordcountThe count still includes the original text length with HTML tags.

  3. Q:wordcountHow does the filter calculate the Chinese content?Answer: For Chinese content,wordcountThe filter usually counts by character number rather than by the concept of 'word'.This means that a Chinese character will be counted as a 'word' or a character.And for English content, it will differentiate and count the number of words based on spaces.