wordwrapandsafeAnd how do they work together?

UnderstandingwordwrapFilter: Intelligent line break for text

wordwrapFilter, as the name implies, its main function is to help us automatically wrap long text.Imagine if there is a long piece of content on your page, such as a long URL without spaces or a long foreign word, it may exceed the boundary of the container and affect the overall layout of the page.wordwrapThe character length can be set, when the text reaches this length, it will wrap at the nearest space.If it encounters continuous Chinese or other characters without spaces, it will treat this long string as a 'word' and will not easily break it up.This is very helpful for improving the readability of long text and avoiding layout errors.{{ content|wordwrap:20 }}It can display up to 20 characters per line.

UnderstandingsafeFilter: Ensure that HTML content is parsed correctly

whilesafeFilter, plays different roles, but still crucial. In the AnQiCMS template, for security reasons, the system defaults to escaping all HTML tags and JavaScript code in the output content, such as<script>Would be displayed as&lt;script&gt;safeThe filter comes into play. By adding it after the variable|safeWe can tell the template engine that this content is 'safe' and can be output directly as HTML. For example{{ article.Content|safe }}Of course, it is especially worth mentioning that once you have usedsafeThis means that you are responsible for the source and security of the content, ensuring that it does not contain malicious code.

wordwrapWithsafeCollaborative work: balancing layout and parsing

So,wordwrapFilters can be used withsafeDo you use filters together?The answer is affirmative, and in many practical scenarios, they can work better together.Imagine you have an article content containing HTML tags, where some paragraphs or list items may be poorly formatted on the page due to the inclusion of especially long words or URLs.You want this content to be correctly parsed as HTML and to automatically wrap at the appropriate places.

In this case, you should use firstwordwrapa filter to handle text line breaks, then usesafeFilter to ensure that HTML tags are rendered correctly. For example, the document of AnQiCMS provides a very clear example:

{{ "Lorem ipsum dolor sit amet, consectetur adipisici elit."|wordwrap:2|linebreaksbr|safe }}

This example cleverly demonstrates the chained use of filters: first,wordwrap:2Attempt to break long text into lines every 2 characters (although the effect is not obvious here because English words do not have spaces, the principle is the same); then,linebreaksbrConvert the newline characters after processing to<br/>tags; finally,safeEnsure the entire output HTML structure is correctly parsed and displayed by the browser, rather than being escaped.

The logic of doing this is:wordwrapis a text processing filter that changes the structure of the string itself, for example, inserting line breaks.safeIt is an HTML parsing control filter that determines how the template engine treats the HTML content in this string.Only by first completing the text structure processing and then informing the template engine how to parse can we achieve the desired effect.wordwrapor `line