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

The case conversion function of AnQiCMS template

AnQiCMS's template system adopts a syntax similar to the Django template engine, one of its core advantages is that it provides a rich set of "filters".These filters can perform various processing and formatting operations on variables in 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 content at the template level without modifying the backend code.

Next, we will understand the four commonly used string case conversion filters and their applications in the AnQiCMS template.

Implement the uppercase conversion of the first letter of the string (capfirst)

When you need to capitalize the first letter of a string while keeping the rest unchanged,capfirstA filter is an ideal choice. It is often used at the beginning of sentences, for capitalizing the first letter of specific names or brand names, and other scenarios.

Usage:Just pass the variable to be processed through the pipe symbol.|Connected tocapfirstthe filter.

{{ 你的变量 | capfirst }}

Example:Assume your variablepageTitleThe value is"anqicms 是一款优秀的内容管理系统"ApplycapfirstAfter the filter:

{{ pageTitle | capfirst }}

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

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

Implement title case conversion (capitalize the first letter of each word)title)

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

Usage:

{{ 你的变量 | title }}

Example:Assume your variablearticleHeaderThe value is"anqicms 模板使用指南"ApplytitleAfter the filter:

{{ articleHeader | title }}

Output Result: Anqicms 模板使用指南

withcapfirstsimilar,titleThe filter also mainly targets English strings. It intelligently identifies word boundaries (such as spaces) and capitalizes the first letter of each word.

Perform 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.upperThe filter can easily meet this requirement.

Usage:

{{ 你的变量 | upper }}

Example:Assume your variableproductCodeThe value is"aq-cms-v3"ApplyupperAfter the filter:

{{ productCode | upper }}

Output Result: AQ-CMS-V3

Implement full lowercase conversion (lower)

withupperThe opposite filter,lowerThe filter is used to convert all letters in a string to lowercase.This is very useful when it is necessary to standardize user input (such as email addresses), generate uniform tags, or URL aliases.

Usage:

{{ 你的变量 | lower }}

Example:Assume your variableuserNameThe value is"Administrator"ApplylowerAfter the filter:

{{ userName | lower }}

Output Result: administrator

Practical application and precautions

This case-insensitive string converter filter can be applied to any string variable obtained from the AnQiCMS backend, including article titles (archive.Title), category names (category.Title) of Page Description (page.Description) of System Settings (system.SiteName) or any custom field.

A few points to note when using these filters:

  • Language LimitationThe above all case conversion filters are mainly for English strings.For Chinese characters or other non-English characters, they usually do not produce uppercase or lowercase conversion effects, or the effects may not be as expected.
  • Non-string valueIf the filter is applied to a non-string variable (such as a number), it will usually be output as is without an error, but it will not be converted either.
  • chaining call: AnQiCMS template filters support chained calls, which means you can combine multiple filters for use, for example{{ yourString | lower | capfirst }}.

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


Frequently Asked Questions (FAQ)

Q1: Do these case conversion filters work for Chinese string?

A1:No, in the AnQiCMS template,capfirst/title/upperandlowerFilters are mainly designed for English string design. They cannot perform case conversion for Chinese characters. For example, using "安企CMS"}upperFilter, the output will still be 'Anqi CMS'.

Q2: Besides article titles and content, what other 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:

  • Article/product related variables: such asarchive.Title(Title),archive.Description(Description),archive.Keywords(Keywords), or custom model fields.
  • Categorization related variables: such ascategory.Title(Category Name),category.Description(Category description).
  • Single page related variables: such aspage.Title(Page title),page.Description(Page description).
  • System/Contact Information Settings: such assystem.SiteName(Website Name),contact.UserName(Contact), or any custom setting parameters.

Q3: If I need to determine if a string contains a substring, does the AnQiCMS template have a corresponding filter?

A3:Yes,