In the daily content operation of AnQiCMS, we sometimes encounter situations where we need to carefully handle the content, such as removing specific characters, extra spaces, or stripping unnecessary HTML tags from the content.These operations are very crucial for ensuring the neatness of content display, compatibility with different front-end styles, and even for data cleaning.The Anqi CMS provides powerful template filter functions to help us efficiently complete these tasks.
Understanding the template filters of Anqi CMS
The template filters of AnQi CMS can be understood as a series of small tools that can transform and process the values of template variables and then output the processed results. Their usage is very intuitive, usually with a pipe symbol|Connect after the variable, followed by the filter name and optional parameters, for example{{ obj|filter_name:param }}By mastering these filters, you can have finer control over the website content.
Remove specific characters from the string
When we need to remove certain characters or substrings from a text, Anqi CMS provides several practical filters.
UsecutFilters perform precise deletion.
cutThe filter can precisely remove specific characters or substrings at any position in a string. It removes all matching target content and returns the remaining string.
Example of Use Case:Suppose some unnecessary special characters are mixed in your content, for example_or"You can usecutThe filter will remove them.
{{"这是一个_带有_下划线的_句子"|cut:"_"}}After processing, it will display as:这是一个带有下划线的句子
{{"这是"一个"带有"引号"的"句子"|cut:"\""}}After processing, it will display as:这是一个带有引号的句子
UsereplaceThe filter performs replacement deletion.
replaceThe filter can replace old content in a string with new content. If we set the new content to an empty string, it acts as a way to delete specific content.
Example of Use Case:If you want to delete a keyword or remove all specific characters, you canreplaceFilter implementation.
{{"欢迎使用安企CMS,安企CMS致力于提供高效解决方案。"|replace:"安企CMS,"}}After processing, it will display as:欢迎使用,致力于提供高效解决方案。
Here we replace the 'AnQi CMS' with an empty string to achieve the purpose of deletion. It should be noted that,replaceThe old and new content in the filter should be separated by a comma,It separates.
Remove leading and trailing characters:trimFilter family
Sometimes, we just want to remove extra spaces, newlines, or specific characters from the beginning or end of a string.trimAnd variant filters can come in handy.
trimRemove all spaces or specified characters from both ends of the string.trimLeftRemove all spaces or specified characters from the beginning of the string.trimRightRemove all spaces or specified characters from the end of the string.
Example of Use Case:Clean leading and trailing whitespace that may be imported from external content.
{{" 这是一段带有空格的文本 "|trim}}After processing, it will display as:这是一段带有空格的文本
{{"### 这是一个标题 ###"|trim:"# "}}After processing, it will display as:这是一个标题
Remove HTML tags from the content.
When the content contains HTML tags, and you want to keep only plain text or remove specific HTML tags, Anqi CMS also provides the corresponding filters.
Completely strip all HTML tags:striptagsFilter
striptagsThe filter can completely strip all HTML tags from a piece of HTML content, leaving only plain text.This is very useful when it is necessary to convert rich text content to plain text summaries or display in environments that do not support HTML.
Example of Use Case:Generate an article summary, or convert user-submitted rich text content to plain text for storage or display.
{{"<strong><i>Hello!</i></strong><p>这是一个段落。</p>"|striptags|safe}}After processing, it will display as:Hello!这是一个段落。
Please note that we usually cooperate here.|safeto use the filters together.|safeThe filter is used to inform Anqi CMS that the content has been processedstriptagsThe processed content is safe HTML and does not need to be automatically escaped.
Selective removal of specific HTML tags:removetagsFilter
If you do not want to delete all HTML tags but only remove part of the tags in the HTML content, for example, only remove<i>tags and keep<strong>tag, removetagsThe filter is your **choice.
Example of Use Case:Keep the bold style of the content, but remove the italic style, or clean up specific disallowed HTML tags.
{{"<strong><i>Hello!</i></strong>"|removetags:"i"|safe}}After processing, it will display as:<strong>Hello!</strong>
If you need to remove multiple specific HTML tags, you can separate the tag names with a comma,and pass them as parameters toremovetagsfilter.
{{"<strong><i>Hello!</i><span>AnQiCMS</span></strong>"|removetags:"i,span"|safe}}After processing, it will display as:<strong>Hello!</strong>
Practical application and precautions
In practice, these filters can be flexibly combined according to your specific needs. For example, you can use them first byremovetagsremoving specific tags, and then bycutcleaning up some special characters.
It should be emphasized that when your filter produces HTML content (for example, usingremovetagsAfter), and you want the browser to parse it as HTML instead of displaying it as a raw string, be sure to add it at the end.|safeFilter. This is a security mechanism designed by Anqi CMS to prevent XSS attacks, it will default to escaping the output HTML.|safeIt is to inform the system that the content is safe and can be rendered directly.
By flexibly using these built-in string and HTML processing filters, you will be able to better manage and display website content, ensuring that users have an excellent reading and browsing experience.
Frequently Asked Questions (FAQ)
How can you remove multiple non-continuous specific characters at the same time, such as all commas, exclamation marks, and question marks in a text?
You can apply it multiple timesreplaceA filter that chains each character that needs to be removed to be replaced with an empty string. For example:{{"这是一个,带有!多种?符号的句子。"|replace:",,"|replace:"!,"|replace:"?,,"}}After processing, commas, exclamation marks, and question marks will be removed in order.
2. After removing HTML tags, will the newline characters in the original content be preserved?
striptagsThe filter focuses on removing HTML tags, usually retaining the newline characters in the original text. However, the final display effect also depends on the frontend CSS style and how the browser renders these newline characters (for example, in HTML,<br/>It is the real newline tag, simple\nIt may only display as a space). If you need to ensure the line break effect, you may need to use\nConvert to<br/>tag, at this time you can uselinebreaksbrfilter.
**3. These filters can handle all