As an experienced website operations expert, I am well aware of the importance of content presentation and processing efficiency in daily operations.AnQiCMS (AnQiCMS) with its flexible and efficient features, has provided us with many conveniences in content management.Among them, the filter (Filters) function provided by the template engine is particularly,Filter chaining callIt is also a powerful tool for us to achieve refined content presentation and efficient template development.

Today, let's delve into the advanced usage techniques of the AnQi CMS filter chain calling, how to cleverly integrate these technologies into daily operations, and make our website content more vivid and accurate.


Unlock the potential of AnQi CMS template: In-depth analysis of advanced chaining filter techniques

In AnQi CMS template design, filters play a crucial role.It allows us to modify, format, or process variable data directly at the template level without changing the backend logic.And when we need to process data in multiple steps,Filter chaining callIt emerged naturally, it is like a data pipeline, taking the output of one filter as the input of the next filter, progressing layer by layer, until it ultimately presents the effect we want.

What is the filter chaining call? Why is it so powerful?

In simple terms, filter chaining call is through|The symbol connects multiple filters together, processing the same data continuously.For example, we may need to truncate a descriptive text first, then remove the HTML tags, and finally convert it to lowercase.If using the traditional method, this may involve multiple lines of code and even backend processing, but in Anqi CMS, a concise chained call can be done in one line:{{ 文章描述 | truncatechars:50 | striptags | lower }}.

The strength of this ability lies in:

  1. Extremely flexible and efficient:The page display logic closely fits the front-end requirements, eliminating the need for frequent changes to the back-end code, greatly speeding up iteration.
  2. Simplicity and readability of the code:Complex conversion logic is encapsulated in a concise filter chain, making template code easier to understand and maintain.
  3. Refinement of data presentation:Ensured that the website content is displayed in the most suitable and aesthetically pleasing format in different scenarios, whether it be SEO meta descriptions, list summaries, or user comments.
  4. Reducing development and maintenance costs:Reduced communication costs between frontend and backend engineers, improving overall team collaboration efficiency.

Master the advanced techniques of filter chain calling, which enables us to operate the website and template development with ease.Next, I will combine practical operating scenarios to show you these practical skills.

I. Refine content presentation: balancing aesthetics and SEO

The display of website content should not only attract users but also meet the requirements of search engine optimization (SEO). Chaining filter calls provide strong support in this regard.

  1. Efficient truncation and formatting of summaries:On the list page or in search results, we usually need to display a brief summary of the article. To avoid content being too long and affecting the layout, and to remove potential HTML tag interference, while ensuring the number of characters meets SEO requirements, we can do this:

    • {{ item.Description | truncatechars_html:120 | safe }}This chain is first usedtruncatechars_html:120Truncate the description containing HTML tags, intelligently maintain the HTML structure, and limit the number of characters to 120 (including ellipsis).safeThe filter ensures that HTML tags are parsed correctly and not displayed as escaped.
    • If the description does not contain HTML or if all formatting needs to be completely removed, you can usestriptags:{{ item.Description | striptags | truncatechars:150 }}This will first remove all HTML tags and then truncate the plain text content.
  2. Unified title and keyword case:In order to maintain brand consistency or comply with certain specific SEO standards, we may need to unify the capitalization of titles or keywords.

    • {{ item.Title | title }}:titleThe filter will capitalize the first letter of each word and the rest will be lowercase, which is very suitable for displaying article titles.
    • {{ item.Keywords | lower | replace:", ","-" }}This example can convert all keywords to lowercase and replace commas and spaces between multiple keywords with hyphens-This is very useful when generating URL-friendly keyword tags.
  3. URL processing and encoding: Ensure the validity and security of the link.In certain scenarios, we need to dynamically generate URL parameters with special characters in templates, or convert URLs in plain text to clickable links.

    • {{ "你的查询词" | urlencode }}:urlencodeThe filter will encode special characters in the string to ensure that it does not cause errors when used as a URL parameter.
    • {{ 文章内容 | urlize | safe }}:urlizeThe filter automatically identifies URLs or email addresses in the text and converts them to clickable<a>tags, and adds them automaticallyrel="nofollow"Property, this is very convenient for handling user submitted content. If you want to limit the display length, you can also cooperateurlizetrunc:30usage.

Second, data cleaning and format conversion: make the data more 'obedient'

The format of the data transmitted from the backend may not always directly meet the needs of the frontend display. By chaining filter calls, we can perform efficient data cleaning and format conversion.

  1. Strip and replace: purify data content

    • {{ item.Content | striptags | removetags:"script,iframe" | safe }}This chain first removes all HTML tags, then especially removesscriptandiframetags, and finally usessafeEnsure that the remaining HTML content is parsed, which is particularly critical for processing external collections or user submissions that require high security.
    • {{ item.Content | replace:"旧词,新词" | replace:"另一个旧词,另一个新词" | safe }}: By continuous usereplaceA filter that can perform batch replacement of multiple keywords, such as uniform sensitive words, changing brand names, etc., which is very efficient during content updates or brand strategy adjustments.
  2. Type conversion and default values: improving data toleranceSometimes, we may receive numbers in string format from the backend, or a field may be empty.

    • {{ item.Price | float | stringformat:"%.2f" }}If:item.PriceIs a string,floatIt will convert it to a floating-point number, thenstringformat:"%.2f"Format it to a string with two decimal places. This is very practical in scenarios such as e-commerce product price display.
    • {{ item.Author | default:"匿名用户" | upper }}If:item.AuthorIf empty, display 'Anonymous User', otherwise display the author's name, and then convert it to uppercase. This ensures the integrity of the key information on the page, avoiding display issues caused by empty values.
  3. Text structuring and reorganization: Flexible handling of lists and stringsWhen data comes as a comma-separated string, and we need to treat it as a list, or vice versa.

    • {% set tags_array = item.Tags | split:", " %} {% for tag in tags_array %}{{ tag | upper }} {% endfor %}Here, we first translateitem.TagsAssuming it is "SEO, optimization, content marketing" passsplit:", "Filter splits it into an array of strings, then iterate over the array, convert each tag to uppercase and display.
    • {{ some_list | join:" | " }}: Conversely, if there is a list of stringssome_listthrough.join:" | "We can elegantly concatenate it into a string separated by vertical bars.

3. Dynamic condition judgment and content generation: Enhancing the intelligence of templates

Filter chaining is not limited to simple formatting, it can also be combined with conditional judgment tags to achieve more intelligent template logic.

  1. Content presentation conditionally: {% if item.Content | contain:"图片" %}<p>本文包含图片</p>{% endif %}: CombinedcontainFilter, we can detect specific keywords in the content to dynamically display hints.{% if user.Status | yesno:"VIP,普通用户,未知" == "VIP" %}欢迎尊贵的VIP用户!{% endif %}:yesnoThe filter can return custom three states based on boolean or null values, combined with conditional judgments to realize dynamic welcome language for user levels.

  2. Layout and text effects: {{ "重要提示" | repeat:3 | upper }}:Please repeat "important reminder" three times and convert it to uppercase for highlighting on the page.{{ item.Title | center:40 }}:Center the title text within a width of 40 characters, which is very helpful for specific layout requirements (such as console information output).

Practical tips and **practice

  • **Understanding