In website content operation, we often hope that the article title can accurately convey information while maintaining visual cleanliness 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 chaos in some display scenarios.For this common question, AnQiCMS provides a very practical template filter-cutIt can help us accurately remove all specific special characters from the article title, making the title look fresh.
UnderstandingcutFilter
AnQiCMS's template system is powerful and flexible, with built-in filters to process and format output data.cutOne of the filters is its core function isRemove all specified characters from a string at any positionThis means, 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 other parts.
For example, if we have a title that is 'How to optimize a website using 'AnQiCMS'?' and we want to remove the book name marks《》and the question mark?,cutThe filter can be used.
Why is it necessary to clean up special characters in the article title?
A tidy title not only enhances the user's reading experience, but also makes information conveyance more efficient and visually appealing. It is particularly important to remove special characters in the following scenarios:
- Uniform style:The website may require all titles to follow a specific style guide, such as not including certain punctuation marks.
- Avoid layout issues:Some special characters may display abnormally on different fonts or devices, causing garbled or misaligned text.
- Simplicity: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变量It usually refers to the title of your article (for exampleitem.TitleorarchiveTitle),while要移除的字符This is a string containing all the special characters you want to remove from the title.
It should be noted that,要移除的字符The parameter is a string where each character is treated as an independent removal target.cutThe filter will not treat it as a regular expression for pattern matching, but will perform simple character stripping.
Actual case demonstration
In order to understand bettercutThe 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 - Efficient removal of special characters"-:
{% set original_title = "AnQiCMS教程-高效移除特殊字符" %}
<p>原始标题:{{ original_title }}</p>
<p>清理后标题:{{ original_title | cut:"-" }}</p>
The output will be:
原始标题:AnQiCMS教程-高效移除特殊字符
清理后标题:AnQiCMS教程高效移除特殊字符
Case two: Remove multiple different special characters (English punctuation)
Now, let's handle the title "Product Release: New Feature Launched! [Limited Offer]", which you want to remove the English colon from:Exclamation mark!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 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 will be:
原始标题:如何使用《AnQiCMS》进行标题优化?
清理后标题:如何使用AnQiCMS进行标题优化
Case 4: Comprehensive application, remove various Chinese and English special characters
If the title “Website Operation Strategy – AnQiCMS Content Optimization” contains multiple hyphens, including Chinese long dashes--, we hope to remove them and spaces:
{% set original_title = "网站运营策略 -- AnQiCMS 内容优化" %}
<p>原始标题:{{ original_title }}</p>
<p>清理后标题:{{ original_title | cut:"- — " }}</p> {# 这里的空格也会被移除 #}
The output will be:
原始标题:网站运营策略 -- AnQiCMS 内容优化
清理后标题:网站运营策略AnQiCMS内容优化
It can be seen that all characters contained in" - — "in the string (including spaces, English hyphens, and Chinese long dashes) have been removed.
are used in the article list tag.cutFilter
In practical applications, the most common scenario is in the article list (archiveList) where the title of each article retrieved in the loop is cleaned up.
{% 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 %}
In this way, you can flexibly control the display effect of the article title without modifying the original data in the database.
Points to note
- Without modifying the original data:
cutThe filter only affects the output result of template rendering; it will 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 damage. - Character rather than pattern:Emphasize again,
cutThe filter is based on character removal, not regular expressions. If you need more complex pattern matching replacement (such as replacing a specific substring), you may need to consider using the one provided by AnQiCMS.replacefilter. - Carefully choose characters to remove:While using
cutWhen filtering, carefully consider which characters to remove to avoid mistakenly deleting and making the title difficult to understand or losing important information.
BycutFilter, AnQiCMS users can easily implement fine-grained control over article titles, enhancing the professionalism and user experience of the website content.
Frequently Asked Questions (FAQ)
Q1:cutDoes the filter modify my article title's original data in the background database?
A1:No.cutA filter is a template filter that processes data only when the content is output to the front-end page. The article title you see in the AnQiCMS backend is the original, and it will not be affectedcutThe impact of the filter.
Q2: If the special characters I want to remove are themselves important parts of the article title, such as numbers, how should I handle them?
A2: cutThe filter will remove all characters specified in the parameters. If numbers or other characters are 'special characters' in some titles and 'important components' in others, then you need to weigh or consider more refined logic.A method is to use different articles for specific models or categoriescutParameters, 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 filters have similar functions but there are key differences.
cutThe filter will remove the characters in the parameter stringof single character occurrencesFor example,"Hello world" | cut:"ol"It will remove all 'o' and 'l', resulting in 'He'