In website content operation, we often encounter the need to format text.In order to unify the brand image, improve the readability of the page, or optimize the search engine friendliness, converting strings to uppercase or lowercase is a fundamental and practical skill.AnQiCMS as an enterprise-level content management system developed based on the Go language, with its flexible template engine syntax (similar to Django templates), provides us with a rich set of tools to accurately control the display of content.

Today, let's delve into how to convert string case and elegantly display it in the AnQiCMS template.These features are implemented through built-in filters (Filters), which are very intuitive and easy to use.

Master the commonly used string case conversion filters

AnQiCMS template engine built-in several very useful filters, which can help us easily achieve different case conversion needs.

1. Convert all English characters to lowercase:lower

When we need to ensure that the text content is presented in full lowercase, lowerThe filter will come in handy. This is especially useful in scenarios involving tags, URL fragments, or the need for standardized input.

The method of use is very simple, just need tolowerThe filter should be added after the variable you want to convert, and use a vertical bar|bar:

{# 假设 article.Title 的值为 "AnQiCMS 模板使用技巧" #}
{{ article.Title|lower }}
{# 输出: anqicms 模板使用技巧 #}

{# 假设 product.SKU 的值为 "PRODUCT-X-123" #}
{{ product.SKU|lower }}
{# 输出: product-x-123 #}

It is worth noting that for non-English characters (such as Chinese) or numbers,lowerThe filter will intelligently maintain its original state without making any changes.

2. Convert all English characters to uppercase:upper

withlowerThe filter is relative,upperThe filter can convert all English characters in a string to uppercase. This is very effective for displaying headings, emphasizing key information, or creating eye-catching text.

Similarly, the usage is also very direct:

{# 假设 system.SiteName 的值为 "AnQiCMS 内容管理系统" #}
{{ system.SiteName|upper }}
{# 输出: ANQICMS 内容管理系统 #}

{# 假设 contact.CompanyAddress 的值为 "深圳市南山区科技园" #}
{{ contact.CompanyAddress|upper }}
{# 输出: 深圳市南山区科技园 #}

As iflowerFilter,upperThe filter will not affect non-English characters and numbers.

3. Convert the first letter of the string to uppercase:capfirst

Sometimes we only need to capitalize the first letter of a string while the rest remains unchanged, which is very useful for handling sentence-initial capitalization or for simple formatting of user input.capfirstThe filter is exactly for this.

{# 假设 page.Description 的值为 "这是一个安企cms的页面描述,非常简洁实用。" #}
{{ page.Description|capfirst }}
{# 输出: 这是一个安企cms的页面描述,非常简洁实用。 #}

{# 假设 item.ShortName 的值为 "hello world" #}
{{ item.ShortName|capfirst }}
{# 输出: Hello world #}

Please note,capfirstThe filter only acts on the first character of the string.

4. Capitalize the first letter of each word in the string:title

In order to make the title or proper nouns display more standardized and professional, we may need to capitalize the first letter of each word in the string.titleThe filter can perfectly achieve this requirement.

{# 假设 product.BrandName 的值为 "anqicms 企业版" #}
{{ product.BrandName|title }}
{# 输出: Anqicms 企业版 #}

{# 假设 archive.Keywords 的值为 "anqicms, cms template, go语言" #}
{{ archive.Keywords|title }}
{# 输出: Anqicms, Cms Template, Go语言 #}

titleThe filter will iterate over each word in the string, capitalizing the first letter and converting the rest to lowercase to ensure a consistent title format.

Combine filters for more flexible formatting

AnQiCMS template engine allows you to use the pipe character|Using multiple filters chained together to achieve more complex string processing logic. For example, if you want to first convert the entire string to lowercase and then capitalize the first letter, you can combine them like this:

{# 假设 originalString 的值为 "THIS IS A TEST SENTENCE." #}
{{ originalString|lower|capfirst }}
{# 先转换为小写: "this is a test sentence." #}
{# 再将第一个字母大写: "This is a test sentence." #}

The order of this chained operation is very important because each filter operates on the result processed by the previous filter.

Application scenarios and **practice

These case conversion filters are extremely valuable in various content operation scenarios:

  • Unified content display:Make sure the website title, description, tags, or user-generated content (such as comments) are displayed in a consistent style.
  • Improve user experience:Standard text format makes it more comfortable for users to read and enhances the professionalism of the website.
  • Optimize SEO:Although search engines are case insensitive, a consistent title format helps improve content quality and indirectly benefits SEO.
  • Data standardization:When processing data imported from external sources, these filters can be used for initial data cleaning and normalization.

It should be noted that if the string content may contain HTML tags and you want these HTML tags to be correctly parsed by the browser after case conversion, you need to use chaining|safeThe filter. This is because the AnQiCMS template engine defaults to escaping HTML content for safety.

{# 假设 article.Content 包含 HTML 标签,如 "<b>AnQiCMS</b> is great" #}
{{ article.Content|lower|safe }}
{# 输出: <b>anqicms</b> is great #}

By flexibly using these filters, you can finely control the display of strings in the AnQiCMS template, thereby creating more professional and user-friendly website content.The flexibility and powerful features of the AnQiCMS template make content operation more efficient and convenient.


Frequently Asked Questions (FAQ)

Q1: Do these case conversion filters work for Chinese or other non-English characters?

A1: Generally, these case conversion filters (such aslower/upper/capfirst/title)It mainly operates on English characters. For languages like Chinese, Japanese, Korean, etc., which do not have the concept of uppercase and lowercase, as well as numbers and special symbols, these filters will maintain their original state and will not be converted.

Q2: Why are HTML tags also displayed as plain text after I use the filter?

A2: AnQiCMS template engine to prevent cross-site scripting attacks (XSS) defaults to escaping all output variable content. This means<Will become&lt;,>Will become&gt;If the string content indeed contains HTML tags that need to be parsed by the browser, and you have confirmed their safety, then you need to add them in a chain after the case conversion filter|safeFilter, for example{{ article.Content|lower|safe }}.

Q3: How do I need a custom string processing logic that is not in the AnQiCMS template?

A3: AnQiCMS provides a flexible extension mechanism. If the built-in filters do not meet your specific needs, you can consider the following two options:

  1. Backend processing:In the AnQiCMS Go language backend code, you can use Go's powerful string processing functions to implement any complex logic and complete the processing before passing the data to the template.
  2. Custom template filter:If this processing logic needs to be reused in multiple template locations, or if you want to integrate it into the template syntax, AnQiCMS allows developers to create custom template tags and filters.This requires a certain amount of Go language development knowledge, you can refer to the AnQiCMS development documentation to learn how to extend the template engine function.