In website content operation, the way content is presented often determines the overall impression of users on the website.Whether it is to maintain brand style consistency or improve the readability of text, flexible case transformation of strings is a basic and important operation.AnQiCMS (AnQiCMS) understands this, having built-in simple and easy-to-use filters (Filters) in its powerful template engine, allowing you to easily perform string transformations such as uppercase, lowercase, and capitalize without complex programming.

We will introduce in detail how to use these filters in Anqi CMS templates to precisely control the case of text.

Convert the string to uppercase:upperFilter

When you need to emphasize a word or phrase, or in a specific context (such as warning information, brand name) where all English characters are required to be in uppercase, upperThe filter comes into play. It converts all letters in the string to uppercase.

Usage:

In the template, you can use the pipe character|toupperApply the filter to any variable.

{{ 您的变量 | upper }}

Example:

Assuming your variableproductNameThe value is"anqi cms"Then:

<p>产品名称:{{ productName | upper }}</p>

The output will be:

<p>产品名称:ANQI CMS</p>

It should be noted that,upperThe filter will only convert English letters, and Chinese or other non-English characters will remain unchanged. For example,"你好 anqi cms"afterupperThe filtered result is still"你好 ANQI CMS".

Convert the string to lowercase:lowerFilter

withupperThe filter is relative,lowerThe filter is used to convert all English letters in a string to lowercase. This is very useful for maintaining consistency in text, especially when dealing with user input or standardizing data.

Usage:

Similarly, use the pipe character tolowerapply the filter to your variable.

{{ 您的变量 | lower }}

Example:

If your variabletagTextThe value is"SEO OPTIMIZATION"Then:

<p>标签:{{ tagText | lower }}</p>

The output will be:

<p>标签:seo optimization</p>

withuppersimilar,lowerThe filter only acts on English characters, and is invalid for Chinese and other non-English characters.

Capitalize the first letter of a string:capfirstFilter

capfirstThe filter is designed to capitalize the first letter of a string while keeping the rest of the string unchanged.This is very useful when you need to convert a common sentence into a standard sentence with the first letter capitalized.

Usage:

{{ 您的变量 | capfirst }}

Example:

Assuming your variablesentenceThe value is"this is a great article."Then:

<p>{{ sentence | capfirst }}</p>

The output will be:

<p>This is a great article.</p>

Please note,capfirstOnly handles strings ofthe first character. If the first character of the string is not an English character, it will not be converted.

Convert the string to title case:titleFilter

titleThe filter will capitalize the first letter of each word in a string and convert the rest of the letters to lowercase.This is very suitable for article titles, chapter titles, or any text that requires 'titling'.

Usage:

{{ 您的变量 | title }}

Example:

If your variableblogTitleThe value is"how to optimize your anqi cms website"Then:

<h2>{{ blogTitle | title }}</h2>

The output will be:

<h2>How To Optimize Your Anqi Cms Website</h2>

titlefilterer thancapfirstEven stronger, as it will iterate over each word in the string and perform case processing to ensure that each word starts with an uppercase letter and the rest is lowercase.

By mastering these simple and practical string transformation filters in the Anqi CMS template engine, you will be able to control the display style of website content more flexibly, enhance the professionalism and user experience of the content.In order to unify brand image or optimize search engine display, these tools will be a powerful assistant for your content operation.

Frequently Asked Questions (FAQ)

1. Do these case conversion filters work for Chinese or other non-English characters?

No, theseupper/lower/capfirstandtitleThe filter mainly targets English characters for case conversion. When applied to a string containing Chinese or other non-English characters, these non-English characters will remain unchanged, and only the alphabetic characters within them will be converted according to the filter rules.

2. Besides case conversion, what are some common string processing filters in AnQi CMS?

AnQi CMS provides a rich set of string processing filters, such as:

  • truncatecharsortruncatewords: Used to truncate strings or HTML content to a specified length and add an ellipsis.
  • join: Concatenate array elements into a single string.
  • replace: Replace a specific substring in a string.
  • striptagsorremovetagsRemove HTML tags.
  • urlizeorurlencode: Handle URL links to make them clickable or encode them. These filters can help you handle and display text content more flexibly in templates.

3. Can I apply multiple filters to a variable in a chain? For example, first convert it to lowercase and then capitalize the first letter?

Yes, Anqi CMS template engine supports chained filter calls. You can use the pipe symbol continuously.|Apply multiple filters to a variable. For example:

{{ "HELLO world" | lower | capfirst }}

This code will first convert"HELLO world"Convert to lowercase"hello world"Then capitalize the result and output it"Hello world".