How do I convert multi-line text with line breaks to HTML tags in the Anqi CMS template?
linebreaksThe filter can convert multi-line text into HTML tags by line breaks. The beginning and end of each line are enclosed.<p>and</p>If there are empty lines in the middle, they are also enclosed.<br/>.
It can also be used.linebreaksbrto process. And withlinebreaksThe difference is,linebreaksbrIt simply replaces the newline character with<br/>and does not add p tags at the beginning and end.
It can also be used.linenumbersNumber each line of the multiline text starting with symbol 1. For example1..
Usage method
linebreaksHow to use the filter:
{{ obj|linebreaks }}
linebreaksbrHow to use the filter:
{{ obj|linebreaksbr }}
linenumbersHow to use the filter:
{{ obj|linenumbers }}
Handle strings like thisthis is a text\nwith a new line in itIt can be written like this:
{{ "this is a text\nwith a new line in it"|linebreaks }}
# 显示结果
<p>this is a text<br />with a new line in it</p>
Example Demonstration
linebreaksFilter
{{ ""|linebreaks|safe }}
{{ simple.newline_text|linebreaks|safe }}
{{ simple.long_text|linebreaks|safe }}
{{ "john doe"|linebreaks|safe }}
# 显示结果
<p>this is a text<br />with a new line in it</p>
<p>This is a simple text.</p><p>This too, as a paragraph.<br />Right?</p><p>Yep!</p>
<p>john doe</p>
linebreaksbrFilter
{{ ""|linebreaksbr|safe }}
{{ simple.newline_text|linebreaksbr|safe }}
{{ simple.long_text|linebreaksbr|safe }}
{{ "john doe"|linebreaksbr|safe }}
# 显示结果
this is a text<br />with a new line in it
This is a simple text.</p><p>This too, as a paragraph.<br />Right?</p><p>Yep!
john doe
linenumbersFilter
{{ simple.long_text|linebreaksbr }}
# 显示结果
1. This is a simple text.
2. This too, as a paragraph.
3.
4. Right?
5. Yep!