In web design and content display, we often encounter scenarios where we need to repeatedly output a certain string.Whether as a separator, decorative element, or placeholder for generating specific patterns, such needs are very common.AnQiCMS with its flexible template engine makes such operations extremely simple.This article will introduce you to how to use the built-in Filter function in AnQiCMS template to easily repeat the same string.

Core function:repeatFilter

In the template syntax of AnQiCMS,repeatThe filter is designed to meet this need. Its function is to repeat a string according to the number you specify.No matter if you want to repeat a character, a word, or an entire sentence,repeatFilters can easily handle it.

The method of use is very intuitive, you just need to put a pipe symbol after the string variable or string literal that you need to repeat|Continuerepeatand pass in the number of repetitions.

For example, if you want to repeat the string "AnQiCMS" on the page 5 times, you can write the template code like this:

{{ "安企CMS"|repeat:5 }}

After this code is executed, the following will be displayed on the page:

安企CMS安企CMS安企CMS安企CMS安企CMS

Actual application scenarios and examples

repeatThe flexibility of the filter allows it to be used in a variety of scenarios:

1. Generate separators or decorative elements

Imagine, you may need to add a dashed line below the article title or place some special symbols before the list items to enhance the visual effect.

<p>最新公告标题</p>
<p>{{ "-"|repeat:30 }}</p> {# 生成30个“-”作为分隔线 #}

<ul>
    <li>{{ "● "|repeat:1 }} 产品服务介绍</li>
    <li>{{ "● "|repeat:1 }} 联系我们方式</li>
    <li>{{ "● "|repeat:1 }} 用户隐私政策</li>
</ul>

2. Dynamic repetition times

repeatThe powerful place of the filter is that the number of repetitions is not necessarily a fixed number, it can be a dynamic variable.This means you can flexibly control the number of repetitions of a string based on background data or user interaction.

For example, on a product detail page, you may need to display the corresponding number of star icons based on the product's rating (assumed to be 1 to 5 stars):

{% set productRating = 4 %} {# 假设从后台获取的产品评分为4星 #}
<p>产品评分:{{ "⭐"|repeat:productRating }}</p>

IfproductRatingThe value is 4, then the page will display "Product rating: ⭐⭐⭐⭐".

You can also use the current loop index to dynamically adjust the repetition count, creating more interesting layouts:

{% for i in 1..5 %} {# 假设循环5次 #}
    <p>级别 {{ i }}:{{ "✦ "|repeat:i }}</p>
{% endfor %}

This code will output in sequence:级别 1:✦ 级别 2:✦ ✦ 级别 3:✦ ✦ ✦ 级别 4:✦ ✦ ✦ ✦ 级别 5:✦ ✦ ✦ ✦ ✦

3. Combined with variable assignment

If you need to use the repeated string multiple times, or want to process it first before outputting, you can cooperate withsettags to assign variables, improving the readability and reusability of the template:

{% set longSeparator = "—"|repeat:50 %}

<section>
    <h3>第一部分内容</h3>
    <p>这里是第一部分的一些描述。</p>
    <p>{{ longSeparator }}</p> {# 引用已定义的变量 #}
</section>

<section>
    <h3>第二部分内容</h3>
    <p>这里是第二部分的一些描述。</p>
    <p>{{ longSeparator }}</p> {# 再次引用,无需重新计算 #}
</section>

Use instructions when in use

AlthoughrepeatThe filter function is powerful, but there are also some suggestions to pay attention to when using it:

  • Performance consideration: Avoid generating excessively long strings. For example, repeating a character several hundred thousand times may consume more resources, especially when used extensively in loops, which may affect the rendering performance of the page.In most website display scenarios, it is rarely necessary to have such an extreme repetition frequency.

  • Content escaping:Generally speaking,repeatThe filter outputs plain text. If your original string contains HTML special characters (such as<or>),and you want the browser to parse it as an HTML tag instead of displaying it as a literal, make sure to use it at the end of the entire expression|safeFilter. For example, to output multiple<br>Carriage return:

    <p>{{ "<br>"|repeat:3|safe }}</p>
    

    This will generate three actual carriage returns.

ByrepeatFilter, AnQiCMS makes it easy to duplicate template content, whether it's simple delimiters or complex dynamic patterns, it can handle them flexibly.Mastering this technique will help you build rich and diverse page layouts and content displays more efficiently.


Frequently Asked Questions (FAQ)

Q1:repeatWhat types of data can the filter repeat?

repeatThe filter is mainly used for string type data. If you try to repeat numbers or other types (such as{{ 123|repeat:3 }}),AnQiCMS will implicitly convert it to the string “123”, and then output it repeatedly.

Q2: If I need to output different content repeatedly, rather than the same string, does AnQiCMS have other methods?

Of course.repeatThe filter focuses on repetitionSameA string. If you need to generate different but regular placeholder text (such as random paragraphs), you can use the built-in function of AnQiCMS.loremLabel. If you need to iterate over a set of data and display each item (such as an article list),forThe loop tag is a better choice.

Q3: If the repetition count is set to 0 or negative,repeatWhat will the filter output?

If in AnQiCMS,repeatThe repetition count of the filter is set to 0 or any negative number, it will not output any content, but will return an empty string. This means you can safely use it in dynamic