In AnQiCMS template development, fine-grained string processing is a key factor in improving the quality of website content display.We often encounter such a scenario: the data read from the database, the content submitted by users, or the text imported from outside, may carry spaces at the beginning or end that we do not want, or even specific punctuation marks or prefix/suffix characters.These extraneous characters not only affect the visual neatness of the page, but may also cause unnecessary trouble to front-end layout, data validation, and even search engine optimization (SEO).

AnQiCMS uses a template engine syntax similar to Django, providing a rich set of filters (Filters) that allow us to easily perform various operations on string content.Today, let's delve into how to use these powerful filters to remove excessive spaces or specific characters from the strings in the AnQiCMS template, making your website content display more standardized and beautiful.

trimFilter: The all-rounder for slimming the beginning and end of strings

When we want to remove all extra spaces from both ends of a string (including spaces, tabs, line breaks, and other whitespace characters), or a specific set of characters at once,trimThe filter is your preferred tool. Its usage is very intuitive, allowing your string content to become clean and tidy quickly.

Basic usage: remove leading and trailing spaces

If you only want to remove whitespace from both ends of a string, no additional parameters are needed,trimThe filter will execute this operation by default:

{% set raw_string = "   这是包含多余空格的文本。   " %}
<div>原始字符串:'{{ raw_string }}'</div>
<div>处理后:'{{ raw_string|trim }}'</div>

Display result:

原始字符串:'   这是包含多余空格的文本。   '
处理后:'这是包含多余空格的文本。'

Advanced usage: Delete specific characters from the beginning and end

In addition to spaces,trimThe filter can also remove a specified set of characters from the beginning and end of a string. You just need to pass the characters to be removed as a string argument totrimFilter. For example, if you want to remove the symbols that may appear before and after the article title#/:

{% set raw_title = "### AnQiCMS 模板技巧!###" %}
<div>原始标题:'{{ raw_title }}'</div>
<div>处理后:'{{ raw_title|trim:"#!" }}'</div>

Display result:

原始标题:'### AnQiCMS 模板技巧!###'
处理后:' AnQiCMS 模板技巧 '

Please note,trimit will remove the characters in the parameter stringalllisted, regardless of their order. For example,trim:"ab"It will remove the 'a' or 'b' characters from the beginning and end of the string until no longer matching.

trimLeftandtrimRight: Precise control, delete from left to right.

Sometimes, we might only want to handle the left or right side of a string, rather than both sides. In this case,trimLeftandtrimRightfilters can provide more refined control.

trimLeftFilter: Remove leading characters of a string

trimLeftFilter specifically used to remove spaces or specified characters at the beginning of a string.

{% set message = "   尊敬的用户,您好!这是一条重要通知。   " %}
<div>原始消息:'{{ message }}'</div>
<div>删除前导空格后:'{{ message|trimLeft }}'</div>

{% set file_path = "//images/upload/image.jpg" %}
<div>原始路径:'{{ file_path }}'</div>
<div>删除前导斜杠后:'{{ file_path|trimLeft:"/" }}'</div>

Display result:

原始消息:'   尊敬的用户,您好!这是一条重要通知。   '
删除前导空格后:'尊敬的用户,您好!这是一条重要通知。   '

原始路径:'//images/upload/image.jpg'
删除前导斜杠后:'images/upload/image.jpg'

trimRightFilter: Remove trailing characters of a string

trimRightFilters focus on removing trailing spaces or specific characters you specify.

{% set price_str = "价格:¥199.00$$$" %}
<div>原始价格:'{{ price_str }}'</div>
<div>删除尾随符号后:'{{ price_str|trimRight:"$" }}'</div>

{% set url_str = "https://en.anqicms.com/products/" %}
<div>原始URL:'{{ url_str }}'</div>
<div>删除尾随斜杠后:'{{ url_str|trimRight:"/" }}'</div>

Display result:

原始价格:'价格:¥199.00$$$'
删除尾随符号后:'价格:¥199.00'

原始URL:'https://en.anqicms.com/products/'
删除尾随斜杠后:'https://en.anqicms.com/products'

What is the value of these filters in practical applications?

  • Unified content display:Ensure that all text fields (such as article titles, product names, category descriptions, etc.) do not have unnecessary leading or trailing spaces when displayed on the front end, to avoid visual confusion due to inconsistent data sources.
  • Optimize URL structure:When building dynamic links or pseudo-static paths,trimLeftandtrimRightIt can help you easily remove extra slashes, ensuring a concise and standardized URL structure, which is beneficial for SEO.
  • Clean up user input:Although front-end validation can prevent some problems, cleaning up at the back-end template level can further ensure that the data submitted by the user is tidy when displayed.
  • Process imported data:When importing a large amount of data from the outside, there are often inconsistencies in the format. These filters can be used to quickly clean the data to meet the display standards of the website.

When using these filters, please be sure to choose the most suitable according to your actual needs.Especially when you specify a particular character to be deleted, be careful to avoid deleting valid content in the middle of the string.trim/trimLeftandtrimRightYou will be able to control the string display effect in the AnQiCMS template more skillfully.


Frequently Asked Questions (FAQ)

Q1:trimFilters andcutWhat are the main differences of the filter?

A1: trimThe filter is mainly used to delete stringsBeginning and endspaces or a set of specified characters. Whilecutfor example, a filter (such as{{ "Hello world"|cut:" " }}is to remove the string containingallmatching specific characters wherever they appear (at the beginning, middle, or end). In short,trimAre trimming at both ends,cutIs a global replacement deletion.

Q2: Can I usetrimThe filter with other filters in a chain?

A2:Of course you can.AnQiCMS 's template filter supports chained calls, you can chain multiple filters together to perform multi-step processing on strings.

{% set mixed_string = "   HELLO WORLD   " %}
<div>处理后:'{{ mixed_string|trim|lower }}'</div>

This will delete firstmixed_stringThe leading and trailing spaces are removed, then the remaining 'HELLO WORLD' is converted to 'hello world'.

Q3: If I want to remove multiple spaces in the middle of a string instead of the spaces at the beginning and end, which filter should I use?

A3: trimThe filter only processes the beginning and end of strings. If you need to process the stringmiddleMultiple spaces (for example, change "Hello World" to "Hello World", you should usereplaceFilter, combined with regular expressions (if more complex matching is needed) or simple replacement to complete.

{% set messy_string = "Hello   AnQiCMS    World" %}
<div>删除中间多余空格后:'{{ messy_string|replace:"  , " }}'</div>

This example will replace all consecutive spaces in a string with a single space. If a more general solution is needed to handle any number of consecutive spaces and replace them with a single space, a more advancedreplaceClean the pattern or during data source processing.