In the daily use of AnQi CMS templates, flexibly handling text content is the key to enhancing website display and user experience.Whether it is the concise title of the article, 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.English CMS based on Django template engine syntax, providing a series of powerful and easy-to-use filters (Filters), making these operations simple and intuitive.

The next, we will delve into how to apply these filters in the security CMS template, making your content presentation more accurate and dynamic.

Text truncation and refinement: Making content just right

When we need to display long text on list pages or summary areas, appropriate truncation can keep the page tidy and guide users to click to view the full content.The CMS provides various truncation filters for this.

truncatecharsandtruncatewords:Automatic truncation to maintain readability

truncatecharsThe filter will truncate the string to the specified number of characters and add an ellipsis (…) at the end. It truncates exactly to the character, even in the middle of a word.truncatewordsIt is even more intelligent, it will truncate based on 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 tag damage, affecting the page structure. In this case, it should be used._htmlVersion:truncatechars_htmlandtruncatewords_htmlThese two filters intelligently preserve 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.

sliceto precisely control text segments

If you need to more finely control the starting and ending positions of the extracted text,sliceFilter is a good choice. It allows you to extract a part of the string by specifying the index range, similar to the slice 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: Clear extra characters

To maintain the neatness of the content, we often need to remove whitespace characters at the beginning or end of the text, as well as specific symbols.trimThe series of filters can help you complete this task:

  • trim: Delete leading and trailing whitespace characters of a string. If a parameter is specified, delete the matching specific characters at the beginning and end.
  • trimLeft: Delete leading whitespace characters or specific characters from the beginning of a string.
  • trimRight: Delete trailing whitespace or specific characters.
{# 去除字符串两端的空格 #}
<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 meet the needs of page design or content emphasis.

upper,lower,capfirst,titleEnglish: Easily switch text style.

  • upperEnglish: Convert all letters in a string to uppercase.
  • lowerEnglish: Convert all letters in a string to lowercase.
  • capfirst: Convert the first letter of a string to uppercase.
  • title: Convert 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,rjust: Adjust the alignment and padding of text.

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

  • center: Center-align a string within a specified width space and fill the sides with spaces.
  • ljust: Left-align a string within a specified width space and fill the right side with spaces.
  • rjust: Right-align the string within a specified width 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

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

addThe 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 a string, it will concatenate them.

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

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

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

joinCombining 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 in handy. It is usually used withsplitormake_listFilter combination use, the latter is used to split strings into arrays.

English

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