In the AnQiCMS content management system, the presentation effect of content often determines the user experience and the efficiency of information communication.To meet the diverse content display needs, AnQiCMS provides a powerful and flexible template filter function.These filters can help us with fine-grained processing of the original text during content display, whether it's adjusting text style, optimizing display length, or performing data conversion, it can be easily achieved.

Understand AnQiCMS template filters

From a technical perspective, the filter syntax of AnQiCMS is very intuitive, its core structure is like{{ 变量 | 过滤器名称: 参数 }}. By following a pipe symbol after a variable|Then specify the filter name and its possible parameters, and we can process the output content of the variable in real time.This mechanism makes content formatting and truncation extremely convenient, without modifying the original content, just controlling it at the template level.

flexible text formatting techniques

In website content operation, text formatting is a key factor in improving the readability and professionalism of the content. AnQiCMS provides a variety of filters to meet different formatting needs:

1. Title case format should be uniform in capitalizationImagine that we need to capitalize the first letter of the article title or paragraph, or convert it to uppercase or lowercase uniformly, or even capitalize the first letter of each word to meet the title specification. At this point,capfirst(First letter capitalized,)lower(All lowercase,)upper(All uppercase) andtitleThese filters can be put to good use, ensuring that the text maintains a uniform style across different display scenarios.

2. Precisely handle HTML contentWhen the content contains HTML tags, we may want these tags to be parsed by the browser normally, rather than displayed as plain text. At this time,safeThe filter is crucial, as it informs the template engine that the content is safe and does not need to be escaped, and can be output directly as HTML. For example, the main content of an article will usually use{{ archive.Content|safe }}Make sure the format is rendered correctly. In addition,renderThe filter is very useful when converting Markdown content to HTML. For the scenario where you need to retain line breaks in plain text and convert<br/>or<p>tags,linebreaksandlinebreaksbrProvided a convenient conversion method. If the goal is to remove all HTML tags,striptagsyou can clear it with one click; whileremovetagsCan accurately delete specified HTML tags, such as removing all<i>.

3. Standardization of numerical and time informationWhen displaying numerical information such as prices or statistics,floatformatThe filter helps us accurately control the decimal places of floating-point numbers, ensuring clear presentation of the numbers. If a more generic formatting output is needed,stringformatit provides a format similar to Go languagefmt.Sprintf()The powerful function, which can output various types of data in a customized pattern. Handling time information is another common requirement, AnQiCMS providesdateFilter (or its aliastimewilltime.TimeTime formatting of the type, as well as more commonly used for timestampsstampToDateFilter, which can easily convert a 10-digit timestamp into a readable date and time format, such as{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}.

4. Automated URL and Email LinkThe website content often includes URLs or email addresses, and we hope they can be automatically converted into clickable links for the convenience of users.urlizeThe filter can recognize these strings and wrap them in<a>tags, even automatically addingrel="nofollow"attributes to optimize SEO. If the link is too long,urlizetruncCan also convert to a link while intelligently truncating the displayed text and adding an ellipsis to keep the page tidy.

5. Flexible management of white spaces and specific characters in text.Sometimes there may be extra spaces, line breaks at the beginning and end of the text, or specific characters in the content need to be deleted.trimSeries filters(trim/trimLeft/trimRightIt can flexibly handle leading and trailing whitespace or specified characters.cutThe filter can remove specific characters from any position in a string, for example, removing all spaces or commas from the text.

6. Combination, splitting, and replacing of text.Concatenating multiple text segments or data is a common operation.addFilters can perform addition operations or concatenation on numbers or strings. When dealing with arrays,joinThe filter can join array elements into a string with a specified delimiter; conversely,splitThe filter can split a string into an array with a delimiter.make_listCan also split a string into an array of characters. If you need to replace specific keywords in the text,replaceThe filter provides an accurate find and replace feature, for example, replacing all occurrences of 'AnQiCMS' with 'AnQiCMS'.

Effective content truncation strategy

On list pages or in the summary display area, appropriately truncating text content is an important means to improve page loading speed and visual aesthetics. AnQiCMS provides various truncation filters:

1. Truncate by character count (truncatechars,truncatechars_html)Under scenarios such as article summaries, list previews, etc., we often need to truncate text to control display length.truncatecharsThe filter truncates by character count and adds an ellipsis (…) at the end. It is worth noting that the ellipsis itself is counted in the character count. If the content contains HTML tags,truncatechars_htmlIt can intelligently truncate while maintaining the integrity of the HTML structure, avoiding page display errors, which is a very practical feature when using rich text in summaries.

2. Truncate by word count (truncatewords,truncatewords_html)If you want to truncate more "semantically", avoid truncating words,truncatewordsThe filter truncates based on the number of words, which is particularly important in English content to maintain the integrity of the sentence. Similarly, its HTML versiontruncatewords_htmlCan maintain word integrity and structure when processing HTML content, avoiding the truncation of half of the HTML tags or words.

3. Flexible slicing operation (slice)For more flexible slicing needs, such as starting from a certain position in a string or array to another position,sliceThe filter provides powerful slicing capabilities, allowing precise control over the start and end positions, which is very useful for scenarios that require dynamically extracting specific parts of the content.

It