The `wordwrap` filter: how to elegantly handle the automatic line break display of long text without truncating words?

Calendar 👁️ 93

In website content display, we often encounter layout problems with long text content.When a piece of text is too long and exceeds the container width, if it is not properly handled, it may cause layout disorder and a decline in reading experience.AnQiCMS (AnQiCMS) provides a very practical template filter -wordwrapIt can help us elegantly solve the automatic line break display of long text, and the key is that it maintains the integrity of words, avoiding truncation in the middle of words, thereby greatly enhancing the professionalism and readability of the content.

wordwrapWhat is a filter?

wordwrapA filter is a tool used to handle text auto-wrap.Its core function is to break long text into lines according to the specified character length.Different from simply forcing a line break by character count,wordwrapThe intelligence lies in its ability to prioritize the recognition of 'word' boundaries in text (usually spaces), ensuring that a complete word is not harshly broken in the middle, thus making the text after line breaks still natural and smooth.

How to usewordwrapFilter?

UsewordwrapThe filter is very intuitive, it accepts a parameter to define the maximum character length of each line of text.

The basic syntax format is as follows:

{{ 你的文本内容 | wordwrap:每行最大字符数 }}

Here, 你的文本内容is the string variable you want to perform line break processing on, and每行最大字符数This is a number that represents the approximate number of characters you want to display per line.

For example, if you have a variable segmentarchive.DescriptionContaining the document summary, and it is desired that it display about 20 characters per line, you can use it like this:

<p>
  {{ archive.Description | wordwrap:20 }}
</p>

If your content is a long English sentence, for example: ”Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.“, when appliedwordwrap:10It will try to break at the word boundary around the 10th character, the effect may be as follows:

Lorem ipsum
dolor sit
amet,
consectetur
adipisici
elit, sed
eiusmod
tempor
incidunt ut
labore et
dolore magna
aliqua. Ut
enim ad minim
veniam, quis
nostrud
exercitation

You can see that each newline occurs at the end of a word, not in the middle of a word.

Actual application scenario: maintain text elegance and layout neatness.

In AnQi CMS,wordwrapFilters can be applied to various scenarios to optimize user experience and website layout:

  1. Document summary and abstract:On the article list page or search results page, the document's summary (archive.DescriptionIt often requires limiting length and beautiful line breaks. UsewordwrapIt can ensure that the introduction is displayed neatly within a limited width, while avoiding the reading障碍 brought by word truncation.
  2. Sidebar or widget text block:The sidebar of a website usually has limited space, if it contains some descriptive text or announcements, usingwordwrapThese texts can better adapt to narrow layouts and avoid overflow.
  3. Content generated dynamically:When the content of the website comes from user input or external data, its length is uncontrollable,wordwrapProvided an automated solution that ensures all text content is presented in a unified and standardized format.

If your content has gone throughwordwrapProcessed, the output needs to be in HTML format and retain line breaks, you can consider combininglinebreaksbra filter to convert line breaks to<br/>tags and usesafeThe filter ensures that HTML is parsed correctly:

<p>
  {{ archive.Description | wordwrap:30 | linebreaksbr | safe }}
</p>

Details to pay attention to.

  • Line break of Chinese text: wordwrapThe filter is based on spaces to identify 'words' and perform line breaks. This means that for languages like Chinese, which are written continuously without using spaces to separate words,wordwrapThe filter will treat a continuous Chinese text as a "long word", so it will not automatically wrap within it. In this case, if you need to force the Chinese text to wrap, you may need to combine CSS styles (such asword-break: break-all;) or use other character-based truncation logic.
  • Parameter flexibility: 每行最大字符数is a suggested value.wordwrapThe filter will try to break lines as close to this length as possible, but to maintain word integrity, the actual line length may be slightly shorter or longer than the set value.
  • Combined with HTML tags: wordwrapThe filter operates on plain text content. If your text contains HTML tags, these tags themselves are also considered part of the text and may be counted as characters when wrapping at line breaks.If you need to process content that includes HTML tags in a similar way (for example, truncating while maintaining the integrity of the HTML structure), you may need to consider using other specialized HTML processors (such astruncatechars_htmlThis does not match withwordwrapThe word wrap logic is different.

MasterwordwrapA filter that helps us better control the display effect of website content, allowing long text to remain clear and beautiful across different layouts, thereby enhancing the overall user experience.

Frequently Asked Questions (FAQ)

1.wordwrapFilters and CSS ofword-breakWhat are the differences between the properties?

wordwrapFilters are inserted at template rendering time by modifying the string content itself to insert line breaks (usually\nIt enables text wrapping. It focuses more on changing the text structure at a logical level. While CSS aims to maintain word integrity,word-breakThe attribute is controlled by style on the browser side to display text.It does not change the original text content, it just adjusts how the browser displays text in the layout for line breaks.wordwrapwhile keeping the integrity of words.word-break: break-allForces a newline at any character to fit the layout, even if it truncates words.

2.wordwrapHow does the filter perform when processing text with HTML tags?

wordwrapThe filter will treat strings containing HTML tags as plain text. This means that it will<p>/<a>The tag characters are also counted, and line breaks are inserted at appropriate word boundaries (i.e., at spaces).It does not intelligently parse HTML structures and does not wrap lines within tags.Therefore, it is usually recommended to ensure that the content is plain text before automatic line breaking, or when usingwordwrapcombine them after that.safeandlinebreaksbrThe filter handles possible HTML tags to display them correctly.

3. Why does my Chinese content usewordwrapNo line break effect after the filter?

wordwrapThe filter mainly relies on spaces in the text to identify word boundaries and perform line breaks. Due to the characteristics of the Chinese language, words are usually not separated by spaces, which causes a continuous Chinese text to bewordwrapRegarded as an inseparable 'long word'. Therefore, it will not be automatically wrapped within this Chinese text.If you need to force a line break in a long Chinese text to fit the layout, you can consider using CSS.word-break: break-all;Property, or when processing on the back-end, use programming methods to insert zero-width spaces between Chinese words, etc., to assist in line breaks.

Related articles

The `random` filter: How to randomly select a character or element from a string or array?

In Anqi CMS template design, we often need to make the website content dynamic and random to avoid the content being too static and to enhance the browsing fun of users.When you want to randomly select one from a series of preset options, text, or data to display, the `random` filter is a very practical tool.It can help you randomly pick a character or element from a string or array (also known as a list or set), adding an element of unpredictability to your website content.

2025-11-08

`repeat` filter: How to repeat a specified string N times to generate placeholder content?

In the world of AnQi CMS templates, we often need some flexible tools to quickly build page layouts or generate placeholder content when there is no actual data.The `repeat` filter is a feature that is not very noticeable but very practical, and it can help you output the specified string N times in a concise way. ### `repeat` filter: A practical technique for quickly generating repeated strings In website content management and template design, we often need to display a character, phrase, or even a short segment of HTML code repeatedly.For example, when you need a separator line

2025-11-08

The `urlize` and `urlizetrunc` filters: How to handle the display and truncation of long URLs?

In website content operation, we often need to handle various links, whether it is external resources cited in articles or websites left by users in comment sections.However, when these links are too long, they may not only destroy the overall layout of the page, affect its aesthetics, but may also reduce the reading experience of users.Luckyly, AnQiCMS provides a pair of very practical filters—`urlize` and `urlizetrunc`, which can help us manage and display these long or unformatted URLs gracefully.

2025-11-08

How to automatically recognize URLs in AnQiCMS templates and convert them into clickable hyperlinks?

In website operation, the flexibility of content display and user experience is crucial.We often need to display some external links or references in the content of articles, product descriptions, or various text areas.Manually adding the HTML `<a>` tag to each URL is not only inefficient but also prone to errors, especially when the amount of website content is large or needs to be updated frequently, which is undoubtedly a cumbersome task.AnQiCMS provides an elegant and efficient solution to this pain point.

2025-11-08

How to implement the unified or independent display of multi-site content on the AnQiCMS front end?

For users with multiple websites, brands, or content branches, how to efficiently manage and flexibly display content has always been a core challenge in website operations.AnQiCMS is a content management system designed for multi-site management, which not only provides a unified backend management interface, but also gives users great freedom in front-end content display, allowing for completely independent content display and easy unified calling and aggregation display across sites.Next, we will delve into how AnQiCMS implements these two seemingly opposing yet complementary content display strategies.

2025-11-08

How to flexibly control the display of website front-end content using a custom content model?

In website content operation, the way content is presented is the key to attracting users, conveying information, and achieving business goals.AnQiCMS (AnQiCMS) provides a highly flexible custom content model, which is a powerful tool for us to finely control the display of website front-end content.It is not just the background data management, but also the cornerstone of the front-end visual and functional experience. ### The Foundation Role of Flexible Content Models One of the core advantages of Anqi CMS is its "flexible content model" feature.In simple terms, a content model is a predefined structural framework for different types of content.

2025-11-08

How does AnQiCMS ensure that users of different languages see the correct content display in its multilingual support feature?

In an increasingly globalized digital world, whether a website can provide a smooth and accurate experience for users of different languages has become a key factor in measuring its success or failure.AnQiCMS (AnQiCMS) knows this and has integrated powerful multilingual support into its design, ensuring that your website content can accurately reach global users.How does AnQiCMS ensure that users of different languages see the correct content display?This is not a one-step solution, but a comprehensive solution from the system level to content management, to front-end display and search engine optimization.

2025-11-08

How to optimize the URL structure of a website using pseudo-static and 301 redirect management to improve the visibility of front-end content?

In website operation, the structure of URL (Uniform Resource Locator) is directly related to the performance of the website in search engines and the user experience.A clear, concise, and meaningful URL can not only help search engines better understand the content of the page, but also allow users to identify the theme of the page at a glance, enhancing trust.AnQiCMS is well-versed in this field and provides powerful features for handling pseudo-static and 301 redirect management, helping you optimize the URL structure of your website and significantly improve the visibility of front-end content.

2025-11-08