In daily website content management, we often need to finely control the display format of English strings, such as capitalizing the first letter of the article title, or making each word of the product name start with a capital letter to enhance the professionalism and uniformity of the content.AnQiCMS (AnQiCMS) understands the importance of these subtle details to the website image, and therefore provides convenient and powerful string processing functions in template design. With built-in template filters, you can easily achieve these formatting needs.

The Anqi CMS uses a template engine syntax similar to Django, which means that when using variables in templates, you can format strings elegantly through the pipe character|Introduce various "filters" to process the data.These filters act like mini-tools, helping us to convert strings, format dates, perform calculations, and so on.For the case of case conversion of English characters, AnQi CMS provides several very practical filters to make your content presentation more standardized and beautiful.

Implement string formatting gracefully:capfirstandtitleFilter

Let's take a look at two core filters that can help you easily implement the capitalization of the first letter of English strings or the first letter of each word:

  1. capfirstFilter: Sentence CapitalizationIf you need to convert the first letter of an English string to uppercase while keeping the rest of the string unchanged (unless it is already uppercase),capfirstThe filter is exactly what you need. It is usually used at the beginning of a sentence to ensure that the first word of each sentence starts with a capital letter, in line with English writing conventions.

    Example Usage:Assuming you have a namedarticleTitlewith a value of"this is a great article about anqicms".

    {{ articleTitle|capfirst }}
    

    Output result: This is a great article about anqicms

    The first letter 't' in the entire string has been changed to uppercase 'T'.

  2. titleFilter: Capitalize the first letter of each wordWhen your requirement is to make a stringof each word.When converted to uppercase,titleThe filter comes into play. This filter is very suitable for use in titles, product names, or any text that needs to be displayed in a 'titleized' format, making each main word stand out.

    Example Usage:沿用刚才的articleTitle变量,其值为"this is a great article about anqicms".

    {{ articleTitle|title }}
    

    Output result: This Is A Great Article About Anqicms

    Withcapfirstdifferent,title过滤器处理了字符串中的每一个单词,将其首字母大写。

扩展应用:全大写与全小写 (upperandlower过滤器)

In addition to the two common capitalization requirements mentioned above, the Anqi CMS also provides filters to convert the entire string to uppercase or lowercase, to meet more comprehensive formatting requirements.

  1. upperFilter: Convert to uppercaseIf you need to convert all the letters of a whole English string to uppercase, you can useupperFilter.

    Example Usage:

    {{ "welcome to anqicms"|upper }}
    

    Output result: WELCOME TO ANQICMS

  2. lowerFilter: Convert to lowercase相反,if you need to convert all letters to lowercase, you can uselowerFilter.

    Example Usage:

    {{ "WELCOME TO ANQICMS"|lower }}
    

    Output result: welcome to anqicms

Actual usage in template

These filters are very flexible in the template of the Aanqi CMS.You can use them when displaying article titles, product names, tag (Tag) titles, even any English string data obtained from the backend.

For example, in your article detail page template, you may display the article title and category name like this:

<h1 class="article-title">{{ archive.Title|title }}</h1>
<p class="article-category">分类:<a href="{{ archive.Category.Link }}">{{ archive.Category.Title|capfirst }}</a></p>
<span class="article-tag">标签:
    {% tagList tags with itemId=archive.Id limit="5" %}
        {% for item in tags %}
            <a href="{{ item.Link }}">{{ item.Title|title }}</a>
        {% endfor %}
    {% endtagList %}
</span>

In this example,archive.Title(Article Title) wastitleprocessed by the filter to ensure that each word starts with a capital letter; whilearchive.Category.Title(Category Title) wascapfirstprocessed by the filter, only capitalizing the first letter. Document tagsitem.Titlewere alsotitleFilter formatting to make the display more unified and professional.

Some usage suggestions

  • Focus on content type:Remember, these case conversion filters are mainly designed for English strings.Although they can be applied to strings containing Chinese, they usually only affect the English letters part, leaving the Chinese part unchanged.
  • Flexible combination and application:The filter of Anqi CMS supports chained calls.This means you can combine multiple filters to achieve more complex formatting requirements.{{ myString|lower|title }}.
  • Consider|safeFilter:If the string you are processing may contain HTML tags, and you want these tags to be parsed normally by the browser rather than as plain text, you may need to add additional filters after applying the case filter.|safefilter. For example:{{ archive.Content|striptags|capfirst|safe }}Of course, **the practice is usually to clean and format the content before output, to avoid potential security risks.

These built-in filters of AnQi CMS greatly simplify the formatting work of front-end content, allowing website administrators and developers to focus more on the content itself without being bothered by cumbersome string processing logic.Through a simple line of code, your website content can be presented to users in the most standardized and attractive way.


Common Questions (FAQ)

1. Q: Do these case conversion filters work for Chinese content?A: These filters are mainly designed for English strings.When applied to a string containing Chinese, it will only perform case conversion on the alphabetical parts of the string, while the Chinese parts will remain unchanged."你好 world"|capfirstIt will output"你好 World".

2. Q: Can I apply multiple filters to the same string at the same time?A: Yes. The template filter of Anqi CMS supports chained calls. You can use the pipe symbol|Connect multiple filters together, each filter will operate on the result of the previous filter. For example,{{ myVariable|lower|title }}it will first convertmyVariablethe value is converted to lowercase, and then