In the presentation of website content, the case format of strings often needs to be flexibly adjusted according to different scenarios, such as the first letter of the article title being capitalized, the username being fully lowercase, or the brand name being fully uppercase, etc.AnQiCMS as a feature-rich enterprise-level content management system, its template engine provides convenient and powerful filters (Filters) to help us easily convert the case of these strings, making the content display more standardized and professional.

AnQiCMS template case conversion function base

AnQiCMS's template system adopts syntax similar to Django template engine, one of its core advantages being the provision of rich 'filters'.These filters can perform various processing and formatting operations on variables within the template, including the string case conversion that we will discuss in detail today.

The basic syntax of using filters in AnQiCMS templates is very intuitive:{{ 你的变量 | 过滤器名称 }}。If the filter requires additional parameters, the format is{{ 你的变量 | 过滤器名称:参数 }}。In this way, we can directly control the display style of the content at the template level without modifying the backend code.

接下来,我们将了解 AnQiCMS 模板中四种常用的字符串大小写转换过滤器及其应用。

实现字符串首字母大写转换 (capfirst)

When you need to convert the first letter of a string to uppercase while keeping the rest of the string unchanged,capfirstFilter is an ideal choice. It is commonly used at the beginning of sentences, capitalizing the first letter of specific names or brand names, etc.

Usage:Simply pass the variables to be processed through the pipe character|connected tocapfirstthe filter.

{{ 你的变量 | capfirst }}

Example:Assume your variablepageTitlehas a value of"anqicms 是一款优秀的内容管理系统".capfirstAfter filter:

{{ pageTitle | capfirst }}

Output result: Anqicms 是一款优秀的内容管理系统

It is worth noting that,capfirstThe filter mainly deals with the first letter of English strings. For Chinese characters, it usually does not produce the effect of case conversion.

Convert each word to have its first letter capitalized (title case) (title)

For titles, names, places, or any text that needs to follow title case rules,titleThe filter can convert the first letter of each word in a string to uppercase and the rest to lowercase. This is very helpful for maintaining the consistency and professionalism of web content.

Usage:

{{ 你的变量 | title }}

Example:Assume your variablearticleHeaderhas a value of"anqicms 模板使用指南".titleAfter filter:

{{ articleHeader | title }}

Output result: Anqicms 模板使用指南

Withcapfirstis similar,titleThe filter is mainly aimed at English strings. It intelligently identifies word boundaries (such as spaces) and converts the first letter of each word.

Implement full uppercase conversion (upper)

Sometimes, to emphasize a word, show an abbreviation, or maintain a specific brand style, you need to convert all the letters of the entire string to uppercase.upperFilter can easily achieve this requirement.

Usage:

{{ 你的变量 | upper }}

Example:Assume your variableproductCodehas a value of"aq-cms-v3".upperAfter filter:

{{ productCode | upper }}

Output result: AQ-CMS-V3

Implement all lowercase conversion (lower)

WithupperThe filter is opposite,lowerFilter used to convert all letters in a string to lowercase.This is very useful when it comes to standardizing user input (such as email addresses), generating uniform tags or URL aliases.

Usage:

{{ 你的变量 | lower }}

Example:Assume your variableuserNamehas a value of"Administrator".lowerAfter filter:

{{ userName | lower }}

Output result: administrator

Application and Precautions in Practice

These string case conversion filters can be applied to any string variable obtained from the AnQiCMS backend, including article titles (archive.Title), category names (category.Title)、页面描述 (English)page.Description)、系统设置 (English)system.SiteName) 或任何自定义字段 (English)

在使用这些过滤器时,有几个小点需要留意:

  • 语言限制:上述所有大小写转换过滤器主要针对英文字符串。For Chinese characters or other non-Latin characters, they usually do not produce the expected case conversion effect.
  • Non-string valueIf the filter is applied to a non-string type of variable (such as a number), it will usually output as is without an error, but also without any conversion.
  • to chain calls.:AnQiCMS Template filter supports chained calls, which means you can combine multiple filters to use, for example{{ yourString | lower | capfirst }}.

By flexibly using these built-in string case conversion filters, AnQiCMS users can have more fine-grained control over the presentation of website content, thereby enhancing user experience and the overall professionalism of the website.


Common Questions (FAQ)

Q1: These case conversion filters are effective for Chinese character strings?

A1:No, in the AnQiCMS template,capfirst/title/upperandlowerFilter is mainly designed for English string. They cannot perform case conversion on Chinese characters. For example, when using "安企CMS",upperFilter, the output will still be 'AnQi CMS'.

Q2: Besides the article title and content, which variables can I use these filters on?

A2:You can use these filters for any string type template variables. This includes but is not limited to:

  • Variables related to articles/productsFor example:archive.Title(Title),archive.Description(Description),archive.Keywords(Keywords), or custom model fields.
  • Category-related variablesFor example:category.Title(Category Name),category.Description(Category description).
  • Single-page related variablesFor example:page.Title(Page title),page.Description(Page description).
  • System/Contact SettingsFor example:system.SiteName(Website Name),contact.UserName(Contact), or any custom parameter settings.

Q3: If I need to judge whether a string contains a substring, does AnQiCMS template have a corresponding filter?

A3:Yes,