AnQiCMS provides strong flexibility in content display, its template engine is built-in with a rich set of filters, helping users to easily process text without modifying the original content, including truncation and case conversion.These filters make the presentation of front-end content more accurate and beautiful, meeting the display needs of different scenarios.
In AnQiCMS template syntax, the use of filters is very intuitive. You can add a pipe symbol after the variable name to|)Apply the filter, if the filter requires parameters, then use a colon after the filter name(:)Pass. The basic format is usually{{ 变量 | 过滤器名称 : 参数 }}The ability to perform chained operations makes text processing efficient and concise.
Flexible text truncation techniques
In website operations, we often need to condense long content into concise summaries or titles to adapt to card layouts, list displays, or maintain page neatness.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 the number of characters, for example: {{ 文章标题 | truncatechars:15 }}The article title will be truncated to a maximum of 15 characters, and the part exceeding this will be displayed as “…”. It should be noted that “…” itself also occupies character space.
If you need to truncate based on the number of words,truncatewordsa 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 reading disconnection caused by truncating words.
When the text content contains HTML tags, it is used directlytruncatecharsortruncatewordsIt may destroy the HTML structure, causing the page to display abnormally. At this time,truncatechars_htmlandtruncatewords_htmlThe filter comes into play. It can intelligently truncate HTML content while maintaining the integrity of tags, ensuring the correct rendering of the page.
For example,{{ 文章内容 | truncatechars_html:50 | safe }}You can extract 50 characters (including HTML tags) while ensuring that the truncated HTML structure remains valid. Here is thesafeThe filter is necessary 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 cut strings or arrays according to specified start and end indices, for example{{ 文本 | slice:"1:5" }}It will extract the second to fifth characters (index starting from 0). For only getting 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 position.trimThe filter can remove spaces or specified characters from the beginning and end of a string,trimLeftandtrimRightwhich is used to delete characters from the left or right. AndcutThe filter is more powerful, it can remove specified characters at any position in the string.These filters are very useful for cleaning and formatting text, such as removing extra punctuation or prefixes.
text case conversion
The uniform formatting of text case is crucial for layout and content presentation. AnQiCMS provides a series of filters that can easily convert text case.
upperThe filter can convert all letters in a string to uppercase:{{ 变量 | upper }}It will convert 'hello world' to 'HELLO WORLD'.
lowerThe filter is the opposite, converting all letters to lowercase:{{ 变量 | lower }}It will convert “HELLO WORLD” to “hello world”.
For situations where you need to capitalize the first letter of a sentence,capfirsta 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, thentitleThe filter is the ideal choice:{{ 变量 | title }}It will convert 'hello world' to 'Hello World'.
These case conversion filters provide great convenience in handling user input, standardizing display formats, and unifying website styles.
Integrated application and **practice
The AnQiCMS filter can not only be used alone but also chained to combine multiple filters, achieving more complex text processing logic. For example, you may want to convert the title of an article to lowercase and then truncate it to the first 20 characters:{{ article.Title | lower | truncatechars:20 }}
When applying filters, especially when dealing with content that may contain HTML, be sure to pay attention to the choice of filters. As mentioned earlier, for HTML content, it is recommended to prioritize filters with_htmlSuffix filter to avoid breaking the page structure. Also, don't forget to add it when you confirm that the output content is safe HTML.| 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, improve user experience, and ensure the uniformity and professionalism of the website interface.
Frequently Asked Questions (FAQ)
- **
truncatecharsandtruncatechars_html