How to convert a string to uppercase, lowercase, or title case?

Calendar 👁️ 90

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".

Related articles

How to perform basic arithmetic operations in the template?

In Anqi CMS template design, it can flexibly handle data, perform simple calculations, and is an indispensable part of creating dynamic and functional websites.You may find that in addition to displaying the content itself, it is also necessary to perform some basic arithmetic operations at the template level, such as calculating the total price of goods, displaying the discounted price, or dynamically adjusting the style of elements based on numerical values.The AnQi CMS template engine (similar to Django syntax) provides us with intuitive and powerful arithmetic calculation capabilities, allowing these requirements to be easily implemented in the template.###

2025-11-09

How to automatically convert URL strings in text to clickable links?

In daily content operations, we often need to mention various URLs in articles, product descriptions, or page introductions.If these URLs are displayed as plain text, users will need to manually copy and paste them to access, which undoubtedly will greatly reduce their reading experience and the interactivity of our website.How can we automatically make the URL strings in these texts clickable?In AnQi CMS, this implementation is very simple, no complex code development is required, just use its powerful template filter function to easily handle it.

2025-11-09

How to truncate long text content and add an ellipsis (...)

In the daily management of website content, we often encounter such situations: an article's summary, a product description, or a list item title, if the content is too long, it not only occupies too much page space but may also destroy the overall layout aesthetics.Especially on mobile and other small screen devices, overly long text can seriously affect user experience.How to elegantly truncate these texts and add common ellipses (...) to make the information complete and tidy, which is a very practical skill in content operation.

2025-11-09

How to safely display HTML content retrieved from a database on the frontend without escaping?

In website operation, we often need to display some content with rich formatting, such as mixed text and images on article detail pages, HTML tables on product introduction pages, or interactive codes embedded in custom pages.This content is usually stored in a database and rendered on the front-end page.However, many content management systems (including the familiar AnQiCMS) by default, for website security, will escape the HTML content retrieved from the database, resulting in the front-end displaying the original HTML code rather than the expected effect.

2025-11-09

How to display a default value when a variable is empty?

In website operation, we often encounter such situations: a content field may not be filled in for various reasons, such as the author of the article, image description, or specific parameters of a product.If the template outputs a blank directly when displaying this content, it not only affects the aesthetics but may also confuse the user.At this time, how to display a friendly default value when a variable is empty has become a very practical skill.The AnQi CMS template system, with its flexible and powerful features, provides us with various methods to elegantly solve this problem

2025-11-09

How to get the length of a string, array, or object?

When managing website content in AnQi CMS, we often need to flexibly adjust the page display according to the number of elements in a string, array, or collection.Whether it is to judge whether the length of the article title needs to be truncated, or to count how many documents are under a certain category, or to display the number of user comments, obtaining the 'length' of this content is the key to dynamic display and logical judgment.The AnQi CMS template engine provides built-in features that are simple and efficient, helping us easily deal with these scenarios.Among them, the `length` filter is a powerful assistant for obtaining the length of strings, arrays, or key-value pairs

2025-11-09

How to format a floating-point number to a specified number of decimal places?

How to accurately control the decimal places of floating-point numbers in AnQiCMS?In website content operations, we often encounter situations where we need to display prices, statistical data, measurement results, and other floating-point numbers.However, the original floating-point numbers often have unnecessary decimal places, which not only affects the appearance but may also reduce the readability of the data.AnQiCMS fully considers this user's needs, providing flexible and powerful template filters to help us easily implement the formatting of floating-point numbers, making data display more professional and accurate.### Use `floatformat`

2025-11-09

How to center or align a string to a specified length?

When building website content, we often need to fine-tune the text layout to ensure that the information presented is both beautiful and clear.Whether it is a list, table, product parameters, or other structured display data, maintaining visual alignment and consistency is crucial for improving user experience.Aq CMS, known for its high flexibility and ease of use as a content management system, deeply understands the importance of content presentation and has built a series of powerful template filters to help us easily align and center strings

2025-11-09