In the daily content operation of AnQiCMS, we sometimes encounter situations where we need to carefully process the content, such as removing specific characters, extra spaces, or stripping unnecessary HTML tags from the content.These operations are crucial for ensuring the neat display of content, adapting to 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 (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|Connected to a variable, followed by the filter name and optional parameters, for example{{ obj|filter_name:param }}By mastering these filters, you can have more refined control over the website content.
Remove specific characters from a string
When we need to delete specific characters or substrings from a text, Anqi CMS provides several practical filters.
UsecutFilters perform precise deletion
cutThe filter can accurately remove specific characters or substrings at any position in a string. It removes all matching target content and returns the remaining string.
Example of usage scenarios:Assuming some unnecessary special characters have been mixed into your content, such as_or"you can usecuta filter to remove them.
{{"这是一个_带有_下划线的_句子"|cut:"_"}}After processing, it will be displayed as:这是一个带有下划线的句子
{{"这是"一个"带有"引号"的"句子"|cut:"\""}}After processing, it will be displayed as:这是一个带有引号的句子
UsereplaceThe filter performs a replacement deletion.
replaceThe filter can replace old content with new content in a string. If we set the new content to an empty string, it acts as a way to delete specific content.
Example of usage scenarios:If you want to delete a keyword or remove all specific characters, you can do so byreplacethe filter.
{{"欢迎使用安企CMS,安企CMS致力于提供高效解决方案。"|replace:"安企CMS,"}}After processing, it will be displayed as:欢迎使用,致力于提供高效解决方案。
Here we replace the "AnQi CMS" with an empty string to achieve the purpose of deletion. It is important to note,replaceCommas must be used between the old and new content in the filter.,Split the content.
Clean leading and trailing extra characters:trimFilter family
Sometimes, we just want to clean up extra spaces, new lines, or specific characters at the beginning or end of a string.trimVariants filter can be useful.
trim: Remove all whitespace or specified characters from both ends of the string.trimLeft: Remove all whitespace or specified characters from the beginning of the string.trimRightRemove all trailing spaces or specified characters.
Example of usage scenarios:Clean up leading and trailing whitespace characters that may be brought in when importing content from the outside.
{{" 这是一段带有空格的文本 "|trim}}After processing, it will be displayed as:这是一段带有空格的文本
{{"### 这是一个标题 ###"|trim:"# "}}After processing, it will be displayed 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 corresponding filters.
Thoroughly 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 to display in environments that do not support HTML.
Example of usage scenarios:Generate article abstract, 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 be displayed as:Hello!这是一个段落。
Please note that here we usually cooperate with|safeFilter used together.|safeThe filter is used to inform the CMS that the content has passedstriptagsThe processed content is safe HTML and does not need to be automatically escaped.
Selectively remove specific HTML tags:removetagsFilter
If you don't 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>tags,removetagsThe filter is your **choice**.
Example of usage scenarios: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 be displayed 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 arguments toremovetagsFilter.
{{"<strong><i>Hello!</i><span>AnQiCMS</span></strong>"|removetags:"i,span"|safe}}After processing, it will be displayed as:<strong>Hello!</strong>
Application and Precautions in Practice
In actual operation, these filters can be flexibly combined according to your specific needs. For example, you can first useremovetagsto remove specific tags, and then usecutto clean up some special characters.
It should be emphasized that when your filter still processes the result as HTML content (for example, using...)}]removetags(and you want the browser to parse it as HTML instead of displaying it as a raw string, please be sure to add it at the end.}|safeFilter. This is a security mechanism designed by Anqi CMS to prevent XSS attacks, which will default to escaping the output HTML.|safeIt indicates to 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 receive an excellent reading and browsing experience.
Common Questions and Answers (FAQ)
How to remove multiple discontinuous specific characters simultaneously, such as all commas, exclamation marks, and question marks in text?
You can apply it multiple timesreplaceFilter, chain-ly replacing each character that needs to be removed with an empty string. For example:{{"这是一个,带有!多种?符号的句子。"|replace:",,"|replace:"!,"|replace:"?,,"}}After processing, commas, exclamation marks, and question marks will be removed in sequence.
2. Will the newline characters in the original content be preserved after removing the HTML tags?
striptagsThe filter primarily focuses on removing HTML tags, and it usually retains newline characters in the original text. However, the final display effect also depends on the front-end CSS styles and how the browser renders these newline characters (for example, in HTML, ...<br/>is the actual newline tag, simply\nmay only be displayed as a space). If you need to ensure the newline effect, you may need to convert\nto<br/>tag, at which point you can uselinebreaksbrFilter.
These filters can handle all