How to truncate, convert case or concatenate strings in AnQi CMS templates?

Calendar 👁️ 70

In the daily use of Anqi CMS templates, flexibly handling text content is the key to improving website display and user experience.No matter the concise article title, the formatting of user comments, or the combination of product descriptions, mastering string truncation, case conversion, and concatenation techniques can make your content more expressive.The AnQi CMS based on the Django template engine syntax provides a series of powerful and easy-to-use filters (Filters) that make these operations simple and intuitive.

Next, we will delve into how to apply these filters in Anqi CMS templates, making your content more accurate and dynamic.

Text truncation and refinement: making content just right.

When it is necessary to display long text on the list page or in the summary area, appropriate truncation can keep the page neat and guide users to click to view the full content.SecureCMS provides various truncation filters for this.

truncatecharsandtruncatewords: Smart truncation, maintain reading continuity

truncatecharsThe filter will truncate the string to the specified number of characters and add an ellipsis (...) at the end. It truncates to the character level, even in the middle of a word.truncatewordsIt is more intelligent, it will truncate according to the number of words, ensuring that the truncation point is always between words, which is very useful for maintaining the semantic integrity of the text.

For example, if you have a long article summary and you want to truncate it to a specific length:

{# 假设archive.Description是文章描述 #}
{# 截断到20个字符(包括省略号) #}
<p>{{ archive.Description|truncatechars:20 }}</p>

{# 截断到5个单词(包括省略号) #}
<p>{{ archive.Description|truncatewords:5 }}</p>

If your text content contains HTML tags, directly using the above filter may cause the tags to be damaged, affecting the page structure. At this point, you should use their_htmlVersion:truncatechars_htmlandtruncatewords_htmlThese two filters intelligently retain the integrity of HTML tags during truncation, ensuring that the output HTML remains valid.

{# 截断HTML内容,保留标签结构 #}
<p>{{ archive.Content|truncatechars_html:50|safe }}</p>

It should be noted that when you are sure that the output content is safe HTML, it is usually paired with|safea filter to prevent HTML tags from being escaped.

slice: precise control of text fragments

If you need to control the start and end positions of text extraction more finely,sliceA filter is a good choice. It allows you to extract a part of the string by specifying an index range, similar to the slicing operation in programming languages.

{# 假设有一个字符串变量 message = "欢迎使用安企CMS模板" #}
{# 从第0个字符开始,截取到第5个字符(不包括第5个) #}
<p>{{ "欢迎使用安企CMS模板"|slice:":5" }}</p> {# 输出: 欢迎使用安企CM #}

{# 从第3个字符开始,截取到字符串末尾 #}
<p>{{ "欢迎使用安企CMS模板"|slice:"3:" }}</p> {# 输出: 使用安企CMS模板 #}

{# 从第2个字符开始,截取到第7个字符 #}
<p>{{ "欢迎使用安企CMS模板"|slice:"2:7" }}</p> {# 输出: 使用安企C #}

trim: Remove extra characters

In order to keep the content neat, we often need to remove whitespace characters at the beginning or end of the text, as well as specific symbols.trimA series of filters can help you complete this task:

  • trim: Remove leading and trailing whitespace from a string. If a parameter is specified, remove the matching characters from the beginning and end of the string.
  • trimLeft: Remove leading whitespace or specified characters from a string.
  • trimRightRemove trailing whitespace or specific characters from a string.
{# 去除字符串两端的空格 #}
<p>'{{ "  安企CMS  "|trim }}'</p> {# 输出: '安企CMS' #}

{# 去除字符串开头的特定字符 '欢迎' #}
<p>{{ "欢迎使用安企CMS"|trimLeft:"欢迎" }}</p> {# 输出: 使用安企CMS #}

{# 去除字符串结尾的特定字符 'CMS' #}
<p>{{ "欢迎使用安企CMS"|trimRight:"CMS" }}</p> {# 输出: 欢迎使用安企 #}

Case conversion: Flexibly display text style.

In different scenarios, we may need to convert text to uppercase, lowercase, or capitalize the first letter, etc., to adapt to page design or content emphasis needs.

upper,lower,capfirst,title: Switch text style easily

  • upper: Convert all letters in the string to uppercase.
  • lower: Convert all letters in the string to lowercase.
  • capfirstConvert the first letter of a string to uppercase.
  • titleConvert the first letter of each word in a string to uppercase.
{# 假设变量 product.Name = "anqicms content management system" #}
<p>大写:{{ product.Name|upper }}</p> {# ANQICMS CONTENT MANAGEMENT SYSTEM #}
<p>小写:{{ product.Name|lower }}</p> {# anqicms content management system #}
<p>首字母大写:{{ product.Name|capfirst }}</p> {# Anqicms content management system #}
<p>标题样式:{{ product.Name|title }}</p> {# Anqicms Content Management System #}

center,ljust,rjustAdjust text alignment and padding.

Although this set of filters is mainly used for text alignment and padding, they can also achieve a certain degree of 'transformation' or 'decoration' of strings, making them present different visual effects within a fixed-width space.

  • center: Center-align a string within a specified width space and fill both sides with spaces.
  • ljust: Left-align a string within a specified width space and fill the right side with spaces.
  • rjust: Align the string to the right within a specified width space and fill the left side with spaces.
{# 将 "Hello" 居中显示在20个字符的宽度内 #}
<p>'{{ "Hello"|center:20 }}'</p> {# 输出: '       Hello        ' #}

{# 将 "Hello" 左对齐显示在20个字符的宽度内 #}
<p>'{{ "Hello"|ljust:20 }}'</p> {# 输出: 'Hello               ' #}

{# 将 "Hello" 右对齐显示在20个字符的宽度内 #}
<p>'{{ "Hello"|rjust:20 }}'</p> {# 输出: '               Hello' #}

String Concatenation and Combination: Building Dynamic Content

Combining multiple strings or variables to form new text is a common requirement when dynamically generating content.

add: The bridge between adding numbers and concatenating strings

addThe filter can be used for arithmetic operations on numbers, as well as for simple concatenation of strings.If the operand is a number, it will attempt to perform mathematical addition;If it contains strings, it will concatenate them.

{# 数字相加 #}
<p>结果:{{ 5|add:2 }}</p> {# 输出: 7 #}

{# 字符串拼接 #}
<p>结果:{{ "安企"|add:"CMS" }}</p> {# 输出: 安企CMS #}

{# 混合拼接,数字会转换为字符串进行拼接 #}
<p>结果:{{ "版本"|add:3.0 }}</p> {# 输出: 版本3 #}

join: Combining list elements into a string

When you have an array (or list) of data and you want to concatenate all elements into a single string with a specific delimiter,jointhe filter comes into play. It is usually used withsplitormake_listThe filter is used in conjunction, the latter is used to split strings into arrays.

"`twig {# Assuming archives is a list of documents, we need to extract their titles and separate them with commas #} {% set titles = [] %} {% for item in archives %}

{% set titles = titles|add:item.Title %} {# 这里仅仅是

Related articles

How to display a custom Banner group in a single page in AnQi CMS?

In website operation, we often need to display a set of exquisite pictures on specific single pages, such as the carousel of product special pages, the promotional Banner of activity introduction pages, or the group photos showing the company's style on the "About Us" page.AnQiCMS fully considers such needs and provides an intuitive and efficient way for you to easily realize the display of custom banner group images on a single page without writing complex code.

2025-11-08

How to format timestamps in the AnQi CMS template to display them in a friendly date/time format?

In the display of website content, how to present the timestamp data stored in the background in a user-friendly and easy-to-read date or time format is a key factor in improving user experience.Anqi CMS deeply understands this, and provides a simple and efficient tag for template developers —— `stampToDate`, making this process effortless. ### Understanding Timestamps in AnQi CMS In many content models of AnQi CMS, such as articles, products, or categories, time data is typically stored in the form of a 10-digit timestamp.

2025-11-08

How does the Anqi CMS scheduled publishing function control the display time of article content?

In content operation, the timing of content release is often closely related to the propagation effect.To help website administrators plan and distribute content more flexibly and efficiently, AnQiCMS (AnQiCMS) provides a very practical feature - scheduled publishing.This feature allows you to precisely control the display time of article content on the website, realizing the automation and intelligence of content publishing.

2025-11-08

How to prevent content scraping in Anqi CMS and add watermark display to images?

In the era where content is king, the value of original content is self-evident, however, the problems of content plagiarism and image theft are becoming increasingly rampant.For website operators, how to effectively protect their hard work, prevent malicious collection of content, and add exclusive identification to images is a key link in maintaining brand image and content rights.AnQiCMS (AnQiCMS) is well aware of this pain point and has built-in features to address these challenges.

2025-11-08

How to implement a custom prompt display for a website in off-site status in AnQi CMS?

In website operations, for reasons such as maintenance, upgrade, or emergency handling, it is sometimes necessary to temporarily set the website to be offline.A good content management system (CMS) not only provides this basic function, but should also allow you to display custom prompts to visitors during the site shutdown to maintain a good user experience and convey necessary notifications.AnqiCMS (AnqiCMS) provides such a flexible and practical mechanism.Why do you need to shut down and display a custom prompt?The website shutdown does not mean a simple interruption of service, but is an important operational strategy

2025-11-08

How to retrieve and display detailed information of different user groups (such as VIP levels) in Anqi CMS template?

In Anqi CMS, implementing personalized information display based on the user's group membership, such as their VIP level, is a key step to improving website user experience and content monetization capabilities.AnQi CMS provides a powerful and flexible user group management function, and we can easily implement this requirement in the front-end template through simple template tag combinations. ### Understanding Anqi CMS User Groups and VIP System One of the core strengths of Anqi CMS is its user group management and VIP system.

2025-11-08

How does AnQi CMS manage and display website image resources, including categories and details?

In website operation, image resources play a crucial role, not only directly affecting user experience, but also being a key factor in the attractiveness of website content and search engine optimization (SEO).A high-efficient CMS system should provide a complete set of image management and display mechanisms.AnQiCMS (AnQiCMS) considers this aspect quite thoroughly, it provides a series of features to help users easily manage and optimize the image resources of their websites, whether it is from classification and archiving, intelligent processing to multi-scene display, it strives to be simple and efficient.

2025-11-08

How to set an independent display template and TDK for a specific Tag label in Anqi CMS?

Aq CMS provides a powerful and flexible content management system, where the Tag label is not only an important tool for content classification, but also a key link for fine-grained SEO optimization and improving user experience.Many operators may want to display unique visual styles for different Tag label pages, or configure exclusive search engine optimization information (TDK) to achieve more accurate marketing goals.Next, we will delve into how to achieve this goal in Anqi CMS.Understanding Tag Label Management in AnQi CMS

2025-11-08