In AnQiCMS template design, in order to better display content, we often need to format text. Among them,linenumbersThe filter is a very practical tool, it can automatically add line numbers to multi-line text content, which is particularly helpful for displaying code snippets, step instructions, or long document list reading experience.
linenumbersThe core function and role of the filter
linenumbersThe main function of the filter is to receive a segment of text content, and add a serial number starting from 1 in front of each line of this content, usually with a1.Hello
World2.such formatting presents. It helps readers clearly distinguish the line numbers, improving readability and citation efficiency, especially when dealing with technical documents, code examples, or any content that requires line-by-line analysis.
linenumbersData types supported by the filter
According to the template filter design of AnQiCMS,linenumbersThe filter mainly targetsstring typedata processing, especially includingMulti-line text.
This is because its working principle is to identify the newline characters in the string(\n),and each newline character is considered a separate line, then line numbers are added one by one. Therefore, if your data is a string, whether it is plain text, HTML code snippet, or any other form of text information, as long as it contains logical line separators (i.e., newline characters),linenumbersThe filter can be effectively processed.
For example, if you have a variablearchive.Contentwhich contains multiple paragraphs of text or code, each separated by a newline. When you willlinenumbersThe filter is applied to this variable, it will traversearchive.Contenteach line and add a serial number to it.
For non-string data types such as numbers, booleans, arrays, or objects,linenumbersThe filter, when applied, usually tries to implicitly convert these non-string types to strings. If the conversion is successful, it treats it as a single-line string for processing and only adds one1.If translation fails or does not meet expectations, it may not produce meaningful results. Therefore, **practical experience is essential to ensurelinenumbersThe filter is always applied to string variables expected to contain text.
Actual application example
Suppose you need to display a piece of code or text with line numbers on the article detail page.archive.ContentContaining the content of the article you edited. You can use it in the template like thislinenumbersFilter:
{# 假设 archive.Content 变量中存储着多行文本内容 #}
<pre><code>
{{ archive.Content|linenumbers|safe }}
</code></pre>
In this example,archive.ContentIt is the main content of the article (string type).linenumbersThe filter will add line numbers to each line of this content. Sincearchive.ContentMay contain HTML tags or special characters, to ensure they are rendered correctly by the browser rather than being escaped and displayedlinenumbersafter thatsafefilter.
Combined with other filters
When processing multiline text,linenumbersfilters are often considered together withlinebreaksorlinebreaksbrfilters. Although they are all related to processing text lines, their focus of function is different:
linebreaks: Convert line breaks in text to HTML tags<p>and add tags to the empty lines within<br/>.linebreaksbr: Only convert line breaks in text to HTML tags<br/>.
AndlinenumbersThe filter is added directly before the logical line of text. In some complex scenarios, if the original content format needs to be processed first,linebreaksorlinebreaksbrPerform HTML processing and then add line numbers, it may be necessary to adjust the application order of the filters to ensure that the line numbers are added based on the final visual lines. However, in most caseslinenumbersDirectly acting on the original text string, and parsing the newline character itself is enough.
In summary,linenumbersThe filter is a simple and powerful tool in the AnQiCMS template, which automatically adds line numbers to multi-line string text content, greatly enhancing the readability and structure of specific types of content.
Frequently Asked Questions (FAQ)
1.linenumbersCan the filter change the display format of line numbers? For example, to display as(1)OrA.?Currently,linenumbersThe filter's default line number display format is fixed, starting from 1 and incrementing by numbers, followed by an English period and a space (for example1./2.)。AnQiCMS built-inlinenumbersThe filter does not support custom styles or prefixes for line numbers. If a more flexible line number format is needed, additional processing may be required through JavaScript on the front end.
2. If my text content is only one line,linenumbershow will the filter handle it?If the text content does not contain any newline characters,linenumbersThe filter will treat it as a single line string. In this case, it will only add a number to the content of this line.1.number of the line. Even if it is an empty string, the applicationlinenumbersThe line will also output an empty line, and it will be displayed before it1..
3.linenumbersCan the filter recognize line breaks within HTML tags and add line numbers to them?
linenumbersThe filter is based on the actual newline characters in the string (\n) and operates accordingly. If HTML tags (such as<p>or<div>) cause visual line breaks but there are no actual newline characters in the string\nthenlinenumbersNumbers will not be added at these visual line breaks. They only exist in string literals.\nThe place to be numbered. If you want the HTML content to be properly numbered, it usually means that the HTML content itself needs to contain\nand should be combined when outputtingsafeThe filter prevents HTML from being escaped.