In website content operation, we often hope that the article title can accurately convey information while maintaining visual neatness and professionalism.However, sometimes due to specific requirements or content import, some unnecessary special characters may appear in the title, which not only affect the appearance but may also cause layout confusion in certain display scenarios.cutIt can help us accurately remove all specific special characters from the article title, making the title look fresh.
UnderstandingcutFilter
The AnQiCMS template system is powerful and flexible, and it is built with various filters to process and format output data.cutOne of the filters is its core function isRemove all specified characters from any position in a stringThis means that you can tell it which characters to remove, and it will 'cut' these characters out of the target string without changing the order of the rest.
For example, if we have a title that is 'How to use 'AnQiCMS' for website optimization?' and we want to remove the book title marks《》and the question mark?,cutThe filter can be used.
Why is it necessary to clean up special characters in article titles?
A tidy title not only enhances the user's reading experience and makes information delivery more efficient, but also looks more visually appealing. It is particularly important to remove special characters in the following scenarios:
- Unified style:The website may require all titles to follow a specific style guide, such as not including certain punctuation.
- Avoid formatting issues:Some special characters may be displayed abnormally in different fonts or devices, leading to garbage or misalignment.
- Conciseness:For a limited title display area (such as list pages, search results), removing unnecessary characters can make the title more concise.
- Data cleaning:When importing articles from external systems, the title may contain irregular special characters that need to be cleaned up uniformly.
cutHow to use the filter
cutThe syntax of the filter is very intuitive:
{{ 变量 | cut:"要移除的字符" }}
Here are the变量It usually refers to your article title (for example)item.TitleorarchiveTitle), while要移除的字符is a string containing all the special characters you want to remove from the title.
It is worth noting that,要移除的字符Parameters is a string where each character is considered as an independent removal target.cutThe filter does not treat it as a regular expression for pattern matching, but performs simple character removal.
Actual case demonstration
To better understandcutThe usage of the filter, we illustrate it with several examples.
Assuming we have the following original article titles:
"AnQiCMS教程-高效移除特殊字符""产品发布:全新功能上线![限时优惠]""如何使用《AnQiCMS》进行标题优化?""网站运营策略 -- AnQiCMS 内容优化"
Case one: Remove a single special character
If we want to remove the hyphen from the title 'AnQiCMS Tutorial - Efficiently Remove Special Characters'-:
{% set original_title = "AnQiCMS教程-高效移除特殊字符" %}
<p>原始标题:{{ original_title }}</p>
<p>清理后标题:{{ original_title | cut:"-" }}</p>
The output result will be:
原始标题:AnQiCMS教程-高效移除特殊字符
清理后标题:AnQiCMS教程高效移除特殊字符
Case Two: Remove Multiple Different Special Characters (English Punctuation)
Now, let's handle the title 'Product Release: New Features Launched! [Limited Offer]' and want to remove the English colon:and exclamation mark!and brackets[and]:
{% set original_title = "产品发布:全新功能上线![限时优惠]" %}
<p>原始标题:{{ original_title }}</p>
<p>清理后标题:{{ original_title | cut:":![]" }}</p>
Please note that all characters to be removed are placed in the same string":![]".
The output result will be:
原始标题:产品发布:全新功能上线![限时优惠]
清理后标题:产品发布全新功能上线限时优惠
Case three: Remove Chinese special characters
For the title 'How to optimize the title of 'AnQiCMS' with Chinese book title marks and question marks?':
{% set original_title = "如何使用《AnQiCMS》进行标题优化?" %}
<p>原始标题:{{ original_title }}</p>
<p>清理后标题:{{ original_title | cut:"《》?" }}</p>
The output result will be:
原始标题:如何使用《AnQiCMS》进行标题优化?
清理后标题:如何使用AnQiCMS进行标题优化
Case Four: Comprehensive Application, Remove Multiple Chinese and English Special Characters
If the title 'Website Operation Strategy – AnQiCMS Content Optimization' contains multiple hyphens, including Chinese long dashes--we would like to remove them as well as spaces:
{% set original_title = "网站运营策略 -- AnQiCMS 内容优化" %}
<p>原始标题:{{ original_title }}</p>
<p>清理后标题:{{ original_title | cut:"- — " }}</p> {# 这里的空格也会被移除 #}
The output result will be:
原始标题:网站运营策略 -- AnQiCMS 内容优化
清理后标题:网站运营策略AnQiCMS内容优化
The characters in the string (including spaces, English hyphens, and Chinese long dashes) are removed." - — "are removed from the string.
In the article list tag usingcutFilter
In practical applications, the most common scenario we encounter is with a list of articles (archiveList), where we clean up the titles of each article that is looped through.
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<h3>
{# 移除标题中的书名号、冒号、感叹号、方括号和问号 #}
{{ item.Title | cut:"《》:![]?" }}
</h3>
<p>{{ item.Description | truncatechars:100 }}</p>
<a href="{{ item.Link }}">阅读更多</a>
<hr>
{% empty %}
<p>目前没有可展示的文章。</p>
{% endfor %}
{% endarchiveList %}
Through this approach, you can flexibly control the display effect of article titles without modifying the original database data.
Precautions
- Do not modify the original data:
cutThe filter only acts on the output result during template rendering; it does not change the original article title data stored in the AnQiCMS backend database.This means you can adjust the filter parameters at any time without worrying about data loss or corruption. - Characters rather than patterns:[en]Again, I emphasize,
cutThe filter is based on character removal, not regular expressions. If you need more complex pattern matching replacements (such as replacing a specific substring), you may need to consider using the AnQiCMS providedreplaceFilter. - Choose characters to remove carefully:When using
cutWhen filtering, consider carefully which characters to remove to avoid deleting titles that become difficult to understand or lose important information.
PasscutFilter, AnQiCMS users can easily achieve fine-grained control over article titles, enhancing the professionalism and user experience of the website content.
Common Questions (FAQ)
Q1:cutDoes the filter modify my article title's original data in the background database?
A1:No.cutThe filter is a template filter that processes data only when the content is output to the frontend page. The article title you see in the AnQiCMS backend is the original one, which is not affected by it.cutThe effect of the filter.
Q2: What should I do if the special characters I want to remove are important parts of the article title, such as numbers?
A2: cutThe filter will remove all characters specified in the parameters.If a number or other character is a 'special character' in some titles and an 'important component' in others, you need to weigh or consider a more refined logic.cutParameters, or standardize the title format when editing in the background to avoid such conflicts.
Q3:cutfilters andreplaceWhat are the differences between the filters? Which one should I choose?
A3:These two filters have similar functions but also key differences.
cutThe filter will remove the parameters from the stringall single charactersFor example,"Hello world" | cut:"ol"It will remove all 'o' and 'l', the result is 'He'