In the template design of AnQi CMS, we often need to format the displayed content to meet the overall style of the website or specific display requirements.When encountering the need to convert the entire English string to lowercase, Anqi CMS provides a very convenient and intuitive template filter (Filter) to help us achieve this goal.
Core Solution: UselowerFilter
The template engine of AnQi CMS supports template syntax similar to Django, which includes many practical filters,lowerThe filter is one of them. Its function is to convert all English characters in the variable to lowercase without affecting other types of characters (such as Chinese characters or numbers).
The method of use is very simple:Only add a pipe symbol after the variable to be processed|andlower.
For example, if you have a variableproductNamehas a value of"AnQiCMS Content Management System"And hope to display in full lowercase in the template, you can write like this:
<p>原始名称:{{ productName }}</p>
<p>小写名称:{{ productName|lower }}</p>
The output of this code will be:
原始名称:AnQiCMS Content Management System
小写名称:anqicms content management system
It can be seen that the entire English string has been successfully converted to lowercase.
Actual application scenarios
lowerThe filter has a variety of practical application scenarios in website content operation:
- Standardized user input display:For example, the username or comment content filled in when the user registers, in order to maintain a unified display style, it can be converted to lowercase.
- Generate a friendly URL alias (Slug):Although Anqi CMS usually handles URL aliases automatically, in certain scenarios that require manual concatenation or processing, converting strings to lowercase is a common step to generate SEO-friendly URLs.
- Ensure consistency in display:The brand name, product features, or keywords on the website, in order to maintain visual consistency and avoid confusion caused by mixed case, are sometimes displayed in lowercase.
- Data comparison and filtering:Although it is mainly performed on the backend, at times the frontend may also need to convert strings to lowercase for case-insensitive initial comparison or classification display.
For example, in the document list loop, you may want to convert part of the title of each article to lowercase for some kind of identifier:
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<h3><a href="{{ item.Link }}">{{ item.Title }} - {{ item.Title|slice:":5"|lower }}...</a></h3>
{% endfor %}
{% endarchiveList %}
String processing filters
Exceptlowerbeyond this, the template engine of AnQi CMS also provides several commonly used string case transformation filters, which can be flexibly selected according to your specific needs:
upperFilter:WithlowerOn the contrary,upperThe filter will convert all letters in the English string to uppercase.{{ "hello world"|upper }} {# 输出:HELLO WORLD #}capfirstFilter:If you only need to capitalize the first English letter of the string and keep the rest unchanged, you can usecapfirst.{{ "anqicms"|capfirst }} {# 输出:Anqicms #}titleFilter:If you want to capitalize the first letter of each word in a string, for example for title formatting,titlethe filter is very applicable.{{ "anqicms content management system"|title }} {# 输出:Anqicms Content Management System #}
Tips and precautions for use
- the filter name must be accurate:In the template, when using filters, please make sure the filter name (such as
lower/upper) is spelled correctly and in lowercase, as they are case-sensitive. - Handling of non-English characters:
lower/upper/capfirstandtitleThese filters are mainly designed to operate on English characters. For Chinese characters or other non-Latin characters, they usually have no effect and will be displayed as they are. - Use in combination with other filters:You can adjust as needed:
lowerFilter combined with other filters. For example, if you want to remove spaces from both ends of a string first and then convert it to lowercase, you can usetrimfilter followed bylower:{{ " HELLO AnQiCMS "|trim|lower }} {# 输出:hello anqicms #}
Through these flexible filters, you can easily achieve various case conversion needs of strings in the Anqi CMS template, making your website content display more standardized and professional.
Common Questions (FAQ)
1.lowerFilter has effect on Chinese string?
lowerThe filter is mainly used to process English strings.It will only convert English letters to lowercase, and non-English characters such as Chinese characters, numbers, or punctuation marks will remain unchanged, being output as they are.
2. How to convert the first letter of a string to lowercase, or the first letter of each word to lowercase?If you need to convert the *first English letter* of a string to uppercase, you can usecapfirstthe filter. If you need to convert the *first letter of each word* to uppercase, you can usetitleFilter. Please note that there is no dedicated filter provided in the current AnQi CMS template to convert the first letter to lowercase, but it is usually done throughlowerFilter converts the entire string to meet most lowercase needs.
3. Can youloweruse the filter with other string processing filters?Yes, the template filters of Anqi CMS support chaining calls. You can use the pipe operator|Join multiple filters together, the processing order is from left to right. For example, you can use{{ 你的变量|trim|lower }}Remove the leading and trailing whitespaces from the string and convert it to lowercase.