In website content operation, the presentation effect of long articles or large blocks of descriptive text directly affects the user's reading experience.If content is piled together without good formatting and proper line breaks, even the most精彩 content will make readers hesitate.AnQiCMS as a content management system that focuses on user experience, provides a variety of mechanisms to cleverly handle the automatic line break of long text, thereby greatly improving the readability of the front-end page.
Basic Coverage: Markdown editing and automatic line breaks
First, AnQiCMS has built-in support for Markdown editors.When we write articles, product descriptions, or single-page content through the back-end editor, we can directly use Markdown syntax for formatting.For example, use blank lines to separate paragraphs, or use simple carriage returns to insert line breaks.These Markdown tags are automatically converted to the corresponding HTML tags by AnQiCMS after saving, such as<p>Used for paragraphs, or converted to them in some cases through subsequent filters<br/>This text is displayed on the page with clear structure and well-defined paragraphs.
However, relying solely on Markdown conversion is sometimes not enough, especially when the content is not edited through Markdown or contains carriage returns from the original text (\nWhen. HTML will treat multiple spaces and newline characters as a single space.To ensure that these natural line breaks are correctly identified and displayed on the front-end page, AnQiCMS provides powerful filters at the template rendering level.
Enhanced typesetting:linebreaksSeries of filters to重现paragraphs
AnQiCMS template engine provides a set namedlinebreaksfilter, which can intelligently convert newline characters in the text content to\nConvert it to HTML tags that the browser can understand and render, thereby simulating the original paragraph and line break structure.
linebreaksFilterThis filter is considered a master in handling line breaks in ordinary text.It detects line breaks in text. For two consecutive line breaks (indicating an empty line), it wraps the text before and after them separately.<p>In tags, a new paragraph is formed. While for a single newline character, it will be converted to<br/>Label, implements inline line breaks. This is like a meticulous typesetter, automatically formatting the background input text content into a neat HTML paragraph, greatly improving the reading experience.For example, if you enter in the background content field:
这是第一段内容。 这一行是第一段的第二行。 这是第二段内容。 这一行是第二段的第二行。By
{{ item.Content|linebreaks|safe }}The front end is likely to render such template code:<p>这是第一段内容。<br/>这一行是第一段的第二行。</p> <p>这是第二段内容。<br/>这一行是第二段的第二行。</p>linebreaksbrFilterIf you do not need complex paragraph division, just want to simply replace all newline characters with<br/>Tags, thenlinebreaksbrThe filter will be your ideal choice. It focuses only on transforming each\nto<br/>without adding any extra.<p>Label. This is very useful when you need to maintain a compact text layout, but also want to have clear line breaks.
An important detail when using these filters is to combine|safefilter.safeThe filter informs the AnQiCMS template engine that the current output content is safe HTML and does not require additional escaping. This ensureslinebreaksorlinebreaksbrThe converted HTML tags can be parsed correctly by the browser, rather than being displayed as plain text.
Optimize the visual:wordwrapThe filter prevents long text from overflowing.
In some cases, even with paragraphs and line breaks, if there is a long character sequence without spaces (such as a very long English word, or a continuous sequence of Chinese), the text may overflow its container, causing the page layout to become disordered, even appearing horizontal scroll bars, seriously affecting readability. To avoid this situation, AnQiCMS provideswordwrapfilter.
wordwrapFilterThis filter can intelligently insert line breaks in long text according to the length you specify.It tries to break lines at word boundaries to maintain semantic integrity.For example, you can set{{ item.Description|wordwrap:50 }}Let the text be wrapped at about 50 characters.This allows for the elegant 'collapsing' of long text even when the design container width is limited, adapting to the page layout and maintaining the neat and professional appearance of the page.
It is worth noting that,wordwrapThe filter processes Chinese text, as Chinese usually does not have spaces to distinguish wordwrapIt may not be able to make perfect punctuation in semantics, but it can still force the text to wrap at a specified length to prevent visual overflow.
Practice and Application
In AnQiCMS template files, these filters can be easily applied to any field containing long text content, such as:
- Document content of the article detail page (
archive.Content) - Category description (
category.Descriptionorcategory.Content) - Single-page content (
page.Content) - Even custom fields store long text
By flexible applicationlinebreaksandwordwrapCombine filters, with|safeThe statement ensures that AnQiCMS can present structured Markdown content on the front end, whether it is handling text with native line breaks or controlling the display of long, uninterrupted text, and achieve **readability and visual effects.This not only improves the user experience, but also helps the website content reach the audience in a more professional and readable way.
Frequently Asked Questions (FAQ)
1. Why is the content in the AnQiCMS backend editor wrapped in a row, but displayed as a group on the front end?
This is usually because HTML defaults to ignoring the line breaks produced by a single return key. If you just pressed return in the background editor\n),而没有使用Markdown的段落分隔(空行),或者内容未经过linebreaksorlinebreaksbrFiltering is applied, the browser will not render it as a visible line. The solution is to use the content field in the template with{{ 你的内容字段|linebreaks|safe }}or{{ 你的内容字段|linebreaksbr|safe }}The filter processes to ensure that the newline characters entered in the background can be converted to HTML.<p>or<br/>.
2.wordwrapCan the filter perfectly solve the automatic line break of all Chinese long texts?
`wordwrap