When managing website content in AnQiCMS, we often need to import data from external sources, such as batch importing articles through the content collection feature, or synchronizing product information through API interfaces.These external data may not be as satisfactory in terms of text format after import, especially for long paragraph texts that lack proper line breaks, which might affect the overall layout of the page and the reading experience of the users.wordwrapThe filter is an effective tool for optimizing the display effect of imported text.

AnQiCMS and External Data Import: Background

AnQiCMS is dedicated to providing efficient and customizable content management solutions for small and medium-sized enterprises and content operation teams.One of the core features highlights is the support for "content collection and batch import", which greatly simplifies the process of content construction, allowing the website to quickly fill in 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 save storage space, for example, a long text without 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 specialized utilities for text processing, its main function is to automatically insert line breaks in long texts according to the character length you specify, thereby splitting long text lines into multiple lines to improve the readability of the text and the neatness of the page layout.

wordwrapThe basic usage of the filter is very intuitive:{{ 变量名|wordwrap:数字 }}Here,变量名represents the text content you want to process,数字the parameter defines the maximum number of characters per line. For example, if you have a variablearchive.ContentThe article content is stored here, and it is recommended to display no more than 80 characters per line. You can use it like this:{{ archive.Content|wordwrap:80 }}.

However, when usingwordwrapWhen using the filter, there is a key feature that needs special attention: it is mainlyIdentify words by spaces and format for line breaksThis means that if your text is composed of continuous characters without spaces (such as Chinese, Japanese, Korean, etc.), then even if you specify the line length, wordwrapIt may not wrap at the expected position, but instead will treat the entire continuous string as an indivisible 'word' and maintain its continuity. The AnQiCMS documentation clearly states:wordwrapWords will be separated by spaces.If it does not contain spaces, it is considered a word.Therefore, if it is a continuous string of Chinese characters, it will not be wrapped.This feature needs to be considered especially when using Chinese content.

wordwrapThe actual application scenarios of the filter

  • Optimization of the format for batch importing articles:Imagine you have imported a large amount of technical articles or news information in batches from industry portals using AnQiCMS's content collection feature.These articles' text may all be a continuous paragraph without proper paragraph breaks or natural line breaks.It may cause page layout to become chaotic when displayed directly on the website.wordwrapThe filter can automatically break lines at reasonable lengths, significantly improving the user's reading experience.
  • User-generated content formatting:For user comments, reviews, product evaluations, or forum posts on the website, users may paste in overly long text without line breaks