In the template development of AnQi CMS, filters are a very practical tool for processing data. They can help us format, truncate, or convert content in various ways.wordwrapFilters are commonly used for automatic line wrapping of text. So, will this filter remove HTML tags from the text it processes? Let's delve into it in detail.
wordwrapThe core function of the filter
Firstly, we need to understandwordwrapThe design purpose of the filter is what. According to the document description of Anqi CMS,wordwrapThe main function of the filter is to automatically wrap the text content to the specified length.It will distinguish 'words' based on spaces in the text and then break lines on this basis.wordwrap:20it will try to insert a line break at about every 20 characters (and as much as possible between words).
It is worth noting that the document also explicitly states,wordwrapThe filter will treat a continuous segment of characters without spaces (such as Chinese) as a single 'word', and will not break the line in the middle of 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 design intention is to optimize the display layout of text, rather than to perform content security filtering or structural transformation. When a piece of text containing HTML tags passes throughwordwrapThe HTML tags themselves are treated as part of the plain string when processed by the filter, and are included in the character length calculation.The filter will search for suitable line breaks in visible text content outside of HTML tags.
This means that if you pass a segment containing<p><strong>这是一段加粗的文字。</strong></p>such HTML code towordwrapthe filter, it will not remove<p>or<strong>Label. It will only try to break lines within the visible text “这是一段加粗的文字。” and keep the HTML tags unchanged.
WhywordwrapDo not remove HTML tags?
In the template system of AnQi CMS, there are special filters for processing HTML tags, such as:
striptagsFilter: used to remove all HTML and XML tags from the text.removetagsFilter:Used to remove specified HTML tags from text.truncatechars_htmlandtruncatewords_htmlFilter:These filters intelligently handle HTML structure during text truncation to avoid destroying the integrity of tags.
These specialized filters exist to separate the different requirements of 'text layout processing' and 'HTML content processing'.wordwrapFocused on line breaks in plain text, while other filters are responsible for parsing and manipulating HTML tags.
Application suggestions in practice
If you want to remove HTML tags before performing text wrapping You should use
striptagsorremovetagsa filter to remove HTML tags first, then applywordwrapFilter. For example:{{ your_html_content | striptags | wordwrap:50 | safe }}This ensures that the content is purified to plain text before a newline.
If you want to keep HTML tags when wrapping lines: You can directly use
wordwrapthe filter. But please note,wordwrapThe line break point is determined 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.|safeFilter. For example:{{ your_content_with_html | wordwrap:50 | safe }}
UnderstandingwordwrapThis feature of the filter can help you control the display effect of content in the CMS template more accurately, and avoid unnecessary confusion.When processing text containing HTML content, using different filters together can achieve the output effect you expect.
Common Questions (FAQ)
1. How can I retain HTML tags when I want to break lines?
wordwrapThe filter itself does not remove HTML tags, so you can use it directly on text that contains HTML. However, to ensure that the browser parses and displays the HTML tags correctly rather than outputting them as plain text, you also need to add it in the final output.|safeFilter.wordwrapIt will act on the visible text content between HTML tags.
2.wordwrapfilters andtruncatecharsWhat are the differences between filters?
They have different purposes.wordwrapThe function of filters isAuto-wrapIt will insert line breaks in long text to improve the layout of the reading, and the text content is complete.truncatecharsThe function of filters isTruncate the stringIt will truncate the text according to the specified character length and add an ellipsis (…) at the end to limit the display length of the text.
3.wordwrapIs the filter effective for the line break of Chinese string?
wordwrapThe filter defaults to identifying "words" and performing line breaks based on spaces. For continuous Chinese text, since there are no spaces between Chinese characters,wordwrapThe text will be treated as a single 'word' for continuous Chinese characters, so there will be no line breaks in the middle.If your Chinese content needs to be wrapped, you may need to consider inserting line breaks at the source or through other custom logic.