In website content operation, we often encounter the need to format text.Whether it is 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 precisely control the display of content.
Today, let's delve into how to perform case conversion on strings and elegantly display them in AnQiCMS templates.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 includes several very useful filters that 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 lowercase form,lowerThe filter will come in handy. This is particularly useful when dealing with tags, URL fragments, or any scenario requiring standardized input.
The usage is very simple, just need to putlowerFilter added after the variable you want to convert, and use a vertical bar.|:
{# 假设 article.Title 的值为 "AnQiCMS 模板使用技巧" #}
{{ article.Title|lower }}
{# 输出: anqicms 模板使用技巧 #}
{# 假设 product.SKU 的值为 "PRODUCT-X-123" #}
{{ product.SKU|lower }}
{# 输出: product-x-123 #}
It is worth mentioning 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
WithlowerFilter relatively,upperThe filter can convert all English characters in a string to uppercase. This is very effective when displaying titles, 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 }}
{# 输出: 深圳市南山区科技园 #}
Aslowerthe already escaped string,upperThe filter will not affect non-English characters and numbers.
3. Convert the first letter of the string to uppercase:capfirst
Sometimes we just need to capitalize the first letter of a string while keeping the rest unchanged, which is very useful in handling the capitalization of the first letter of a sentence or in simple formatting of user input.capfirstThe filter is created for this purpose.
{# 假设 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. Convert the first letter of each word in the string to uppercase:title
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 meet 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, capitalize the first letter, and convert the rest to lowercase to ensure a consistent title format.
Using filters together: More flexible formatting
AnQiCMS template engine allows you to use vertical bars|Chaining multiple filters to perform 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 it 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.
Actual application scenarios and **practice
These case conversion filters are extremely valuable in various content operation scenarios:
- Unified content display:Ensure that the website title, description, tags, or user-generated content (such as comments) maintain a consistent style when displayed.
- Enhance user experience:The standardized 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, consistent title formatting helps improve content quality and indirectly benefits SEO.
- Data standardization:When processing data imported from external sources, these filters can be used for preliminary data cleaning and standardization.
It should be noted that if your string content may contain HTML tags and you want these HTML tags to still be correctly parsed by the browser after performing case conversion, then you need to use chaining.|safeFilter. This is because the AnQiCMS template engine defaults to escaping HTML content for security reasons.
{# 假设 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 AnQiCMS templates, thereby creating more professional and user-friendly website content.The flexibility and powerful features of AnQiCMS templates make content operation more efficient and convenient.
Common Questions (FAQ)
Q1: Are these case conversion filters effective for Chinese or other non-English characters?
A1: Usually, these case conversion filters (such aslower/upper/capfirst/titleMainly for English characters operation.For languages without the concept of case such as Chinese, Japanese, Korean, as well as numbers and special symbols, these filters will maintain their original state and no conversion will be performed.
Q2: Why is the HTML tag also displayed as plain text after I use a filter?
A2: AnQiCMS template engine prevents cross-site scripting attacks (XSS) by default, which means that it will HTML-escape all output variable content.<Will become<,>Will become>and. If your 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: What if I need custom string processing logic that is not available 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:
- Backend processing:In the AnQiCMS Go backend code, you can take advantage of Go's powerful string processing functions to implement any complex logic and complete the processing before passing the data to the template.
- Custom template filter:If the 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 certain knowledge of Go language development, you can refer to the AnQiCMS development documentation to learn how to extend the template engine's functionality.