ingeniously utilizecapfirstFilter: Capitalize the first letter
For converting the first letter of an English string to uppercase, Anqi CMS provides a very intuitive and efficient template filter: capfirstThis filter will identify the first letter of the English string and convert it to uppercase, while the rest of the string maintains the original letter case format.
For example, if you have a variable{{ archiveTitle }}with the value"hello world"And hope 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.
Except for the first letter being capitalized, you may also encounter other needs for letter case conversion in content operation. The Anqi CMS also provides the corresponding filters to meet these situations:
1. Convert the entire English string to lowercase:lower
Sometimes, you may need to convert the entire English string to lowercase for unified display style or data matching. At this time, 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 English 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 the title or phrase, we often need to capitalize the first letter of each word to comply with common title formats.titleThe filter is born for this:
{{ "anqi cms is great"|title }}
{# 输出: Anqi Cms Is Great #}
Example of practical application scenarios
These filters are very practical in a variety of security CMS template scenarios. For example, when displaying the article title on the article detail page, we usually want the first letter of the title to be capitalized, even if the editor did not pay attention to it when entering 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 ensure that the first letter is capitalized:
<span class="site-name">{{ system.SiteName|capfirst }}</span>
Flexible filter chain calling
The template filters of Anqi CMS support chained calls, which means you can connect multiple filters together to process the data in a series of steps. For example, if you are unsure about the original case of a string but want it to be capitalized at the beginning, you can first convert it to lowercase and then perform the capitalize operation:
{{ "AnQi CMS"|lower|capfirst }}
{# 先转小写为 "anqi cms",再首字母大写为 "Anqi cms" #}
Moreover, if your string may contain HTML code and you wish for HTML tags to be parsed correctly rather than escaped, you can add it at the end of the filter chain.safefilter. For example:
{{ "<strong>hello world</strong>"|capfirst|safe }}
{# 输出: <strong>Hello world</strong> #}
Through the flexible use of these string filters provided by the security CMS, you can easily control the display format of text, enhancing the professionalism and user experience of the website content.These tools are not only simple and easy to use, but also powerful, and they are a powerful assistant for you to carry out safe CMS template development and content operation.
Common Questions (FAQ)
Do these string filters work for Chinese character strings?These filters (
capfirst/lower/upper/titleMainly deals with English characters.When they encounter Chinese character strings, they usually do not change the display of Chinese characters, or they may only convert the English characters contained within.Therefore, if you mainly deal with 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 single variable?Yes, the template filters of Anqi CMS support chaining calls. You can use the pipe operator
|to connect multiple filters together and process the same variable continuously. For example{{ myVar|lower|capfirst|safe }}it will first convertmyVarConvert to lowercase, then capitalize the first letter, and mark it as safe output.If I want to display the value of a field in uppercase, but sometimes the field may be an empty string or undefined, how can I 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 it is empty, it will display “N/A”;if it has a value (such as “abc”), it will display “ABC”.productCodeIf it has a value (such as “abc”), it will display “ABC”.