In website content operation, the way content is presented often determines the overall impression of users on the website.Whether it is to maintain brand style consistency or to enhance the readability of text, flexible case conversion of strings is a basic and important operation.AnQiCMS (AnQiCMS) knows this well, and has built-in simple and easy-to-use filters (Filters) in its powerful template engine, allowing you to easily implement string transformations such as uppercase, lowercase, and capitalization without complex programming.
We will introduce in detail how to use these filters to precisely control the case of text in the Anqi CMS template.
Convert a string to uppercase:upperFilter
When you need to emphasize a word or phrase, or in specific situations (such as warning messages, brand names) where all English characters are required to be in uppercase.upperThe filter comes in handy. It converts all letters in the string to uppercase.
Usage:
In the template, you can use the pipe character|toupperFilter applied to any variable.
{{ 您的变量 | upper }}
Example:
Assume your variableproductNamehas a value of"anqi cms"Then:
<p>产品名称:{{ productName | upper }}</p>
The output result will be:
<p>产品名称:ANQI CMS</p>
It is worth noting that,upperThe filter will only convert English letters, and Chinese or other non-English characters will remain unchanged. For example,"你好 anqi cms"AfterupperThe filtered result is still"你好 ANQI CMS".
Convert a string to lowercase:lowerFilter
WithupperFilter relatively,lowerThe filter is used to convert all alphabetic characters in a string to lowercase. This is very useful for maintaining consistency in text, especially when dealing with user input or standardizing data.
Usage:
Similarly, use the pipe symbol to.lowerApply a filter to your variable.
{{ 您的变量 | lower }}
Example:
If your variabletagTexthas a value of"SEO OPTIMIZATION"Then:
<p>标签:{{ tagText | lower }}</p>
The output result will be:
<p>标签:seo optimization</p>
Withupperis similar,lowerthe filter also only acts on English characters, and is invalid for Chinese and other non-English characters.
Capitalize the first letter of a string:capfirstFilter
capfirstThe filter is intended to convert the first letter of the string to uppercase, while the rest of the string remains unchanged.This is very useful when you need to convert a common sentence into a standard title case format.
Usage:
{{ 您的变量 | capfirst }}
Example:
Assume your variablesentencehas a value of"this is a great article."Then:
<p>{{ sentence | capfirst }}</p>
The output result will be:
<p>This is a great article.</p>
Please note,capfirstIt only handles strings of thefirst character. If the first character of the string is not an English character, it will not be converted.
Convert a string to title case:titleFilter
titleThe filter will capitalize the first letter of each word in a string and convert the remaining letters of each word to lowercase.This is very suitable for article titles, chapter titles, or any text that requires 'titling'.
Usage:
{{ 您的变量 | title }}
Example:
If your variableblogTitlehas a value of"how to optimize your anqi cms website"Then:
<h2>{{ blogTitle | title }}</h2>
The output result will be:
<h2>How To Optimize Your Anqi Cms Website</h2>
titleFilter bycapfirstEven more powerful because it will iterate over each word in the string and perform case handling, ensuring that each word starts with an uppercase letter and the rest is lowercase.
By mastering these simple yet practical string transformation filters in the AnQi CMS template engine, you will be able to control the display style of website content more flexibly, enhancing the professionalism and user experience of the content.Whether it is for unifying brand image or optimizing search engine display, these tools will be a powerful assistant for your content operation.
Common Questions (FAQ)
Do these case conversion filters work for Chinese or other non-English characters?
No, theseupper/lower/capfirstandtitleThe filter mainly targets English characters for case conversion.When applied to a string containing Chinese or other non-English characters, these non-English characters will remain unchanged, and only the English letters within them will be converted according to the filter rules.
2. Besides case conversion, what are some common string processing filters in Anqi CMS?
Anqi CMS provides a variety of string processing filters, such as:
truncatecharsortruncatewords[en]: Used to truncate strings or HTML content to a specified length and add an ellipsis.join[en]: Joins array elements into a single string.replace[en]: Replaces a specific substring in a string.striptagsorremovetagsRemove HTML tags.urlizeorurlencodeHandle URL links to make them clickable or to perform URL encoding. These filters can help you handle and display text content more flexibly in templates.
Can I apply multiple filters to a variable in a chain? For example, convert to lowercase first and then capitalize the first letter?
Yes, the template engine of AnQi CMS supports chained calls of filters. You can use the pipe character continuously.|Apply multiple filters to a variable. For example:
{{ "HELLO world" | lower | capfirst }}
This code will first convert"HELLO world"to lowercase"hello world", then capitalize the first letter of the result, and finally output"Hello world".