Using AnQi CMS for website content operations, the content collection module is undoubtedly an important way to obtain a large amount of information and quickly enrich the website content.However, text data collected or imported from external sources often contains unnecessary leading or trailing spaces, line breaks, or even other invisible characters.These seemingly minor 'dirty data' can affect the aesthetics of page layout, content alignment, and even cause subtle negative impacts on search engine optimization (SEO).

It's fortunate that Anqi CMS, based on the Django template engine syntax, provides a very convenient and powerful template filter feature, which can help us easily solve these troubles caused by invisible characters, making the collected content displayed cleanly and neatly in the template.

Core tool:trimFilter

The most common need in string data processing is to remove extra spaces or newline characters at the beginning and end. Anqi CMS'strimThe filter is designed for this. It can intelligently remove all default defined whitespace characters from both ends of the string, including conventional spaces, tabs, carriage returns, and line feeds.

Imagine that you have collected an article title from some information website, which may have quietly included a few leading spaces or trailing new lines.Display directly, which may cause the title to be misaligned with other elements on the page.trimA filter can make the content neat instantly.

Usage example:

{% set rawTitle = "   \n 独家解析:安企CMS高效内容管理技巧   \t " %}
<p>原始标题(包含非可见字符):'{{ rawTitle }}'</p>
<p>经trim处理后:'{{ rawTitle|trim }}'</p>

The above code will be displayed clearly on the page, even if there are multiple spaces and newline characters at both ends of the original string, aftertrimThe filter leaves only clean text content, and the quotes are tightly attached to valid characters.

In addition to the default whitespace characters, if your data contains other specific leading or trailing characters that need to be removed,trimThe filter can also handle it. You just need to pass these characters as parameters to it.

Example of removing specified characters:

{% set productCode = "###AQCMS-2023-PRO###" %}
<p>原始产品编码:'{{ productCode }}'</p>
<p>去除指定字符`#`后:'{{ productCode|trim:"#" }}'</p>

here,trim:"#"It will remove all characters from the beginning and end of the string.#Characters, so that the product code can be displayed asAQCMS-2023-PRO.

Fine control:trimLeftandtrimRightFilter

trimLeftandtrimRightThe filter can be used.

As the name implies,trimLeftThe filter only removes non-visible characters from the beginning of a string (or the specific characters you specify), whiletrimRightit focuses on clearing non-visible characters from the end of a string (or the specified characters).

trimLeftUsage example:

{% set introText = "   产品名称:高端定制CMS解决方案——安全稳定!" %}
<p>原始介绍文本:'{{ introText }}'</p>
<p>仅去除前导空格:'{{ introText|trimLeft }}'</p>

This code will only clearintroTextall leading spaces and newline characters, while any whitespace at the end of the string will be preserved.

trimRightUsage example:

{% set contactInfo = "联系方式:010-12345678 (客服在线)   " %}
<p>原始联系信息:'{{ contactInfo }}'</p>
<p>仅去除尾随空格:'{{ contactInfo|trimRight }}'</p>

withtrimLeftOn the contrary,trimRightit will only removecontactInfoTrailing spaces and newline characters are preserved, while any leading whitespace characters at the beginning of a string are also retained.

Of course,trimLeftandtrimRightIt also supports liketrimSimilarly, characters to be removed can be specified through parameters.

Remove the specified leading characters example:

{% set bulletPoint = "* 核心功能:多站点管理" %}
<p>原始列表项:'{{ bulletPoint }}'</p>
<p>去除前导`* `后:'{{ bulletPoint|trimLeft:"* " }}'</p>

When to use these filters?

These string processing filters are widely used in the daily content operations of AnQi CMS:

  1. Data cleaning after content collection:This is the most direct scenario.Regardless of the article title, abstract, or custom field content collected, it may be necessary to remove excessive characters from the beginning and end to ensure uniformity and aesthetics on the front end.
  2. Data submitted by the user:The user input content in comment sections, etc., to avoid large spaces from malicious or unintentional input affecting the layout, these filters can be used for preprocessing.
  3. Import data and unify the format:Batch import product information, article content, etc., the data source may have different formats, and the filter can quickly unify the display effect.
  4. SEO-related metadata:Page Title, Keywords, Description, etc., SEO information should be concise and non-redundant, which is crucial for search engine friendliness.

Tip

When applying these filters, remember that they are temporary processing at the content display layer and usually do not change the storage of the original data in the database.This means you can flexibly format the same data differently according to different display requirements.

Especially when dealing with rich text content (such as article body) if the content contains HTML tags, to ensure that the HTML tags are parsed correctly by the browser and not displayed as escaped, it is necessary to use them in conjunction.safeFilter. For example,{{ archive.Content|trim|safe }}First,trimremove both leading and trailing whitespaces, then,safeEnsuring HTML renders normally is a common **practice.

By mastering the use oftrim/trimLeftandtrimRightThese simple and powerful filters allow you to manage and display the content of Anqicms more efficiently, providing visitors with a better and more professional browsing experience.


Frequently Asked Questions (FAQ)

Q1: Why did I usetrimThe filter, but some of the HTML tags in the content are still displayed as escaped? For example, what I see is&lt;p&gt;instead of paragraphs.

A1:The template engine of AnQi CMS defaults to automatically escaping all output content to prevent cross-site scripting attacks (XSS).trimThe filter is responsible for removing non-visible characters from both ends of a string, but it does not change the escaping behavior. If you are dealing with a string containing HTML tags (such as article text), you need to use it simultaneouslysafeA filter is used to explicitly tell the template engine that this content is safe and does not need to be escaped. The correct usage is: {{ archive.Content|trim|safe }}.

Q2:trimDoes the filter remove spaces in the middle of a string? For example, will “Hello World” become “HelloWorld”?

A2:No.trimThe filter only removes strings.beginningandendingThe invisible characters, including spaces, newline characters, etc. Any spaces or character sequences in the middle of the string will be retained intact. Therefore, 'Hello World' goes throughtrimThe processed text remains 'Hello World', it will not become 'HelloWorld'.If you need to handle multiple consecutive spaces within a string, you can consider using other string processing methods or a replacement filter.

Q3: If the string I obtain from the content collection module contains non-standard Unicode whitespace characters (such as zero-width spaces),trimCan the filter automatically delete them?

A3:`