The template language of AnQiCMS provides rich functions, not limited to direct display of data or simple assignment.Among them, Filters play an important role, which can transform, format and process the variable values in the template, thereby achieving more flexible and dynamic content display.When we delve deeper into the practical scenarios of these filters, we will find that they greatly enhance the expressiveness and development efficiency of templates.

Refined processing of data display

In the presentation of website content, original data often needs to be "dressed up" before it can be presented elegantly to the user. Filters provide strong support in this aspect.

For example, date and time are the most common data types that need to be formatted. We can utilizedatefilters to store in the databasetime.Timetime values of the type,2006-01-02or2006年01月02日 15:04Such a custom format is displayed, avoiding the direct output of raw timestamps or formats that do not conform to reading habits. At the same time, if the data is purely a timestamp,stampToDateSuch auxiliary functions can also provide similar formatting capabilities.

For numerical data, especially amounts or statistical figures,floatformatFilters can help us control the number of decimal places, such as to34.23234格式化为English34.23Making the number display more regular and readable.

And when dealing with text content,upper/lower/capfirstandtitleThese filters can easily convert letter cases, such as capitalizing the first letter of article titles or converting user comments to lowercase to maintain a consistent style. They are more commonly used.truncatecharsandtruncatewordsFilters that can intelligently truncate excessively long text and add an ellipsis at the end, which is particularly useful for displaying article summaries or user comment previews on list pages, avoiding the layout of the page being stretched by long text. For rich text containing HTML tags,truncatechars_htmlandtruncatewords_htmlEnsure that the HTML structure is not damaged while truncating, maintaining the integrity of the page rendering.

Content formatting and optimization

In addition to the data itself, the presentation of content is also a key factor affecting user experience. Filters can also play a unique role in this aspect.

Users often enter multi-line text when commenting or leaving messages, but if it is output directly, the page may not be able to correctly recognize newline characters. At this time, linebreaksandlinebreaksbrThe filter can be used, it can convert line breaks in text to HTML tags.<p>or<br/>Tags, so that the content entered by the user is displayed normally in the front-end.

For URL strings in articles or comments,urlizeThe filter can automatically recognize and convert it into a clickable hyperlink, greatly enhancing the convenience of user browsing. If the link is too long,urlizetruncThen, while converting to a link, it can truncate the display length of the link text, making the page more aesthetic.

When processing rich text content imported from external sources or submitted by users, it may be necessary to clean up unnecessary HTML tags or ensure the purity of the content.striptagsFilter can remove all HTML tags, whileremovetagsThen it allows us to specify the removal of specific HTML tags, which is very important for content security and formatting consistency. If the website has enabled the Markdown editor,renderThe filter can directly render Markdown formatted text into standard HTML content, allowing developers to convert it manually.

Dynamic logic and conditional judgment

Filter and combine with the security CMS template,if/foretc., to achieve more complex dynamic logic, surpassing simple display or assignment.

For example,lengthandlength_isThe filter can obtain the length of a string, array, or object (such as a list of articles) and compare it with a specified value.This is very useful when determining whether a list is empty or a text field is too long, and you can decide whether to display certain prompts or apply different styles based on the result.

containThe filter allows us to search for a keyword or element in a string or array, which provides great convenience for scenarios such as content filtering, keyword highlighting, or displaying or hiding content under specific conditions. For example, determining whether an article'sKeywordsField contains a hot word, if it does, a 'hot' icon will be displayed next to the title.

A particularly practical scenario is to combineforloop anddivisiblebyFilter. We can use{{ forloop.Counter|divisibleby:3 }}To judge whether the current loop index is a multiple of a certain number, thus applying different background colors or layout styles to every third and fifth element of the list, to achieve a visual rhythm or special display effect.

Data Conversion and Operation

Filters also provide a convenient way for deep data processing.

splitFilters can take a string containing delimiters (such astag1,tag2,tag3)Split into an array of strings, whilejoinThe filter can then recombine the array into a string. This is very efficient for processing data structures such as article tags, keyword lists, etc.make_listandfieldsAlso has similar functions, which can quickly convert strings into array form.

addFilters can be used for adding numbers or concatenating strings, such as dynamically generating prefix or suffix for titles.replaceandcutThe filter can perform precise replacement or deletion operations on strings, such as clearing specific symbols or sensitive words from text.

In addition,sliceThe filter can extract a specified part of a string or an array, which is very convenient when you need to extract the first few sentences from a long article as a summary, or to display only a part of the elements from a large list.trimSeries filter (trim/trimLeft/trimRight) Can clear whitespace at both ends or specific positions of strings, ensuring the output is neat.

Data security and debugging

In template development