In AnQi CMS template development, filters are very practical tools for data processing, which can help us format, truncate, or convert content in various ways. Among them,wordwrapFilters are commonly used for automatic line breaks in text. So, does this filter remove HTML tags from the content when processing text? Let's discuss it in detail.

wordwrapThe core function of the filter

Firstly, we need to understandwordwrapWhat is the purpose of the filter design. According to the Anqi CMS documentation,wordwrapThe primary function of the filter is to automatically wrap the text content that is longer than specified.It will distinguish 'words' based on spaces in the text and then break lines on this basis.For example, if you have a long English text and setwordwrap:20it will try to insert line breaks at positions around every 20 characters (and as much as possible between words).

It is noteworthy that the document also clearly states,wordwrapThe filter treats continuous characters without spaces (such as Chinese) as a single 'word' when processing, and therefore does not break lines between these continuous characters.

wordwrapWill the HTML tags be removed?

In short,wordwrapThe filter will not automatically remove or parse HTML tags from the text.

Its original design was to optimize the display layout of text, rather than to perform content security filtering or structural transformation. When a text containing HTML tags is passed throughwordwrapWhen the filter is processed, these HTML tags themselves are considered as part of the normal string and are involved in the calculation of character length.The filter will look for suitable line breaks in the visible text content outside of HTML tags.

This means, if you pass a section that contains<p><strong>这是一段加粗的文字。</strong></p>such HTML code towordwrapa filter, it will not remove<p>or<strong>Label. It will only try to wrap the visible text in “This is a bold text.” at this point, while keeping the HTML tags intact.

WhywordwrapDo not remove HTML tags?

In the Anqi CMS template system, there are special filters for handling HTML tags, such as:

  • striptagsFilter: to remove all HTML and XML tags from the text.
  • removetagsFilter: Used to remove the specified HTML tags from the text.
  • truncatechars_htmlandtruncatewords_htmlFilter: These filters intelligently handle HTML structures during text extraction to avoid breaking the integrity of the tags.

These dedicated filters exist to separate the different needs of 'text layout processing' and 'HTML content processing'.wordwrapFocus on line breaks in plain text, while other filters are responsible for parsing and manipulating HTML tags.

Suggestions for application in practice.

  1. If you want to remove HTML tags before text wrapping: You should usestriptagsorremovetagsThe filter to remove HTML tags first and then applywordwrapfilter. For example:

    {{ your_html_content | striptags | wordwrap:50 | safe }}
    

    This ensures that the content has been purified into plain text before the line break.

  2. If you want to keep the HTML tags when line breaks occur: Can be used directlywordwrapFilter. But please note,wordwrapIt calculates and determines the line break point based on the length of visible text (excluding the HTML tags themselves).HTML tags themselves are not split by the filter, but the text around them is wrapped according to the rules.At the same time, in order to ensure that the front-end page can correctly parse and render these HTML tags instead of displaying them as plain text, you usually need to use at output time|safefilter. For example:

    {{ your_content_with_html | wordwrap:50 | safe }}
    

UnderstandingwordwrapThis feature of the filter can help you control the display effect of the Anqi CMS template more accurately and avoid unnecessary confusion.When processing text containing HTML content, combining different filters can achieve the output effect you expect.


Frequently Asked Questions (FAQ)

How do I keep HTML tags when I want to break lines?

wordwrapThe filter itself does not remove HTML tags, so you can use it directly on text containing HTML. However, to ensure that the browser parses and displays HTML tags correctly rather than treating them as plain text, you also need to add them in the final output.|safefilter.wordwrapAffects visible text content between HTML tags.

2.wordwrapFilters andtruncatecharsWhat are the differences between filters?

They serve different purposes.wordwrapThe function of the filter isAuto-wrap.It inserts line breaks in long text to improve reading layout, and the text content is complete.truncatecharsThe function of the filter isString slicing.It will truncate text according to the specified character length and add an ellipsis (...) at the end to limit the display length.

3.wordwrapDoes the filter work for line breaks in Chinese string?

wordwrapThe filter is by default set to identify "words" based on spaces and to wrap lines. For continuous Chinese text, since there are no spaces between Chinese characters,wordwrapIt treats a continuous Chinese text as a 'word', so it does not break the line in the middle.If your Chinese content needs to be wrapped, you may need to consider inserting line breaks manually at the source or through other custom logic.