AnQiCMS provides strong flexibility in content display, with its template engine built-in with rich filters to help users easily process text in various ways without modifying the original content, including truncation and case conversion.These filters make the presentation of front-end content more precise and beautiful, meeting the display needs of different scenarios.

In AnQiCMS template syntax, the usage of filters is very intuitive. You can add a vertical bar symbol after a variable name to use a filter.|Apply the filter, if the filter requires parameters, use a colon after the filter name:Pass. The basic format is{{ 变量 | 过滤器名称 : 参数 }}This chainable operation capability makes text processing efficient and concise.

Flexible text truncation techniques

In website operation, we often need to extract long content into concise summaries or titles to adapt to card layouts, list displays, or maintain page tidiness.AnQiCMS provides various text truncation filters to meet the needs of different scenarios.

For example, to truncate plain text content, you can usetruncatecharsandtruncatewords.truncatecharsThe filter will truncate text based on character count, for example:{{ 文章标题 | truncatechars:15 }}The article title will be truncated to a maximum of 15 characters, with the overflow part displayed as “…”. It is important to note that “…” itself also occupies character space.

If you need to truncate based on the number of words,truncatewordsthe filter would be a better choice:{{ 文章描述 | truncatewords:10 }}It extracts the first 10 words of the article description and ends with '...'. This is particularly common in English content to avoid breaking words and causing reading discontinuity.

When the text content contains HTML tags, use it directlytruncatecharsortruncatewordsit may break the HTML structure, causing the page to display abnormally. At this time,truncatechars_htmlandtruncatewords_htmlFilters come into play. They can intelligently truncate HTML content while maintaining the integrity of tags, ensuring the correct rendering of the page. For example,{{ 文章内容 | truncatechars_html:50 | safe }}Can extract 50 characters (including HTML tags) while ensuring that the HTML structure remains valid after truncation. Here,safeThe filter is necessary, used to inform the template engine that the output content is safe HTML and does not require escaping.

In addition to the above common truncation methods,sliceThe filter provides a more general string slicing function. It can extract strings or arrays based on specified start and end indices, for example{{ 文本 | slice:"1:5" }}Extracts the second to the fifth character (index starting from 0). For obtaining only the beginning or end of the text,firstandlastThe filter can quickly obtain the first or last character of a string, for example{{ 文本 | first }}.

In some cases, it may be necessary to remove characters from the beginning or end of a string, or from a specific positiontrimThe filter can remove whitespace or specified characters from the beginning and end of a string.trimLeftandtrimRightThey are used to remove characters from the left or right side, respectively.cutThe filter is even more powerful, able to remove specified characters at any position in a string.These filters are very useful for cleaning and formatting text, such as removing excessive punctuation or prefixes.

Text case conversion

Maintaining consistent text case formatting is crucial for layout and content presentation. AnQiCMS provides a series of filters that can easily convert text to different cases.

upperThe filter can convert all letters in a string to uppercase:{{ 变量 | upper }}It will convert 'hello world' to 'HELLO WORLD'.

lowerThe filter does the opposite, converting all letters to lowercase:{{ 变量 | lower }}It will convert “HELLO WORLD” to “hello world”.

For cases where the first letter of a sentence needs to be capitalized,capfirstthe filter can achieve this function:{{ 变量 | capfirst }}it will convert 'hello world' to 'Hello world'.

If you want the first letter of each word to be capitalized, for example for titles, thentitlea filter is the ideal choice:{{ 变量 | title }}Will convert 'hello world' to 'Hello World'.

These case conversion filters provide great convenience in handling user input, standardizing display formats, and unifying the website style.

Comprehensive application and **practice

AnQiCMS's filter can not only be used individually but also chained, combining multiple filters to achieve more complex text processing logic. For example, you might want to convert an article's title to lowercase and then truncate to the first 20 characters:{{ article.Title | lower | truncatechars:20 }}

When applying filters, especially when handling content that may contain HTML, be sure to pay attention to the choice of the filter. As previously mentioned, for HTML content, it should be prioritized to use filters with_htmlSuffix filter to avoid破坏页面结构. Meanwhile, when you confirm that the output content is safe HTML, don't forget to add| safefilter to prevent content from being escaped twice.

By flexibly using these text truncation and case conversion filters of AnQiCMS, you can finely control the presentation of website content, enhance user experience, and ensure the consistency and professionalism of the website interface.

Common Questions and Answers (FAQ)

  1. **truncatecharsandtruncatechars_html