When managing website content in AnQiCMS, we often need to import data from external sources, such as importing articles in batches through the content collection feature, or synchronizing product information through API interfaces.These external data may not be satisfactory after import, especially for some long paragraph texts that lack proper line breaks, which may affect the overall layout and reading experience of the page directly.In order to solve such problems, AnQiCMS's powerful template engine provides a variety of practical filters to process text content, among whichwordwrapThe filter is an effective tool for optimizing the display effect of imported text.

AnQiCMS and external data import: background

AnQiCMS is committed to providing efficient and customizable content management solutions for small and medium-sized enterprises and content operation teams.One of the core features is that it supports "content collection and batch import", which greatly simplifies the process of content construction, allowing the website to quickly fill a large amount of information.However, the text content obtained from third-party platforms is often not finely formatted to adapt to its original environment or to save storage space, for example, a long text may not have any line breaks.When this content is imported into AnQiCMS and displayed on the front-end page, it may cause text overflow in the container, horizontal scroll bars to appear, or poor display in different screen sizes, especially affecting the user experience on mobile devices.

UnderstandingwordwrapFilter

AnQiCMS's template engine is based on the powerful capabilities of the Go language, and it adopts a syntax style similar to Django, with built-in rich filters (filters) for various processing before data is rendered to the page.wordwrapThe filter is one of the practical tools specifically designed for text processing, its main function is to automatically insert line breaks in long texts according to the specified character length, thereby splitting long text lines into multiple lines to improve readability and the neatness of page layout.

wordwrapThe basic usage of the filter is very intuitive:{{ 变量名|wordwrap:数字 }}. Here,变量名Represents the text content you want to process, and数字The parameter defines the maximum number of characters per line. For example, if you have a variablearchive.ContentContaining the main text of the article, and it is desired that each line display a maximum of 80 characters, you can use it like this:{{ archive.Content|wordwrap:80 }}.

However, when usingwordwrapWhen filtering, there is a key feature that needs to be paid special attention to: it is mainlyIdentify words based on spaces and perform line breaksThis means that if your text is composed of consecutive characters without spaces (such as Chinese, Japanese, Korean, etc.), then even if you specify the line length, wordwrapIt may not be wrapped at the expected position, but the entire continuous string will be treated as an indivisible 'word' and kept continuous. The AnQiCMS documentation states explicitly:wordwrapWords are separated by spaces. If there are no spaces, then it is considered a single word.Therefore, if Chinese characters are continuous, they will not be wrapped. This feature needs to be considered especially when using Chinese content.

wordwrapThe actual application scenarios of the filter

  • Batch import article format optimization:Imagine you import a large number of technical articles or news information in batches through the content collection function of AnQiCMS from industry portals.These articles' main text may all be a continuous paragraph without proper paragraph breaks or natural line breaks.Displayed directly on the website may cause page layout to be confused.Applying to the variable of the article contentwordwrapFilter, which can automatically break lines at reasonable lengths, significantly improving the user's reading experience.
  • User-generated content layout:For the user messages, comments, product reviews, or forum posts on the website, users may paste in overly long text without line breaks