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 content for generating specific patterns, such needs are very common.AnQiCMS with its flexible template engine makes such operations extremely simple.This article will introduce how to use the built-in filter (Filter) function in AnQiCMS template to easily repeat the same string.
Core Function:repeatFilter
In AnQiCMS template syntax,repeatThe filter is designed to meet this requirement.Its function is to output a string repeatedly according to the number you specify.repeatFilters can easily handle it.
The usage is very intuitive, you just need to add a pipe symbol after the string variable or string literal that you want to repeat|torepeatthe filter, and pass in the number of repetitions.
For example, if you want to repeat the string 'AnQi CMS' five times on the page, you can write the template code like this:
{{ "安企CMS"|repeat:5 }}
After executing this code, 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 work in various scenarios:
1. Generate separators or decorative elements
Imagine that you might need to add a dashed line below the article title or place some special symbols before 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 count
repeatThe more powerful aspect of the filter is that the number of repetitions is not necessarily fixed; it can be a dynamic variable.This means that 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 within the loop to create more interesting layouts:
{% for i in 1..5 %} {# 假设循环5次 #}
<p>级别 {{ i }}:{{ "✦ "|repeat:i }}</p>
{% endfor %}
This code will output in order:级别 1:✦
级别 2:✦ ✦
级别 3:✦ ✦ ✦
级别 4:✦ ✦ ✦ ✦
级别 5:✦ ✦ ✦ ✦ ✦
3. Combined with variable assignment
If you need to use the repeated string multiple times or want to perform some processing before output, you can cooperatesetwith tags for variable assignment, 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>
Useful hints while using
AlthoughrepeatThe filter function is powerful, but there are also some points to pay attention to when using it:
Performance considerations:Avoid generating overly long strings.For example, repeating a character several hundred thousand times may consume more resources, especially when used in large quantities 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 number of repetitions.
Content escaping:In most cases,
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 HTML tags instead of displaying it as a literal, make sure to use a comma at the end of the entire expression|safeFilter. For example, to output multiple<br>Line break:<p>{{ "<br>"|repeat:3|safe }}</p>This will generate three actual line breaks.
PassrepeatFilter, AnQiCMS makes the repeated output of template content easy, whether it is a simple delimiter or a complex dynamic pattern, it can respond flexibly.Master this little trick, and it will help you build rich and diverse page layouts and content displays more efficiently.
Common Questions and Answers (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 perform the repeated output.
Q2: If I need to output different contents repeatedly, rather than the same string, does AnQiCMS have other methods?
Of course.repeatThe filter focuses on repetitionThe sameString. If you need to generate different but regular placeholder text (such as random paragraphs), you can use the built-in AnQiCMS.loremLabel. If you need to loop through 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?
In AnQiCMS,repeatThe filter repetition count is set to 0 or any negative number, it will not output any content, only return an empty string. This means you can safely use it in dynamic