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 %} {# 这里仅仅是