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

The Anqi CMS adopts a template engine syntax similar to Django, which means that when using variables in templates, you can use the pipe symbol|Introduce various "filters" to process the data. These filters are like small tools that can help us convert strings, format dates, perform calculations, etc.For the case of case conversion of English strings, Anqi CMS provides several very practical filters to make your content more standardized and beautiful.

to elegantly format strings:capfirstandtitleFilter

Let's take a look at two core filters that can help you easily implement the title case or all words capitalized of English strings:

  1. capfirstFilter: Sentence title caseIf you need to capitalize the first letter of an English string while keeping the rest of the string unchanged (unless it is already capitalized),capfirstThe filter is exactly your ideal choice. 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 standards.

    Usage example:Suppose you have a variable namedarticleTitleThe variable, its value is"this is a great article about anqicms".

    {{ articleTitle|capfirst }}
    

    Output Result: This is a great article about anqicms

    You can see that only the first letter 't' of the entire string has been changed to uppercase 'T'.

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

    Usage example:Continue from the previousarticleTitleThe variable has the value"this is a great article about anqicms".

    {{ articleTitle|title }}
    

    Output Result: This Is A Great Article About Anqicms

    withcapfirstdifferent,titleThe filter has processed each word in the string, capitalizing the first letter.

Extended application: full uppercase and full lowercase (upperandlowerFilter)

In addition to the two common needs for capitalizing the first letter of each word, 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 the entire English string to uppercase, you can useupperfilter.

    Usage example:

    {{ "welcome to anqicms"|upper }}
    

    Output Result: WELCOME TO ANQICMS

  2. lowerFilter: Convert to lowercaseOn the contrary, if you need to convert all letters to lowercase, you can uselowerfilter.

    Usage example:

    {{ "WELCOME TO ANQICMS"|lower }}
    

    Output Result: welcome to anqicms

In actual use in the template

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

For example, in your article detail page template, you might 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) istitleprocessed by a filter to ensure that the first letter of each word is capitalized; andarchive.Category.Title(Category title) is beingcapfirstFiltered, only the first letter is capitalized. Document tagitem.Titleis also beingtitleFiltered and formatted 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 in the string, leaving the Chinese part unchanged.
  • Flexible combination application:The Anqi CMS filter supports chained calls. This means you can combine multiple filters to achieve more complex formatting requirements.For example, first convert the string to lowercase, then capitalize the first letter of each word:{{ myString|lower|title }}.
  • Consider|safeFilter:If the string you are processing may contain HTML tags and you want these tags to be parsed by the browser normally rather than as plain text, then you may need to add extra after applying the case filter|safea filter. 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 and not have to worry about complex string processing logic.By a simple line of code, your website content can be presented to users in the most standardized and attractive way.


Frequently Asked 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 characters, they will only perform case conversion on the alphabetic part of the string, and the Chinese part will remain unchanged. For example,"你好 world"|capfirstIt will output."你好 World".

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