The template engine of AnQi CMS is designed with the simplicity and power of Django templates, allowing us to display data and control the page structure through intuitive syntax.Among them, various "Filters" are important tools for processing data format and text style.{{变量|过滤器名称:参数}}Process variable content in this format.

Flexible use of filters for case conversion of English strings.

For the case of case conversion of English characters, Anqi CMS has built-in several very practical filters that can help us easily achieve common layout needs:

1. Convert the first letter of an English string to uppercase:capfirstFilter

When you want the first letter of an English sentence or phrase to be capitalized, and the rest of the letters to remain unchanged,capfirstThe filter will be your ideal choice. It will only process the first English letter in the string.

For example, if you have a variabletitleThe value is 'hello there, anqicms!' and it can be displayed with the first letter capitalized like this:

{{ title|capfirst }}

This will output "Hello there, anqicms!"}]EnglishIt is worth noting that if the string starts with Chinese or other non-English characters, this filter will not perform any processing.

2. Convert all English characters to uppercase or lowercase:upperandlowerFilter

Sometimes, we need to convert the entire English string to uppercase or lowercase to meet specific typesetting styles or emphasis requirements.upperandlowerThe filter can be used.

  • upperFilter: Convert to UppercaseIf you have a variableproductNamethe value is "anqi cms", and it is hoped to display it all in uppercase:

    {{ productName|upper }}
    

    The output will be "ANQI CMS".

  • lowerFilter: Convert to lowercaseIf you have a variableTAGThe value is 'SEO OPTIMIZATION', it needs to be converted to lowercase:

    {{ TAG|lower }}
    

    输出将是 “seo optimization”。

These two filters will traverse the entire string, converting all English letters according to the rules without affecting Chinese characters or other special characters.

3. Convert the first letter of each word to uppercase:titleFilter

In handling scenarios such as titles and names, it is often necessary to capitalize the first letter of each word in the string, for example, “Anqi Cms”.titleFilter is specifically designed for this purpose. It will capitalize the first English letter of each word in the string, and convert the rest to lowercase.

Suppose you have a variablearticleHeadingThe value is 'how to use anqicms templates', would you like to format it as a heading style?

{{ articleHeading|title }}

输出将会是 “How To Use Anqicms Templates”。This filter is very suitable for unifying the display specifications of titles, product names, and other content on the page.

Application scenarios and precautions

These case conversion filters are very useful in the template development of Anqi CMS. For example, you can apply them to:

  • Article title (archive.Title)Ensure that the titles are displayed in a unified style.
  • 分类名称 (English)category.Title):标准化分类导航的显示。
  • 标签名称 (English)tag.Title):使标签云或标签列表更整洁。
  • 用户提交的英文内容:Before displaying, format the content to improve the standardization.

Points to note:

  • Character limit: All the above filters are mainly forEnglish charactersPerform the operation. Chinese characters are usually unchanged after applying these filters.
  • to chain calls.: English CMS of security supports chained calls, which means you can connect multiple filters together to use. For example,{{ variable|lower|capfirst }}The first will convert all letters to lowercase, and then the first letter will be converted to uppercase. But please note that the execution order of the filter is from left to right, and different orders may produce different results.
  • The type is not a stringIf these filters are used on non-string type variables (such as numbers), they are usually converted to strings before processing, or returned as the original value, depending on the filter implementation.When performing type conversion, please ensure the variable type is as expected.

Master these filters, and you will be able to control the display of Chinese and English strings in the security CMS website more freely, thus creating a more professional and consistent page layout.


Common Questions (FAQ)

Q1:这些大小写转换过滤器对中文或其他非英文字符串有效吗?A1:不,这些过滤器(capfirst/upper/lower/title专门设计用于处理英文字符。当应用于包含中文或其他非英文字符的字符串时,这些非英文字符通常会保持不变。例如,"你好 world"|upperThe output is still 'hello WORLD'.

Q2: How should I perform multiple case conversions on a variable?A2:You can chain the filter call.The template engine of AnQi CMS will apply these filters in order from left to right.{{ myVariable|lower|title }}But please note that different chained call sequences may produce different results, please combine according to actual needs.

Q3:What are some common string processing filters in the AnQi CMS template, besides case conversion?A3:English CMS provides rich string processing filters, such as:

  • truncatechars:数字:Truncate strings by character count and add “...” at the end.
  • replace:"旧词,新词"Replace the specified old word with a new word in the string.
  • cut:"字符"Remove all occurrences of the specified character from the string.
  • trimDelete leading and trailing spaces or specified characters. These filters can help you better control the display of content and improve the quality of layout.