How to automatically wrap long text in AnQiCMS templates for optimized reading experience?

Calendar 👁️ 87

In website content operation, optimizing the reading experience is a key factor in attracting and retaining users.Especially for long text content, if there is a lack of proper formatting and automatic line breaking, users may face issues such as horizontal scrolling and text stacking, which seriously affect reading comfort and even lead to user loss.As AnQiCMS users, we can take advantage of its powerful template engine and built-in features to easily implement automatic line breaks for long text, thereby greatly enhancing the reading experience of website content.

Understanding the importance of long text wrapping

Imagine browsing an article on your phone and finding that a paragraph of text is so long it extends beyond the screen width, requiring you to swipe side to side just to read one sentence. This experience is undoubtedly frustrating.Whether on the desktop or mobile end, unreasonable text layout will increase the reading burden of users.Text wrapping ensures that content always fits the user's device screen, avoids horizontal scrolling, effectively controls the length of each line of text, making the layout more uniform, reduces visual fatigue, and allows readers to immerse themselves more smoothly in the content.

AnQiCMS template's core line break tool:wordwrapFilter

AnQiCMS template engine provides a variety of filters (Filters), including:wordwrapThe filter is specifically designed to handle automatic line breaks in long text. This filter can intelligently wrap long text according to the specified character length.

wordwrapThe filter usage is very intuitive. You just need to add it after the text variable you want to process|wordwrap:数字and the number represents the maximum number of characters you want each line of text to have.

For example, if you have a long article content stored inarchiveContentIn a variable, and you want it to automatically wrap every 50 characters or so (by word), you can use it like this:

<div>
    {{ archiveContent|wordwrap:50|safe }}
</div>

Please note,wordwrapThe filter, when performing line breaks, uses space-separated words as the basis.This means it will try to avoid breaking in the middle of a word to maintain the integrity of the word, thereby improving readability.But, there is an important detail to understand: for continuous characters without spaces (such as Chinese text),wordwrapThe filter will not force a line break in the middle of these continuous characters.It only considers line breaks when encountering spaces. This means that if there are very long continuous sentences in your Chinese content without punctuation or spaces, it may be necessary to combine other methods for assistance processing.

CSS styles to assist in optimizing the reading experience

In addition to template filters, CSS is the main means to achieve elegant line breaks for long text in modern web design.You can globally control the line break behavior of article content through custom CSS styles without using filters for each text variable.

The most commonly used CSS property isword-wrapandoverflow-wrap. Its function is to allow word-breaking within the text when it overflows the text box to prevent overflow. Typically, we would set it tobreak-wordorbreak-allSelect based on requirement:

  • word-wrap: break-word;(or)overflow-wrap: break-word;Allow breaking within words to prevent content overflow. This is the most commonly used option, which tries to maintain word integrity and only breaks when necessary.
  • word-break: break-all;: Allow breaks between any characters, regardless of whether they are words or not.This option may be more effective in handling Chinese and other languages because it does not depend on word boundaries, but it may sacrifice a bit of the integrity of English words.

You can add these styles to the container of the article content (for example<article>or somedivelement) in the CSS stylesheet you include in your template file:

.article-content {
    word-wrap: break-word; /* 针对较旧的浏览器 */
    overflow-wrap: break-word; /* 现代浏览器推荐 */
    word-break: normal; /* 保持默认的单词断裂行为,结合上面两个属性 */
    line-height: 1.8; /* 调整行高,增加行间距,提升可读性 */
    max-width: 700px; /* 控制行宽,过长的行也影响阅读,这是推荐的实践 */
    margin: 0 auto; /* 居中内容区域 */
}

to{{ archiveContent|safe }}Such content wrapped in a container witharticle-contentclass namedivIt can make CSS styles take effect.

Handle manual line breaks in the editor:linebreaksFilter

Sometimes, when you are editing article content in the background, you may be accustomed to using the Enter key to create line breaks. However, browsers do not default to converting carriage returns (line breaks) in text.\nIt is parsed as a visible line break, but it is treated as a space. The result is that the originally segmented text is displayed as a long string on the web page.

To solve this problem, the AnQiCMS template engine provideslinebreaksandlinebreaksbrFilter:

  • linebreaksA filter will convert a single newline character in the text to.<br />tags, and two consecutive newline characters will be converted to<p>and</p>Paragraph wrapped in tags.
  • linebreaksbrThe filter is simpler, it will directly convert all newline characters to<br />.

This is particularly important for content entered from a rich text editor or Markdown editor, ensuring that the segmentation and line breaks in the content are rendered correctly on the front end. Usage is as follows:

<div class="article-content">
    {{ archiveContent|linebreaks|safe }}
</div>

Or, if you prefer a simple line break:

<div class="article-content">
    {{ archiveContent|linebreaksbr|safe }}
</div>

Remember to add these filters when using them:|safeFilters to ensure HTML tags (such as<br />or<p>It can be correctly parsed by the browser and not displayed as plain text.

Content truncation as a supplement strategy for long text.

Although automatic line wrapping solves the problem of text overflow, in some display scenarios, such as list pages, card previews, etc., you may want to truncate the long text directly, displaying only part of the content with an ellipsis. The AnQiCMS template engine also provides the corresponding filter:

  • truncatechars:数字: Truncate by character count and add at the end....
  • truncatewords:数字: Truncate by word count and add at the end....
  • truncatechars_html:数字andtruncatewords_html:数字These two versions are specifically designed to handle text containing HTML tags, and they try to maintain the integrity of the HTML structure during truncation to avoid breaking the page layout due to incomplete tags.

For example, display the article summary on the article list page:

<p>{{ item.Description|truncatechars:100|safe }}</p>

This will display the first 100 characters of the summary, followed by an ellipsis, making the list page look neater.

Summary

By flexibly using the AnQiCMS template engine providedwordwrap/linebreaksSeries filter, combined with the CSS style rules of the website itself, we can effectively solve the reading difficulties brought by long text.Whether it is to finely control line breaks at specified lengths in text, or to correctly render line breaks in the editor as HTML, or to elegantly truncate long content, AnQiCMS provides practical tools.Reasonable text processing not only makes your website content layout more professional, but also significantly improves the comfort of reading and the overall website experience.

Related articles

How to define an array and display its content in the template using the AnQiCMS filter?

## Advanced Template Development: Flexibly Define and Display Array Content in AnQiCMS During the template development process of AnQiCMS, we often encounter scenarios where we need to process list data, dynamic configuration options, or generate a series of contents based on specific logic.Although most of the data is obtained through tags from the backend, in some cases, defining and operating on simple data sets directly in the template, especially arrays, can greatly improve the flexibility and efficiency of front-end development and reduce dependence on backend data.

2025-11-07

How to extract a specified part of a string or array for display in the AnQiCMS template?

In AnQiCMS template development, we often encounter the need to handle string or array content, such as displaying article summaries, limiting the number of image lists, or extracting specific information from a long text.The AnQi CMS is developed based on the Go language, its template engine supports syntax similar to Django and Blade, and provides a rich set of filters (filters) to help us efficiently complete the content extraction and display requirements.Let's take a look at how to flexibly extract the specified part of a string or array for display in the AnQiCMS template.

2025-11-07

How to link array elements into a string for display in AnQiCMS template?

In AnQiCMS template development, we often encounter situations where we need to display a series of related data, such as multiple tags of an article, various characteristics of a product, or multiple options stored in a custom field.These data are often present in the template in the form of an array, and we hope to display them in a concise and beautiful string format, such as connecting them with commas, slashes, or other symbols.

2025-11-07

How to convert a numeric string in AnQiCMS template to a floating-point number or integer for display?

In AnQiCMS template development, data processing is one of the core links.The flexible content model allows us to customize various fields to store website information, including scenarios where numbers are needed, such as product prices, inventory quantities, ratings, and so on.Although these numbers may look normal in the background, they are likely to be presented as strings when called in the template.This brings up a common question: How can we convert these numeric strings to floating-point numbers or integers in a template for mathematical operations or specific formatting?

2025-11-07

How to format floating-point numbers and retain a certain number of decimal places in AnQiCMS templates?

In website content operation, we often need to display floating-point numbers such as prices, percentages, ratings, and so on.The precision and display method of these numbers directly affect the user experience and the professionalism of information.AnQi CMS knows this need and provides a powerful and flexible floating-point number formatting function in the template engine, allowing you to easily control the display accuracy of floating-point numbers.Next, we will explore two main methods of formatting floating-point numbers and retaining specified decimal places in AnQiCMS templates: the `floatformat` filter and the `stringformat` filter

2025-11-07

How to get the thumbnail of AnQiCMS image resources and display it?

AnQiCMS focuses on visual effects and page loading efficiency in content display, therefore, how to efficiently obtain and display thumbnail images of image resources is a common problem we encounter in daily operations.It is fortunate that AnQiCMS provides a very convenient and flexible way to handle image thumbnails, allowing us to easily meet various display needs. --- ### How to manage and generate image thumbnails in AnQiCMS?

2025-11-07

How to parse and output HTML code in AnQiCMS template instead of escaping it?

When using AnQi CMS for website content creation and template design, we often encounter a problem related to HTML code display: Why is the HTML code I enter in the background editor displayed as plain text on the front page instead of being parsed by the browser as actual HTML elements?This is actually the content management system that defaults to enabling HTML content escaping for website security.This article will delve into how to effectively control the output of HTML code in the AnQiCMS template, ensuring that it is parsed correctly and not displayed as escaped.

2025-11-07

How to get and display detailed information of user groups in AnQiCMS template?

In AnQiCMS, user group management is one of the core functions for building differentiated services and content monetization systems.It is crucial to display detailed information about user groups accurately in the front-end template, whether it is to provide exclusive content for VIP users or to set access restrictions for users of different permission levels.This not only improves the user experience, but also effectively guides users to understand and upgrade their own permissions.How can we flexibly obtain and display the detailed information of these user groups in the AnQiCMS template?

2025-11-07