During the operation of the website, we often encounter unnecessary spaces, line breaks, or special characters in the content. These 'impurities' not only affect the appearance of the content and the reading experience of users, but may also have a negative impact on the layout of the page and search engine optimization (SEO) at times.AnQiCMS (AnQiCMS) is an efficient content management system that provides flexible and powerful template functions, and the built-in template filters are exactly the tools to solve such problems.

When you want to clean up strings before displaying content on the page, removing extra spaces or unnecessary characters, you don't need to modify the original data in the database. Just apply the appropriate filter at the template level, and you can easily achieve this goal.

Understanding the problem: Why is it necessary to remove extra characters?

Extra characters in the content may come from various sources:

  • Copy and paste:When copying content from other documents or web pages, additional spaces, line breaks, or format characters are often included.
  • Data importEnglish: When importing data in bulk, the source file may contain irregular characters.
  • English: Human input error.English: The editor may have accidentally pressed the space bar too many times or entered irrelevant characters during the input process.

These seemingly minor issues may accumulate to cause page layout disorder, inaccurate keyword density calculation, reduced user experience, and even affect the professional image of the website.Therefore, mastering how to clear these 'impurities' in the template is particularly important.

English translation: A powerful template filter of AnQiCMS

The template engine of Anqi CMS supports filter syntax similar to Django, allowing you to apply various processing functions to variable output. These filters are{{变量名|过滤器名称:参数}}The format of the form is used, which only affects the display effect of the content and does not modify the original data stored in the database, ensuring the integrity and security of the data.

Next, we will focus on several very practical filters for removing extra spaces or characters from strings.

1.trimSeries of filters: Remove leading and trailing whitespace or specified characters from strings.

trimSeries filters are the ideal choice for processing "dirty data" at the beginning and end of strings.

  • trimFilterIt can remove all whitespace characters from the beginning and end of a string (including spaces, tabs, newlines, etc.).If you specify a parameter, it will remove all specified characters from the beginning and end of the string.

    • Example: Remove extra spaces at the beginning and end of strings.
      
      {{ "   欢迎使用 安企CMS   "|trim }}
      {# 显示结果: "欢迎使用 安企CMS" #}
      
    • Example: Remove specific characters at the beginning and end of strings.
      
      {{ "---重要通知---"|trim:"-" }}
      {# 显示结果: "重要通知" #}
      
  • trimLeftFilter: withtrimIt is similar, but it only clears the whitespace or specified characters from the left (beginning) of the string.

    • Example: Clears the whitespace at the beginning of the string.
      
      {{ "   左侧有空格"|trimLeft }}
      {# 显示结果: "左侧有空格 " #}
      
    • Example: Clears the specified characters at the beginning of the string.
      
      {{ "### 标题"|trimLeft:"#" }}
      {# 显示结果: " 标题" #}
      
  • trimRightFilter: withtrimLeftRelative, it only clears the whitespace or specified characters from the right end of the string.

    • Example: Clears the spaces at the end of the string.
      
      {{ "右侧有空格   "|trimRight }}
      {# 显示结果: "右侧有空格" #}
      
    • Example: Clears the specified characters at the end of the string.
      
      {{ "产品名称.zip"|trimRight:".zip" }}
      {# 显示结果: "产品名称" #}
      

2.cutFilter: Remove specified characters from any position in a string

When you need to remove all occurrences of a specific character from a string, regardless of their position,cutThe filter comes into play.

  • ExampleEnglish: Remove all commas from the string.
    
    {{ "苹果,香蕉,橘子"|cut:"," }}
    {# 显示结果: "苹果香蕉橘子" #}
    
  • ExampleEnglish: Remove all hyphens from the string.-English: And underscores (_).
    
    {{ "产品-型号_V1.0"|cut:"-"|cut:"_" }}
    {# 显示结果: "产品型号V1.0" #}
    
    It is worth noting that,cutThe filter directly removes characters, if it removes spaces, it will remove all spaces, including the single spaces between words that you might want to keep. Therefore, when dealing with spaces, you may need more fine-grained control, which is wherereplaceFilter.

3.replaceFilter: Replace specific characters or strings

replaceThe filter is the most powerful one, it allows you to replace a substring with another substring in a string.This is very effective for unifying formats, reducing repeated spaces, or removing specific patterns of characters.

  • Usage:{{ 变量|replace:"旧子串,新子串" }}Comma-separated between the "old substring" and "new substring".

  • ExampleReplace all consecutive spaces in a string with a single space. Sometimes, a string may contain one or more consecutive spaces,replacewhich can help you standardize them.

    {{ "这是    一个   多余空格   的例子"|replace:"  "," " }}
    {# 显示结果: "这是  一个  多余空格  的例子" #}
    

    You may need to apply this filter multiple times to ensure that all consecutive spaces are replaced with a single space:

    {{ "这是    一个   多余空格   的例子"|replace:"  "," "|replace:"  "," "|replace:"  "," " }}
    {# 显示结果: "这是一个多余空格的例子" #}
    
  • ExampleRemove specific symbols from the string, such as brackets.

    {{ "产品名称 (型号)"|replace:"(",""|replace:")","" }}
    {# 显示结果: "产品名称 型号" #}
    
  • ExampleReplace newlines with spaces to display multi-line text as a single line.

    {{ "第一行\n第二行\n第三行"|replace:"\n"," " }}
    {# 显示结果: "第一行 第二行 第三行" #}
    

Combine with advanced techniques.

These filters can be chained as needed, for example:

{% set dirty_content = "   ##  产品名称-型号 (V1.0)   \n\n  更多描述  " %}
{{ dirty_content|trim|replace:"  "," "|replace:"  "," "|cut:"#"|replace:"-"," "|replace:"(",""|replace:")",""|replace:"\n"," " }}
{# 预期显示结果: "产品名称 型号 V1.0 更多描述" #}

After processing the string, if the original content may contain HTML tags and you want the browser to parse these HTML correctly (instead of displaying them as plain text), remember to add all cleaning filters after that.|safeFilter:

{% set html_content = "  <p>  一段 <b>格式化</b> 的文本。  </p>  " %}
{{ html_content|trim|replace:"  "," "|safe }}
{# 显示结果: <p> 一段 <b>格式化</b> 的文本。 </p> #}

Please notesafeThe position of the filter, it should be at the end of all string operations to ensure that the processed result is safely output as HTML.

By flexibly using these security CMS provided template filters, you can ensure that the display of website content always remains neat and professional, providing users with a better browsing experience, also