How to convert the case of a string and display it in AnQiCMS template?

Calendar 👁️ 66

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.

Related articles

How does the AnQiCMS website achieve fast content display under high concurrency through static caching and Go language features?

In today's rapidly changing digital world, the speed of website access and stability under high concurrency have become key factors in user experience and search engine rankings.For a content-rich corporate website or self-media platform, ensuring that content is presented quickly while efficiently utilizing server resources is a major challenge for operators.AnQiCMS (AnQiCMS) with its powerful core based on the Go language and the exquisite static caching mechanism, provides an excellent solution to this problem.### Go Language Empowerment

2025-11-09

How to set up an independent template for a single page in AnQiCMS and display its content correctly?

In AnQiCMS, single pages (such as "About Us", "Contact Information", or specific product landing pages) are an important part of our website content.They usually carry unique information and need to have a different layout and visual style from other content on the website (such as article lists or product details).Fortunately, AnQiCMS provides极高的flexibility, allowing you to easily set up a dedicated independent template for each single page, thereby creating a unique user experience.### AnQiCMS template base

2025-11-09

How to define variables and display their values in AnQiCMS templates?

In AnQiCMS templates, we often need to handle various data, whether it is the name of the website, the title of the article, or the product details, these dynamic contents all need to be carried and displayed through variables.Understanding how to define and display variables in templates is the core of AnQiCMS template development.AnQiCMS adopts a syntax similar to the Django template engine, making the use of variables intuitive and easy to pick up.In the template file (usually in `.html` format, stored in the `/template` directory)

2025-11-09

How to iterate over an array or slice in AnQiCMS template and loop through its elements?

In the AnQi CMS template world, dynamically displaying content is the key to building an active and feature-rich website.Whether it is an article list, product display, navigation menu, or image gallery, we need a mechanism to traverse the data set and display each element on the page.Fortunately, the template engine of Anqi CMS provides an intuitive and easy-to-use loop structure, allowing us to easily achieve this goal.

2025-11-09

Does AnQiCMS support displaying collected articles directly on the website?

## AnQiCMS content collection feature: let the article go online directly, or go further?In the era of information explosion, continuously delivering high-quality content to websites is a major challenge faced by many operators.The efficiency of content production is directly related to the activity of the website, user stickiness, and even search engine rankings.

2025-11-09

How to round numbers or retain a specified number of decimal places in AnQiCMS templates?

In the presentation of website content, the accuracy and readability of numbers are crucial for improving user experience and the effectiveness of information communication.Whether it is to display product prices, financial data, or various statistical indicators, we often need to round numbers or retain specific decimal places.As a template developer or content operator for AnQiCMS, understanding how to flexibly control the display format of numbers is a key step in achieving professional content presentation.AnQiCMS's template system borrowed the syntax of Django template engine

2025-11-09

How to display the related article list of the `tagDataList` tag in AnQiCMS according to the specified Tag ID?

Manage and display content in AnQi CMS, especially when it is necessary to aggregate articles based on specific themes or keywords, the Tag feature is particularly important.It can not only help visitors quickly find the content they are interested in, but also effectively improve the internal link structure and SEO performance of the website.Today, let's delve into a very practical template tag in AnQiCMS: `tagDataList`, and see how it accurately displays a list of related articles based on the specified Tag ID.

2025-11-09

How to add captcha functionality to the AnQiCMS comment form to reduce the display of spam comments?

In website operation, spam comments are undoubtedly a headache.They not only affect the user experience of the website, dilute high-quality discussion content, but may also harm the website's SEO performance in an invisible way, even occupying valuable server resources.Faced with increasingly intelligent spam comment robots, we need more effective defense measures.Fortunately, AnQiCMS provides a simple and powerful solution - adding a captcha feature to the comment form, which can significantly reduce the intrusion of spam comments.

2025-11-09