In AnQi CMS template development, we sometimes need to add line numbers to multiline text content to better display code snippets, quoted paragraphs, or any information that needs to be clearly identified line by line.linenumbersThe filter is designed for this purpose.However, whether this filter can start counting from a custom starting number is a question in the minds of many users.linenumbersFilter defaults andAlways starts counting from number 1Custom starting number is not supported.

linenumbersFilter: Basic features and usage

linenumbersThe filter is mainly used to automatically add line numbers to multi-line text content. When you have a variable containing multi-line text, applying this filter will add an incremental number and separator to the front of each line (for example1./2.As this is very useful when it is necessary to refer to specific line numbers or to structure the text content.

Its basic usage is very concise and clear, usually acting on a string variable containing a newline character (\n)})

{% set my_code_snippet = "function greet() {\n  console.log('Hello, AnQiCMS!');\n}" %}
<pre>{{ my_code_snippet|linenumbers }}</pre>

The output effect of the above code will be:

1. function greet() {
2.   console.log('Hello, AnQiCMS!');
3. }

You can see that the line numbers start from 1 and increment line by line.

About the question of custom starting number counting

Many developers are usinglinenumbersWhen filtering, you may want it to support specifying a custom starting number, such as when you only display a certain code segment from a large file and want the line numbers to correspond to the actual line numbers in the original file, this need is particularly prominent.

However, according to the current template engine design of Anqi CMS and the official documentation,linenumbersFilterno additional parameters are provided to set the starting count pointThis means that regardless of how long your multi-line text content is or in what context it is called,linenumbersthe filter will strictly start from1.to mark the first line of content.

The practical application and effect

ThoughlinenumbersThe filter currently only supports counting from 1, but it still plays its due role in many scenarios, especially when it comes to formatting the output of plain text content. For example, you can apply it to display text content with empty lines:

{% set long_text_example = "第一行文本。\n第二行文本。\n\n第四行文本。" %}
<pre>示例文本:</pre>
<pre>{{ long_text_example|linenumbers }}</pre>

The output effect is as follows:

1. 第一行文本。
2. 第二行文本。
3. 
4. 第四行文本。

From this example, it can be seen that even if it is a blank line,linenumbersThe filter will also assign it an independent line number, maintaining the continuity of the line numbers.

Used in conjunction with other text processing filters

In practical applications,linenumbersFilters are often used withlinebreaksorlinebreaksbrCombine filters to ensure that the text content is correctly formatted according to line breaks before adding line numbers. For example, such usage is also mentioned in the document:

{% set multi_line_content = "This is a simple text.\nThis too, as a paragraph.\n\nRight?\nYep!" %}
<pre>{{ multi_line_content|linebreaksbr|linenumbers }}</pre>

here,linebreaksbrFirst, convert the newline characters to<br />tags, thenlinenumbersadd line numbers to the text blocks containing<br />tags. It should be noted thatlinenumbersthe processing isThe final text lineNot in the HTML structureporbr.

Summary

Of Security CMSlinenumbersA filter is a convenient tool used to add sequential line numbers to multi-line text content.The operation is simple, just apply it to a string variable containing a newline.Currently, this filter always starts counting from the number 1 and does not provide an option to customize the starting number.Although this may be somewhat limited in certain specific scenarios, it is still an efficient and practical solution for most needs for standard line number marking of plain text display.For future versions, we look forward to the AnQi CMS introducing the function of custom starting numbers, which will undoubtedly further enhance its flexibility and applicability.


Frequently Asked Questions (FAQ)

1.linenumbersCan the filter add line numbers to HTML content? linenumbersThe filter is designed to process plain text content.If you apply it to a string containing HTML tags, the HTML tags themselves will be treated as part of the plain text and numbered.<div>Hello</div>\n<div>World</div>May be considered as two lines, where the tags are also included in the line numbers, rather than numbering the rendered HTML content.Generally, if you need to number code or text blocks, it is recommended to extract the plain text content first.

2. How to makelinenumbersThe generated line numbers consistent with the line numbers in my original code file?due tolinenumbersThe filter is fixed to start counting from 1 and does not provide the functionality to customize the starting number, therefore it cannot directly keep the generated line numbers consistent with any starting position in the original code file.If you have this need, you may need to implement a more complex line number generation mechanism yourself (for example, by using a loop in conjunction with a manually set starting variable), or consider using a JavaScript library on the front end to implement a dynamic, customizable starting number line number display.

3.linenumbersWill it skip empty lines?No.linenumbersThe filter will identify each newline character (including consecutive newline characters) as a new line. Therefore, if your text content contains empty lines,linenumbersIt also assigns a unique line number to these blank lines, maintaining the continuity and integrity of the line numbers.