When managing content in AnQi CMS, we often take advantage of the Markdown editor to conveniently write articles.The power of Markdown lies in its ability to convert simple plain text format into rich HTML structure, which brings great convenience to the styling and expressiveness of content.But sometimes, we do not need or want these HTML tags to be completely displayed on the final page.For example, we may only want to extract the plain text summary of the article, for use in the home page list display, SEO description, or in other scenarios where a specific format is required, we hope to remove certain specific tags.

Fortunately, the template system of Anqi CMS provides a very flexible and powerful Filters feature, 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

After entering content through the Markdown editor and saving it, Anqi CMS will convert it to HTML format in the background for storage or perform real-time conversion during template rendering. This means that even if you enter Markdown syntax, what is usually displayed on the page is usually includedp/h1/strong/em/a/imgHTML content of tags.

Our goal is to 'clean' the HTML content when outputting it in the template, selectively removing tags according to different requirements.

Remove all HTML tags: usestriptagsFilter

Suppose we want to extract pure text from the full content of an article, without any bold, italic, links, images, and other HTML elements. At this point,striptagsThe filter is our good helper.

When we get the article content field in the template (for examplearchive.Content,it is usually the HTML content rendered by Markdown)when it is simply appended with|striptagsThe filter will automatically strip all HTML tags from the content, leaving only plain text.

The Anqi CMS template system escapes HTML tags by default when outputting content for safety (such as converting <becomes&lt;),to prevent potential XSS attacks. When we need to manipulate actual HTML structures, such as removing tags, we usually append to the content variable.|safeThe filter tells the system that this content is safe HTML, it does not need to be escaped, and can be operated on directly. Therefore, the common usage would be:

{{ archive.Content|safe|striptags }}

Thus,striptagsIt can handle real HTML tags and return plain text.

Remove specific HTML tags: usingremovetagsFilter

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>, link tags<a>And bold tags<strong>.

removetagsThe filter can accurately delete the specified HTML tags. Just specify the name of the tag to be removed (without brackets)