The template syntax of Anqi CMS is borrowed from the Django template engine, which means we can easily achieve these needs with its built-in rich filters.These filters act like mini-tools, capable of processing variables so that the final displayed content meets our expectations.
How to achieve the capitalization of the first letter of the string:capfirstFilter
If you want the first letter of a string to be capitalized and the rest to remain the same,capfirstThe filter is the tool you need. It checks the first character of the string, and if it is an English letter, it converts it to uppercase; otherwise, it does nothing.
This filter is very suitable for use at the beginning of a sentence or when you want to capitalize the first letter of a single English word, for example, when displaying user-submitted text that may not have proper capitalization.
Example Usage:
Suppose you have a variableproductNameThe stored value is"anqicms模板制作", if you want to capitalize the first letter, you can write it like this in the template:
{{ "anqicms模板制作"|capfirst }}
The output will beAnqicms模板制作You see, only the first character of the string was converted to uppercase. If the string ishello world, the output will beHello world.
Implement the capitalization of the first letter of each word:titleFilter
In contrast,titleThe filter is more suitable for title-like content. Its function is to capitalize the first letter of each English word in a string and convert the rest to lowercase.
This is particularly useful when displaying article titles, product names, or proprietary terms, as it can make the text look more standardized and formal. For example, you may receive a title from a user that does not strictly adhere to the rules of capitalization.titleFilter can help you standardize with one click.
Example Usage:
If your variable isarticleTitleThe stored value is"anqicms模板中如何实现字符串首字母大写"or even"HELLO tHERE!"aftertitleAfter the filter process, they will all be displayed in the format of each word with the first letter capitalized:
{{ "anqicms模板中如何实现字符串首字母大写"|title }}
The output will beAnqicms模板中如何实现字符串首字母大写."HELLO tHERE!",The output will beHello There!. This greatly ensures the consistency and professionalism of the title display.
Auxiliary case conversion:upperandlowerFilter
Except for these two main ways of capitalizing the first letter, the security CMS template also provides filters to convert the entire string to uppercase or lowercase, respectively,upperandlower。They each have a very intuitive function:
upperThe filter will convert all uppercase letters in the string to uppercase.lowerThe filter will convert all uppercase letters in the string to lowercase.
They come in handy when you need to emphasize a word, such as website copyright information, or when you need to unify text formats (such as converting all user tags to lowercase for easy processing).
Example Usage:
{{ "AnQiCMS 内容管理系统"|upper }}
{{ "AnQiCMS 内容管理系统"|lower }}
The output results will beANQICMS 内容管理系统andanqicms 内容管理系统.
Recommended for actual application
In practice, we usually will be in the document'sTitlefields (such as article titles, product names),Description(Description)or other custom text fields use these filters. For example, you can use data obtained from the background.{{ archive.Title }}Add it.|titleFilters to ensure that all article titles are displayed in a standardized format.
It is worth noting that these filters only affect the content on the pageDisplay effectIt will not change the original data stored in the background database.This means you can flexibly adjust the display of the same text according to different display requirements without modifying the original data.Through such processing, the professionalism and readability of the website content can be significantly improved, providing visitors with a better browsing experience.
Provided by the Anqi CMS templatecapfirst,