In managing and presenting content in AnQi CMS, we often encounter a common requirement: how to elegantly handle HTML tags contained in the content.Whether it is to display a concise abstract on the article list page, provide clean meta descriptions for search engine optimization (SEO), or simply to avoid unnecessary style conflicts, removing or filtering HTML tags is a fundamental and important skill.removetagsandstriptagsThey can all help us complete the label cleaning work, but their working methods and applicable scenarios are quite different.
Understand the difference between these filters and make a **choice based on actual needs, which can make your website content display more accurate and efficient.
striptags: Strip off completely, pursuing purity.
When you need a completely pure text, without any form of HTML or XML tags included.striptagsFilter is your best choice. Its function is direct and thorough, as indicated by its name—"剥去标签". Once the content passes throughstriptagsProcessing, all HTML tags, including<p>/<strong>/<a>/<img>This, even HTML comments, will be mercilessly removed, leaving only the plain text content inside the tags.
Applicable scenariosImagine that your article content is very rich, including images, links, and various formats.But on the home page or category list page, you only want to display a brief, unformatted text summary, such as the "Description" field of the article.If the original HTML is output directly, it may destroy the page layout or display unfriendly code in search results.striptagsThen you can easily solve this problem.
Usage Example:
If you have a description field for an articlearchive.DescriptionMay contain HTML, you can obtain plain text in the following way:
{{ archive.Description|striptags }}
No matter after such processing,archive.DescriptionHow many complex HTML structures there are in Zhongyuan, the output will be a piece of plain text without any tags.striptagsthe advantage lies in its simple and thorough operation, ensuring the absolute 'purity' of the output content, but it also means you will lose all formatting and structural information.
removetags:English control, retain essence
WithstriptagsDifferent from the 'one-size-fits-all' strategy,removetagsThe filter provides more fine-grained control capabilities.It allows you to explicitly specify the list of HTML tags to be removed from the content.This means, you can cleverly retain the formats that you consider valuable or necessary while cleaning up the unnecessary tags.
Applicable scenarios:
Assume you want the summary of the article to retain bold or italic emphasis effects, but at the same time need to remove all possible images that may interfere with the layout (<img>) and videos (<video>Tags. Or, do you want to display the article content in some specific areas, but you must disable all external links (<a>) to prevent users from jumping to another page. In this case,removetagsThe flexibility is fully demonstrated. You can accurately select the label type to be removed according to your needs.
Usage Example:
If you want to remove all the content<i>and<span>labels, you can operate like this:
{{ "<strong><i>Hello!</i><span>AnQiCMS</span></strong>"|removetags:"i,span"|safe }}
The output will be<strong>Hello!AnQiCMS</strong>The bold labels are retained, while the italic and span tags are removed. You can specify multiple tags to be removed at once by separating them with commas.removetagsThe advantages lie in the high degree of customization and precise control it offers, allowing you to clean HTML while retaining the semantic and visual presentation of the content to the maximum extent.Of course, the drawback is that you need to clearly know which tags you want to remove.
**Choose: Balance Purity and Flexibility
InremovetagsandstriptagsChoose between them, mainly depending on your formatting and cleaning needs:
- If you need complete plain text contentFor example, used in the HTML page header,
meta descriptiontag, system notification information, or any situation where HTML format is not allowed, then choose without hesitation.striptagsIt operates simply, with thorough effects, ensuring the purity of the content. - If you wish to keep some specific formats or structures while cleaning HTMLRemove any tags that you think are redundant, disruptive, or potentially risky,
removetagsIt will be a more suitable tool. It gives you finer control and more possibilities in content presentation.
No matter which filter you choose, it is recommended that you use it in conjunction with the processing of HTML content and outputting it to the page|safeFilter. This is because the Anqi CMS defaults to escaping all output content for security reasons. If you do not add|safeEven if the tags are cleaned, the remaining HTML content may still be displayed as plain text instead of being correctly parsed by the browser. For example:{{ archive.Content|removetags:"a"|safe }}.
Proficient in usingremovetagsandstriptags,will make you more proficient in the content operation of Anqi CMS, ensuring that your website content is presented to users and search engines in the most expected state in any scenario.
Common Questions (FAQ)
1.removetagsCan the filter remove multiple different types of HTML tags at once?Yes,removetagsThe filter supports you by comma,separating multiple tag names, removing them at once. For example,{{ content|removetags:"a,img,script"|safe }}It will also remove the links, images, and script tags from the content.
2. In the case of usingremovetagsorstriptagsAfter filtering, why does the cleaned HTML content sometimes still display as raw code instead of being rendered by the browser?This is because the security CMS template engine defaults to escaping all output content as HTML entities to prevent cross-site scripting attacks (XSS). When you useremovetagsorstriptagsProcess HTML content and if you still want the browser to parse and render it as HTML, you need to append it to the end of the filter chain|safefilter. For example:{{ archive.Description|striptags|safe }}.|safeExplicitly tell the template engine that this content is safe and can be directly output as HTML.
3. Do these filters affect the plain text content outside of HTML tags?No.removetagsandstriptagsFilters operate only on the HTML tag structure, they remove or retain the tag itself, but do not modify the plain text content inside or between the tags. For example,<span>Hello World</span>Afterstriptagsafter processing will becomeHello World,其中“Hello World”这段文本本身不会被改变。