In AnQiCMS template design, in order to better display content, we often need to format the text. Among them,linenumbersThe filter is a very practical tool, which can automatically add line numbers to multi-line text content. This is particularly helpful for displaying code snippets, step-by-step instructions, or the list reading experience of long documents.

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 sequential number starting from 1 before each line of this content, usually in the form of1.2.English format presented like this.It can help readers clearly distinguish the line numbers of text, improve the readability and reference efficiency of text, especially when dealing with technical documents, code examples, or any content that requires line-by-line analysis, its value is self-evident.

linenumbersData types supported by the filter

According to the template filter design of AnQiCMS,linenumbersThe filter mainly targetsstring typeprocessing the data, especially containingMulti-line textstrings.

This is because its working principle is to identify newline characters in strings (\n),并将每个换行符分隔开的文本视为独立的一行,然后为这些行逐一添加行号。因此,如果您的数据是一个字符串,无论其内容是纯文本、HTML代码片段,还是其他任何形式的文本信息,只要它包含有逻辑上的“行”分隔(即换行符),linenumbersThe filter can effectively process it.

For example, if you have a variablearchive.Contentwhich contains multiple paragraphs of text or code, separated by new lines. When you willlinenumbersThe filter is applied to this variable, it will iterate over.archive.ContentEach line in it, and add a serial number to it.

For non-string data types, such as numbers, boolean values, arrays, or objects,linenumbersFilter is typically applied by attempting to implicitly convert non-string types to strings. If the conversion is successful, it treats it as a single-line string and adds only one “1.The line number of the "auto" translation. If the conversion fails or does not meet expectations, it may not produce meaningful results. Therefore, **practical experience is crucial**.linenumbersThe filter is always applied to string variables expected to contain text content.

Actual application example

Suppose you need to display a segment of code or text with line numbers on the article detail page.archive.ContentEnglish version: Storing the content of the article you edited. You can use it like this in the template.linenumbersFilter:

{# 假设 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 displayed, we usuallylinenumbersafter thatsafeFilter.

used in combination with other filters

In processing multi-line text,linenumbersfilters are often considered withlinebreaksorlinebreaksbrfilters. Although they are both related to processing text lines, their functional focuses are different:

  • linebreaks: Convert line breaks in text to HTML's<p>tags, and add to blank lines in<br/>.
  • linebreaksbr: Only convert line breaks in text to HTML's<br/>Label.

whilelinenumbersThe filter adds a number to the logical line before the text. In some complex scenarios, if the original content format needs to be processed beforelinebreaksorlinebreaksbrPerform HTMLization, then add line numbers, you may need to adjust the order of filter application to ensure that the line numbers are added based on the final visible lines. However, in most caseslinenumbersAct directly on the original text string, and self-parsing newlines is enough.

In summary,linenumbersThe filter is a concise and powerful tool in AnQiCMS template, which automatically adds line numbers to the multi-line text content of string type, greatly enhancing the readability and structure of specific types of content.


Common Questions (FAQ)

1.linenumbersCan the filter change the display format of line numbers? For example, display as(1)orA.?Currently,linenumbersFilter's default line number display format is fixed, starting from 1 and incrementing by numbers, followed by an English period and a space (e.g.,1./2.)。AnQiCMS内置的linenumbersThe filter does not support custom line number styles or prefixes. For more flexible line number formatting, additional processing may be required on the front end via JavaScript.

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 line number for this line content.1.Even if it is an empty string, the applicationlinenumbersThe output will also include an empty line, and the same line number will be displayed before it1..

3.linenumbersCan the filter recognize line breaks within HTML tags and add line numbers to them? linenumbersFilter is based on the actual newline characters in the string\n). If HTML tags (such as<p>or<div>) cause visual line breaks, but there are no actual newline characters in the string\nso thatlinenumbersIt will not add line numbers at these visual line breaks. It only exists in string literals.\nwhere it is numbered. If you want the HTML content to be correctly numbered, it usually means that the HTML content itself needs to contain\nand should be combined when outputsafeFilter to prevent HTML from being escaped.