When managing and displaying content in AnQi CMS, we often encounter a common requirement: how to elegantly handle HTML tags contained in the content.In order to display a concise summary on the article list page, to provide clean meta descriptions for search engine optimization (SEO), or simply to avoid unnecessary style conflicts, removing or filtering HTML tags is a basic and important skill.Safe CMS provides two powerful filters for this: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 two filters and make a**choice based on actual needs, which can make your website content display more accurate and efficient.
striptags: Strip thoroughly, pursuing purity.
When you need an absolutely pure text and do not want any form of HTML or XML tags to be included in the content,striptagsThe filter is your best choice. Its function is direct and thorough, as its name suggests - 'label stripping'. Once the content passes throughstriptagsProcessing, including all HTML tags,<p>/<strong>/<a>/<img>Even HTML comments, which 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 homepage or category list page, you only want to display a short, unformatted text summary, such as the 'Description' field of the article.At this point, if the original HTML is output directly, it may disrupt the page layout or display unfriendly code in search results. Usestriptagscan easily solve this problem.
Usage example
If you have a description field for an article:archive.DescriptionMay contain HTML, you can get plain text in the following way:
{{ archive.Description|striptags }}
After such processing, regardlessarchive.DescriptionHow many complex HTML structures there are in ancient China, the output will be a segment of plain text without any tags.striptagsThe advantage lies in its simple and thorough operation, which ensures the absolute purity of the output content, but it also means that you will lose all formatting and structural information.
removetags:Precise control, retain essence
withstriptagsDifferent from the 'one-size-fits-all' strategy,removetagsThe filter provides more refined control capabilities. It allows you to explicitly specify the list of HTML tags to be removed from the content.This means that you can neatly retain those formats that you consider valuable or necessary while cleaning up unnecessary tags.
Applicable scenarios
Assuming you want the summary of the article to retain bold or italic emphasis, but at the same time, you need to remove all images that may disrupt the layout (<img>) and videos (<video>)Label. Or, do you want to display the article content in certain specific areas, but must disable all external links(<a>),to prevent users from jumping to other pages. In this case,removetagsThe flexibility is fully demonstrated. You can accurately select the type of tag to be removed according to your needs.
Usage example:
If you want to remove all the content<i>and<span>tags, you can do it like this:
{{ "<strong><i>Hello!</i><span>AnQiCMS</span></strong>"|removetags:"i,span"|safe }}
The output will be<strong>Hello!AnQiCMS</strong>The bold tags are retained, while the italic and span tags are removed. You can specify multiple tags to remove at one time by separating them with commas.removetagsThe advantage lies in the high degree of customization and precise control it provides, allowing you to clean up HTML while retaining the semantics and visual presentation of the content to the greatest extent possible.Of course, the drawback is that you need to clearly know which tags you want to remove.
**Choose: Weigh between purity and flexibility
InremovetagsandstriptagsChoose between them, mainly depending on your formatting and cleaning needs:
- If you need completely plain text contentFor example, used at the top of HTML pages
meta descriptionTags, system notification information, or any occasion where HTML format is not allowed, then choose without hesitationstriptagsIt operates simply, with thorough effects, ensuring the purity of the content. - If you wish to retain some specific format or structure while cleaning HTML.Remove those labels that you think are redundant, distracting, or potentially risky, then
removetagsIt will be a more suitable tool. It gives you finer-grained control, allowing you to have more possibilities in content presentation.
It is recommended to use in conjunction when processing HTML content and outputting it to the page regardless of which filter you choose|safeThe filter. This is because AnQi CMS, for security reasons, defaults to escaping all output content with HTML entities. If you do not add|safeEven if the tags are cleaned, the remaining HTML content may be displayed as plain text instead of being correctly parsed by the browser. For example:{{ archive.Content|removetags:"a"|safe }}.
Proficient in usingremovetagsandstriptagsIt will make you more skillful in the content operation of Anqi CMS, ensuring that your website content can be presented in the most expected state to users and search engines in any scenario.
Frequently Asked Questions (FAQ)
1.removetagsCan the filter remove multiple different types of HTML tags at once?Yes,removetagsThe filter supports you by comma,Separate multiple tag names to remove them all at once. For example,{{ content|removetags:"a,img,script"|safe }}Will simultaneously remove links, images, and script tags.
2. After usingremovetagsorstriptagsWhy does cleaned HTML content sometimes still display as raw code instead of being rendered by the browser after filtering?This is because the AnQi CMS template engine defaults to escaping all output content to prevent cross-site scripting attacks (XSS). When you useremovetagsorstriptagsAfter processing HTML content, 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|safea filter. For example:{{ archive.Description|striptags|safe }}.|safeTell the template engine that this content is safe and can be directly output as HTML.
3. Will these filters affect the plain text content outside the HTML tags?No.removetagsandstriptagsFilters are only designed to operate on the HTML tag structure, removing or retaining the tag itself, but not modifying the plain text content inside or between tags. For example,<span>Hello World</span>afterstriptagsWill be changed after processingHello WorldAmong which the text 'Hello World' itself will not be changed.