In website operation, the clarity of content presentation and user experience are crucial.Sometimes, the content entered from the background or the fields obtained from the database may contain some unnecessary characters, such as extra spaces, specific delimiters, or leading text that needs to be cleaned up. All these may affect the appearance of the page and the readability of the content.AnQiCMS as an efficient and flexible content management system, provides a powerful template engine and rich filter functions, allowing us to easily handle these strings at the template level and optimize the final display effect.

This article will delve into how to use built-in filters in AnQiCMS templates to remove specified characters from strings, making your website content look fresh.

Use flexiblycutFilter: Precisely remove characters at any position

When you need to precisely remove one or more specific characters from any position in a string,cutFilter is your powerful assistant. It will search the entire string for all matched characters (or substrings) and remove them. This is very effective for cleaning up irregularly inserted symbols or punctuation.

For example, you have a product title, and due to the reason of data import, there may be an unnecessary special character at the middle or end of it, such as产品名称+/产品名称*. In this case, you can usecutFilter to clean up.

Example of usage:

Assuming your product title variable isproduct.TitleYou want to remove all the plus signs in it+:

{# 原始标题可能显示为 "高品质T恤+" #}
<div>清理后的标题:{{ product.Title | cut:"+" }}</div>
{# 输出结果:高品质T恤 #}

If the character you need to remove is not a single character, but a fixed substring, such as an accidental appearance in some descriptions[广告]Mark:

{# 原始描述可能显示为 "这款手机[广告]性能卓越" #}
<div>清理后的描述:{{ product.Description | cut:"[广告]" }}</div>
{# 输出结果:这款手机性能卓越 #}

cutThe filter is direct and efficient, suitable for scenarios where you know exactly which fixed characters or substrings you want to remove.

use cleverlyreplaceFlexible combination of replacement and removal in filters

replace

Example of usage:

Assuming your product number might bePROD-001orSKU_002English, You want to remove it uniformlyPROD-orSKU_Prefix, so that it only displays pure number numbers:

{# 原始编号可能显示为 "PROD-12345" #}
<div>产品编号:{{ product.Code | replace:"PROD-," }}</div>
{# 输出结果:12345 #}

{# 如果有多种前缀,可以链式调用或者根据情况判断 #}
{# 原始编号可能显示为 "SKU_67890" #}
<div>产品编号:{{ product.Code | replace:"SKU_," }}</div>
{# 输出结果:67890 #}

replaceFilter is especially suitable for batch processing character sets with fixed patterns that need to be removed, as it can accurately replace the found substrings.

trimSeries filter: Process leading and trailing whitespace and specific characters

In many cases, excessive whitespace (spaces, tabs, newline characters) often appears at the beginning or end. AnQiCMS'strimSeries filters are specifically designed for such scenarios, they can help you easily clean the 'edges' of strings.

  • trimFilter:By default, it removes all leading and trailing whitespace characters (spaces, newline characters, etc.) in the string.You can also specify a set of characters, which will remove all matching instances of these specified characters from both ends of the string.
  • trimLeftFilter:Remove only the whitespace characters or specified characters at the beginning of the string.
  • trimRightFilter:Remove trailing whitespace characters or specified characters from the string.

Example of usage:

An English user submitted comment content, which may contain extra spaces or newline characters at the beginning and end due to reasons such as copy and paste.

{# 原始留言内容可能为 "  \n 欢迎使用 AnQiCMS! \t " #}
<div>{{ message.Content | trim }}</div>
{# 输出结果:欢迎使用 AnQiCMS! #}

Sometimes, the title or description may be enclosed in special characters, such as### 我的文章标题 ###You can usetrimto remove the leading and trailing###:

{# 原始标题为 "### 这是一个重要的通知 ###" #}
<div>{{ article.Title | trim:"#" }}</div>
{# 输出结果: 这是一个重要的通知 #}

Please note,trim:"#"is to removeallthe leading and trailing#characters, not to remove###This whole. If your requirement is to remove###As a whole,replacea filter may be more appropriate.

If you only care about removing the leading text of a string, such as some automatically generated prefixes:

{# 原始字段为 "ID: ANQI-001" #}
<div>{{ item.Identifier | trimLeft:"ID: " }}</div>
{# 输出结果:ANQI-001 #}

General principles and precautions for use

  1. Chained call filter:AnQiCMS template supports the chained invocation of filters, which means you can connect multiple filters together to process data in multiple steps. For example, remove special characters first, then truncate the length:{{ article.Content | cut:"[图]" | truncatechars:100 }}

  2. |safeFilter:When handling strings that may contain HTML code, such as content retrieved from a back-end editor, if other filters are applied, ensure that the HTML tags are correctly parsed and not displayed as plain text. Be sure to add at the end,|safefilter. For example:{{ article.FullContent | replace:"<p>","<span>" | safe }}

  3. Testing is crucial:Before deploying to the production environment, make sure to thoroughly test your template modifications in the development or testing environment, ensuring that the character removal logic meets expectations and does not accidentally delete important content or disrupt the page layout.

  4. Optimized content source:Although the template filter function is powerful, the most ideal situation is to standardize the content as much as possible from the background editing process of the content management system, thereby reducing complex processing on the front-end template.Use the filter as the last line of defense against inevitable data format issues.

These string processing filters in the AnQiCMS template are powerful tools to enhance the quality of website content display and optimize user experience.Master and apply them flexibly, which will help your website show a more professional and tidy appearance.


Common Questions (FAQ)

Question: I only want to remove all HTML tags from a string, which filter should I use?Answer: You can usestriptagsa filter. For example{{ article.Content | striptags | safe }}The string will remove all HTML tags and only keep the plain text content.

Q: If I remove the characters and it leads to a long string content, can I use the truncation feature in combination?答:Of course. You can chain call after removing characters from the filter.truncatecharsortruncatewordsFilter to truncate the string.