In AnQi CMS template design, we often need to format the displayed content to match the overall style of the website or specific display requirements.When encountering the need to convert an 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 AnQi CMS template engine supports a template syntax similar to Django, including 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 usage is very simple:Add a pipe symbol after the variable you need to process|andlowerJust do it.
For example, if you have a variableproductNameThe value is"AnQiCMS Content Management System"And you can write it in lowercase in the template, 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.
Application scenarios in practice
lowerThere are many practical scenarios for filters 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 uniform display style, it can be converted to lowercase.
- Generate a friendly URL alias (Slug):Although Anqi CMS usually automatically handles URL aliases, in certain scenarios that require manual concatenation or processing, converting a string to lowercase is a common step to generate SEO-friendly URLs.
- Ensure consistency in display:Brand names, product features, or keywords on the website may sometimes be displayed in lowercase for visual consistency and to avoid confusion in case of mixed case.
- Data comparison and filtering:Although mainly performed on the backend, the frontend may also need to convert strings to lowercase for case-insensitive initial comparisons or categorization displays.
For example, in a document list loop, you may want to convert part of the title of each article to lowercase to use as 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 filter related
exceptlowerIn addition, the template engine of Anqi CMS provides several commonly used string case conversion filters that 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 letter of the string while the rest remains 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 #}
Usage tips and precautions.
- The filter name must be accurate:When using filters in the template, please make sure that 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 aimed at English characters. For Chinese characters or other non-Latin characters, they usually have no effect, and they will be displayed as they are. - Use in conjunction with other filters:You can move as needed:
lowerThe filter can be used in combination with other filters. For example, if you want to remove the spaces at the beginning and end of a string first, and then convert it to lowercase, you can usetrimthe filter followed bylower:{{ " HELLO AnQiCMS "|trim|lower }} {# 输出:hello anqicms #}
With these flexible filters, you can easily achieve various string case conversion needs in the Anqi CMS template, making your website content display more standardized and professional.
Frequently Asked Questions (FAQ)
1.lowerDoes the filter have an effect on Chinese string?
lowerThe filter is mainly used to process English strings. It will only convert English letters to lowercase, while Chinese characters, numbers, or punctuation symbols and other non-English characters will not be affected, and they will remain unchanged.
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 capitalize the *first letter* of a string, you can usecapfirstfilter. If you need to capitalize the *first letter* of each word, you can usetitleFilter. Please note that the Anq CMS template does not provide a dedicated filter to convert the first letter to lowercase, but it is usually done bylowerThe filter can convert the entire string to meet the needs of most lowercase requirements.
3. Can youlowerUse the filter with other string processing filters.Yes, Anqi CMS template filters support chaining. You can use the pipe symbol|Combine multiple filters together, the order of processing is from left to right. For example, you can use{{ 你的变量|trim|lower }}First remove the leading and trailing spaces of the string, then convert it to lowercase.