In AnQi CMS template development, formatting strings is a common requirement.Whether it is to display article titles, user nicknames, or category names, sometimes we need to ensure that they are presented in a specific letter case, such as capitalizing the first letter of a string.The powerful template engine of Anqi CMS provides a variety of flexible filters (Filter) to help us complete these tasks efficiently.
Skillfully usecapfirstFilter: Capitalize the first letter.
For capitalizing the first letter of an English string, Anqi CMS provides a very intuitive and efficient template filter:capfirst. This filter will recognize the first letter of the English string and convert it to uppercase, while the rest of the string maintains its original letter case.
For example, if you have a variable{{ archiveTitle }}Its value is"hello world"And, to capitalize the first letter, you can use it like this:
{{ archiveTitle|capfirst }}
{# 输出: Hello world #}
It is worth noting that,capfirstThe filter mainly processes English characters. If the string starts with Chinese or other non-English characters, it will not be converted.
Letter case conversion in more scenarios.
In addition to capitalizing the first letter, you may also encounter other letter case conversion needs in content operations. Anqi CMS also provides corresponding filters to meet these situations:
1. Convert the entire English string to lowercase:lower
Sometimes, to maintain a consistent display style or for data matching, you may need to convert the entire English string to lowercase. At this point, you can uselowerFilter:
{{ "HELLO WORLD"|lower }}
{# 输出: hello world #}
2. Convert the entire English string to uppercase:upper
withlowerThe filter is the opposite, if your goal is to convert all letters to uppercase,upperThe filter can be put to use:
{{ "hello world"|upper }}
{# 输出: HELLO WORLD #}
3. Capitalize the first letter of each word:title
For a title or phrase, we often need to capitalize the first letter of each word to comply with common title format.titleThe filter is created for this purpose:
{{ "anqi cms is great"|title }}
{# 输出: Anqi Cms Is Great #}
Example of practical application scenarios
These filters are very practical in various security CMS template scenarios. For example, when displaying the article detail page title, we usually want the first letter of the title to be capitalized, even if the editor does not pay attention to it in the background:
{% archiveDetail archiveTitle with name="Title" %}
<h1 class="article-title">{{ archiveTitle|capfirst }}</h1>
Or maybe you want to display the website name uniformly at the footer of the website and make sure the first letter is capitalized:
<span class="site-name">{{ system.SiteName|capfirst }}</span>
Flexible filter chaining call
The AnQi CMS template filter supports chaining, which means you can connect multiple filters together to process data in a series of steps. For example, if you are unsure of the original case of a string but want to present it with the first letter capitalized, you can first convert it to lowercase and then perform the capitalize-first-letter operation:
{{ "AnQi CMS"|lower|capfirst }}
{# 先转小写为 "anqi cms",再首字母大写为 "Anqi cms" #}
In addition, if your string may contain HTML code and you want the HTML tags to be parsed correctly rather than escaped, you can add it at the end of the filter chain.safea filter. For example:
{{ "<strong>hello world</strong>"|capfirst|safe }}
{# 输出: <strong>Hello world</strong> #}
By flexibly using these string filters provided by Anqi CMS, you can easily control the display format of text, enhance the professionalism and user experience of website content.These tools are not only simple and easy to use, but also powerful, and are a great helper for you to develop security CMS templates and operate content.
Frequently Asked Questions (FAQ)
Do these string filters work for Chinese string?These filters (
capfirst/lower/upper/titleIt primarily handles English characters. When it encounters Chinese string, it usually does not change the display of Chinese characters, or it will only convert the part of English characters included in it.Therefore, if you mainly handle Chinese content, these filters may not be applicable, and you may need to seek other customized string processing solutions.Can I use multiple filters on a variable at the same time?Yes, Anqi CMS template filters support chaining. You can use the pipe symbol
|Connect multiple filters together to process the same variable continuously. For example{{ myVar|lower|capfirst|safe }}Will firstmyVartranslate to lowercase, then capitalize the first letter, and finally mark it as safe output.How can I display a field in uppercase if the field may be an empty string or undefined, and how to set a default value?You can combine the use of
defaultfilter.defaultThe filter can provide a default value when a variable is empty or undefined. For example, if you want a product code to be displayed in uppercase and show "N/A" when it is empty:{{ productCode|upper|default:"N/A" }}so if
productCodeIf empty, it will display "N/A"; ifproductCodethere is a value (such as "abc"), it will display "ABC".