AnQiCMS's template engine uses syntax similar to Django, which includes many practical filters (Filters) that can help us easily handle text content, including case conversion.
UsecapfirstFilter: Implement capitalization of the first letter
If you want to capitalize the first letter of the English nameof the entire stringYou can usecapfirstFilter. This filter will only focus on the first character of a string, and if it is an English letter, it will convert it to uppercase while keeping the other characters unchanged.
Assuming you collect a user's name through a form, and use it in a template like this:user.FirstNameoruser.FullNameYou can use it in the template file like this:
{# 假设 user.FirstName 的值是 "john" #}
<p>欢迎您,{{ user.FirstName|capfirst }}!</p>
{# 显示结果将是:欢迎您,John! #}
{# 假设 user.LastName 的值是 "doe" #}
<p>姓氏:{{ user.LastName|capfirst }}</p>
{# 显示结果将是:姓氏:Doe #}
Through this simple filter, regardless of whether the user submits 'john' or 'JOHN', it passes throughcapfirstProcessed, all will be displayed in the form of "John", effectively standardizing the display format.
UsetitleFilter: Capitalize the first letter of each word.
很多时候,用户提交的姓名可能包含多个单词,比如英文全名 “john doe”。如果只使用 EnglishcapfirstFilter, then only the 'J' of 'john' will be uppercase, while the 'd' of 'doe' will remain lowercase, displaying as 'John doe', which may not be the effect we want.
In this case,titleFilter is more appropriate. It can capitalize the first letter of the stringof each word.Convert all to uppercase, and the rest to lowercase. This is very useful for displaying names in standard English format.
Let's look at an example:
{# 假设 user.FullName 的值是 "john doe" #}
<p>姓名:{{ user.FullName|title }}</p>
{# 显示结果将是:姓名:John Doe #}
{# 即使是 "peter van der merwe" 这种复杂一些的名字 #}
{% set userName = "peter van der merwe" %}
<p>姓名:{{ userName|title }}</p>
{# 显示结果将是:姓名:Peter Van Der Merwe #}
As you can see,titleThe filter can intelligently identify words and perform uppercase conversion, ensuring professionalism and consistency in the display of full names.
Actual application scenarios in AnQiCMS templates
These filters can be used in any AnQiCMS template file where user names need to be displayed, common application scenarios include:
- User profile page:Display the registration name in the user center in a uniform format.
- Display comments or messages:Display the visitor's name on the page after they submit a comment or message.
- Form submission confirmation page:Display the standard format of the submitted content to users after they submit information.
- List display of the backend management interface:Optimize the readability of the background data list.
You just need to locate to/templateThe website template file under the directory, find the output point you need to modify, and then apply these filters.The flexibility of AnQiCMS templates makes such customization needs within reach.
Some practical tips
- Chinese name processing:It is worth noting that,
capfirstandtitleFilter is primarily designed for English characters. If the user submits a Chinese name, it will not perform any uppercase conversion, and the Chinese characters will be output as they are. - Data source:The data submitted by the user will usually be processed through
requestbound to template variables, such as objects or other business logic,archive.Author/guestbook.UserNameor custom user model fields. When using it, please replace it with your specific data variable name. - Combined with other filters:You can also combine these filters with other string processing filters, such as first extracting a part of the content and then performing case conversion to meet more complex display requirements.
Through these powerful and easy-to-use template filters provided by AnQiCMS, we can easily implement automatic uppercase conversion of the first letter of English names submitted by users, making the website content show higher professionalism and consistency.
Common Questions (FAQ)
1. These filters will modify the user data stored in the database?
Answer: No.capfirstandtitleThe filter only affects the display process of data in the front-end template.They do not change the original stored data in your database, the original data will remain in the state when submitted by the user.If you need to store data in uppercase in the database, you need to handle it in the backend logic.
2. If the user submits a non-standard English format, such as 'john-doe' or 'macdonald',titlehow will the filter handle it?
Answer:titleThe filter usually identifies words by common separators such as spaces and hyphens.For “john-doe”, it may be identified as “John-Doe”; for “macdonald”, if it does not contain spaces or separators, the entire string will be considered as a single word, and it may remain as “Macdonald” or be processed according to the first letter capitalization rule.The specific effect should be tested in the actual template to ensure it meets expectations.
3. Where can I find more detailed information about AnQiCMS template filters and tags?
答:AnQiCMS provides very detailed official development documents.You can usually find these materials in the 'Template Design' or 'Help Documents' module on the website backend, especially in chapters such as 'Template Tags and Usage' and 'More Filters', where there are examples of various filters and tags that can help you understand and use AnQiCMS templates more deeply.