When managing content in AnQi CMS, we often use the Markdown editor conveniently to write articles.The power of Markdown lies in its ability to convert simple plain text formats into rich HTML structures, which brings great convenience to the styling and expressiveness of content.But sometimes, we may not need or want these HTML tags to be fully displayed on the final page.For example, we may want to extract the plain text summary of an article for the homepage list display, SEO description, or in other scenarios that require specific formats, where we hope to remove certain specific tags.
It is fortunate that the template system of Anqi CMS provides a very flexible and powerful filter (Filters) function, which can help us easily remove all or specified HTML tags from HTML content rendered from Markdown.
Understanding the background of content rendering and tag removal
When we input content using a Markdown editor and save it, the Anqi CMS will convert it to HTML format for storage in the background or perform real-time conversion during template rendering. This means that even if you input Markdown syntax, what is usually displayed on the page is often also includedp/h1/strong/em/a/imgHTML content for tags such as 【en】.
Our goal is to 'clean' these HTML contents when outputting them in the template, selectively removing tags based on different requirements.
Remove all HTML tags: usestriptagsFilter
Assuming we want to extract pure text from the full content of an article, without any bold, italic, links, images, or other HTML elements.striptagsThe filter is our good helper.
When we get the article content field in the template (for example,archive.Contentit is usually the HTML content rendered by Markdown, when it is, simply add it after|striptagsFilter, the system will automatically strip all HTML tags from the content, leaving only plain text.
The template system of AnQi CMS escapes HTML tags by default when outputting content for security (such as replacing<becomes<To prevent potential XSS attacks. When we need to manipulate the actual HTML structure, such as removing tags, we usually add a closing brace to the content variable.|safeFilter, telling the system that this content is safe HTML that does not require escaping and can be operated on directly. Therefore, the usual writing would be:
{{ archive.Content|safe|striptags }}
This is,striptagsCan handle actual HTML tags and return plain text.
Remove specific HTML tags: useremovetagsFilter
Sometimes, we may not want to remove all tags, but selectively retain some tags, removing only specific tags. For example, we may want to retain paragraph tags<p>and newline tags<br>But remove all image tags<img>And link tags<a>And bold tags<strong>.
removetagsThe filter can accurately remove the specified HTML tags. Simply enter the name of the tag to be removed (without brackets)