The template language of AnQiCMS (AnQiCMS) offers rich features, 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 to achieve 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 expression ability and development efficiency of templates.
Fine-grained processing of data display
In presenting content on the website, raw data often needs to be "dressed up" to appear elegant to the user. Filters provide strong support in this aspect.
For example, dates and times are the most common data types that need to be formatted. We can usedatefilters to store in thetime.Timetype of time values, according to2006-01-02or2006年01月02日 15:04Such a customized format is displayed, avoiding direct output of the original timestamp or a format that does not conform to reading habits. At the same time, if the data is purely a timestamp,stampToDateThis auxiliary function can also provide similar formatting capabilities.
Especially for numerical data, such as amounts or statistical figures,floatformatThe filter can help us control the number of decimal places, such as to34.23234Formatted as34.23to make the number display more neat and easy to read.
while dealing with text content,upper/lower/capfirstandtitleThese filters can easily convert letter cases, such as capitalizing the first letter of an article title or converting user comments to lowercase to maintain consistency in style. It is more commonly used totruncatecharsandtruncatewordsFilters that can intelligently truncate 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 being stretched by long text. For rich text containing HTML tags,truncatechars_htmlandtruncatewords_htmlIt ensures that truncation does not destroy the HTML structure while maintaining the integrity of page rendering.
Content formatting and optimization
The presentation of content, in addition to the data itself, is also a key factor affecting user experience. Filters can also play a unique role in this regard.
Users often enter multiline text when commenting or leaving messages, but if it is output directly, the page may not correctly recognize newline characters. At this point,linebreaksandlinebreaksbrFilters come into play, they can convert line breaks in text to HTML's<p>tags or<br/>tags, so that the content entered by the user is displayed in segments on the front end.
For URL strings in articles or comments,urlizeThe filter can automatically identify and convert it into a clickable hyperlink, greatly enhancing the convenience of browsing. If the link is too long,urlizetruncIt can truncate the display length of the link text while converting it to a link, making the page more beautiful.
When handling 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.striptagsThe filter can remove all HTML tags, andremovetagsIt allows us to specify the removal of specific HTML tags, which is very important for content security and format consistency. If the website has enabled the Markdown editor,renderThe filter can directly render Markdown formatted text into standard HTML content, allowing developers to avoid manual conversion.
Dynamic logic and conditional judgment
The filter with the Anqi CMS template inif/forcombined with logical tags, can realize more complex dynamic logic, beyond simple display or assignment.
For example,lengthandlength_isThe filter can get 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 whether a text field is too long, as it can decide whether to display certain prompt information or apply different styles.
containThe filter allows us to search for the existence of a keyword or element in a string or array, which provides great convenience for implementing content filtering, keyword highlighting, or displaying or hiding content under specific conditions. For example, to determine whether an article'sKeywordsWhether the field contains a hot word, if it contains, 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 determine if the current loop index is a multiple of a number, so as to apply different background colors or layout styles to every third, fifth element in the list, thus achieving 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 (for exampletag1,tag2,tag3Split 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_listandfieldsThere is also a similar function that can quickly convert a string into an array format.
addThe filter can be used for adding numbers or concatenating strings, such as dynamically generating title prefixes or suffixes.replaceandcutThe filter can perform precise replacement or deletion operations on strings, such as removing specific symbols or sensitive words.
Furthermore,sliceThe filter can extract the specified part of a string or array, which is very convenient when you need to extract the first few sentences as a summary from a long article or to display only a part of the elements from a large list.trimSeries filters(trim/trimLeft/trimRightCan clear whitespace from both ends or specific positions of a string, ensuring a tidy output.
Data security and debugging
In template development and