When building a website with AnQiCMS, we often encounter multi-line text content submitted by users, and we hope that it can be displayed in a more beautiful and layout-friendly manner, rather than simply stacking it into a long paragraph.Especially when this content comes from a specific user group, such as in-depth comments from VIP members or detailed answers from technical support staff, ensuring readability becomes particularly important.The powerful template engine and rich filter functions of AnQi CMS provide us with an elegant solution.
Understanding multiline text andlinebreaksThe necessity of filters
In web pages, the content entered by users in multiline text boxes is usually separated by newline characters (\n) to separate paragraphs.However, the browser does not render these newline characters as visual line breaks by default, but treats them as spaces.This will cause the user's expected segmented display to become a dense block of text, severely affecting the reading experience.
At this point, the Anqi CMS'slinebreaksThe filter came in handy. Its main function is to convert line breaks in text to HTML tags.<br />and use tags to separate logical paragraphs (text blocks separated by consecutive line breaks) for<p>Labels are wrapped. This way, the original line breaks and分段 intentions entered by the user can be accurately还原 on the web page, making the content structure clear and easy to read.
For example, a simple text:
第一行内容。
这是第二行。
这是第三段,中间有空行。
afterlinebreaksAfter the filter, it will output an HTML structure like this:
<p>第一行内容。<br />这是第二行。</p><p>这是第三段,中间有空行。</p>
This is different from just usinglinebreaksbrThe filter replaces all newline characters with<br />(such as第一行内容。<br />这是第二行。<br /><br />这是第三段,中间有空行。).linebreaksPay more attention to restoring paragraph structure, providing more professional formatting effects.
It should be noted in particular that when usinglinebreaks(or any filter that generates HTML tags) is usually used immediately after processing content|safeThe filter. This is because the template engine of Anqicms defaults to escaping all output content to prevent cross-site scripting attacks (XSS). If not|safe,linebreaksGenerated<p>and<br />Tags will be escaped<p>and<br />Therefore, they cannot be displayed normally as HTML elements.|safeThe filter explicitly tells the template engine that this part of the content is safe and can be rendered directly as HTML.
Set the multi-line text field in Anqi CMS
AnQi CMS has a flexible content model function, allowing us to customize various content types according to business needs. To apply multi-line text submitted by userslinebreaksFilter, the first step is to ensure that your content model includes a "multi-line text" field.
- Enter content model management:Log in to the AnQi CMS backend, navigate to the 'Content Management' section under 'Content Model.'
- Select or create a model: Select an existing content model (such as "article" or "product"), or create a new model to manage a specific type of content.
- Add custom field:On the selected content model editing page, find the "Content Model Custom Field" section. Click Add New Field, then:
- Parameter name:Fill in a name that is easy to understand, for example, 'VIP review content', 'user suggestion', etc.
- Call field:Fill in an English name, which will be used as a variable name in the template, for example,
vip_comment/user_suggestion. - Field type:Make sure to select the 'Multi-line text'.
- Other settings:Set whether required, default values, etc. according to your needs.
In this way, you will be able to provide for specific users.