In web template design, we often encounter the need to repeatedly output a specific string or character pattern to achieve visual separation, emphasis, or layout effects.For example, you may want to display a line of multiple hyphens between content blocks, or repeat asterisks in a certain area as decoration.In the AnQiCMS template system, thanks to its syntax similar to the Django template engine, such repeated output is very flexible and efficient.

This article will delve into how to cleverly utilize built-in functions in the AnQiCMS template to repeatedly output a specific string multiple times, and provide two main methods and their applicable scenarios.

Method one: utilizerepeatA filter that repeats strings simply and efficiently

For scenarios where a specific string needs to be repeated simply, AnQiCMS template provides a namedrepeatThe built-in filter, this is the most direct and concise way to achieve this purpose. It allows you to specify a string and the number of repetitions, then output the result directly.

Working principle: repeatThe filter takes two parameters: the string (or variable) you want to repeat, and an integer representing the number of repetitions. It will concatenate the string the specified number of times and return it.

Usage example:Assume you want to output a line of 20 dashes under the article title.

{# 假设这是您的文章标题 #}
<h1>文章详情页标题</h1>

{# 使用 repeat 过滤器重复输出 '-' 20 次 #}
<p>{{ "-"|repeat:20 }}</p>

{# 或者,如果您想重复一个更复杂的图案,比如星号和空格 #}
<p>{{ "* "|repeat:10 }}</p>

{# 您也可以将要重复的字符串存储在一个变量中 #}
{% set separator_char = "=" %}
<p>{{ separator_char|repeat:25 }}</p>

Actual effect preview:

文章详情页标题

--------------------

* * * * * * * * * * 

=========================

Application scenarios:This method is very suitable for generating simple text delimiters, decorative patterns, or any string repetition that does not require complex logic.Its advantages are that the code is extremely concise, easy to understand and maintain, and has high execution efficiency.

Method two: combineforLoop to achieve more flexible repeated output

When your repeated requirements are not just simple string concatenation, but need to repeatedly output an HTML structure, or include some dynamic content in each repetition, or even execute different logic based on the number of iterations, forLooping makes it a more powerful choice.

AnQiCMS template system supportsforLooping, which allows you to iterate through a collection or repeat rendering content by simulating the number of loop iterations.

Working principle: forLoops allow you to iterate over an array, list, or iterable object. Although the AnQiCMS template does not provide a direct likerange(N)Such a function is used to generate a number sequence for a pure counting loop (usually requiring the backend to provide a list containing N elements, or to useforloop.CounterCome control), but we can simulate it by setting up a list containing enough elements or combiningsetSimulate with tags.

Usage example:Suppose you want to repeat output 5 specific style small squares as visual elements.

{# 假设我们需要重复输出 5 次 #}
{% set num_blocks = 5 %}

{# 这里我们创建一个包含 5 个元素的虚拟列表,以便 for 循环可以遍历 #}
{% set dummy_list = ["", "", "", "", ""] %}

{# 遍历虚拟列表,每次循环输出一个 div 元素 #}
<div class="block-container">
    {% for _ in dummy_list %} {# 使用 '_' 表示我们不关心当前迭代的元素值 #}
        <div class="visual-block"></div>
    {% endfor %}
</div>

{# 如果你希望在每次重复中显示当前的循环次数,可以使用 forloop.Counter #}
<p>
    {% for _ in dummy_list %}
        项目 {{ forloop.Counter }} 
    {% endfor %}
</p>

Actual effect preview:(Supposevisual-blockIs a CSS class that defines width, height, and background color

<div class="visual-block"></div>
<div class="visual-block"></div>
<div class="visual-block"></div>
<div class="visual-block"></div>
<div class="visual-block"></div>

项目 1 项目 2 项目 3 项目 4 项目