In the world of Anqi CMS templates, we often need some flexible tools to quickly build page layouts or generate placeholder content when there is no actual data.repeatThis is a very subtle but very practical feature that can help you output a specified string N times in a concise way.
repeatFilter: A Practical Trick for Quickly Generating Repeated Strings
In website content management and template design, sometimes we need to repeat a character, phrase, or even a short HTML code.For example, when you need a separator, a star rating display, or to temporarily fill the page area while waiting for backend data loading.Copying and pasting manually is not only inefficient but also makes template code long and difficult to maintain.At this time, Anqi CMS providesrepeata filter can come into play.
What isrepeatFilter?
In AnQi CMS's Django-like template engine, Filters are a powerful tool used to modify the display output of variables. They are used to|The symbol is connected to the variable or string at the end and can accept a parameter to customize its behavior.repeatThe filter is among many filters, and its core function is to repeat the specified string output according to your instructions.
How to userepeatFilter?
repeatThe usage of the filter is very intuitive. You just need to place the string to be repeated on the left, through the pipe symbol|连接repeatAnd use the colon after the filter:To specify the number of repetitions.
The basic syntax structure is as follows:{{ 要重复的字符串 | repeat:次数 }}
Here要重复的字符串Any text content can be次数An integer represents how many times the string should be repeated.
Let's take a simple example: If you want to output 'AnQi CMS' five times on the page, you can write it like this:
{{ "安企CMS"|repeat:5 }}
The output of this code will be:安企CMS安企CMS安企CMS安企CMS安企CMS
Practical scenario examples
repeatThe filter is very flexible in practical applications, and here are some common application examples:
Generate placeholder textWhen you are designing a new article list or product display page but do not have real titles or descriptions available, you can use
repeatGenerate placeholder content to quickly check the layout effect.<p>文章标题:{{ "这是占位标题内容"|repeat:2 }}</p> <p>文章简介:{{ "这是一段简短的占位文本内容,用于填充页面空间。"|repeat:3 }}</p>Output effect:
文章标题:这是占位标题内容这是占位标题内容文章简介:这是一段简短的占位文本内容,用于填充页面空间。这是一段简短的占位文本内容,用于填充页面空间。这是一段简短的占位文本内容,用于填充页面空间。Create visual separatorCommonly used visual separator on the page, especially one made up of characters, can be
repeatIt can be easily realized<div class="section-separator"> {{ "-"|repeat:80 }} </div> <p>这里是正文内容。</p> <div class="section-separator"> {{ "="|repeat:40 }} </div>Output effect:
--------------------------------------------------------------------------------这里是正文内容。========================================Display star ratingCan be used on product detail pages or user review modules
repeatDynamically display star ratings.{% set rating = 4 %} {# 假设从后台获取的评分是4星 #} <div> 当前评分:{{ "★"|repeat:rating }} </div>Output effect:
当前评分:★★★★Generate duplicate content with HTML.If you need to repeat a string that contains HTML tags and want the browser to parse these tags normally, you need to combine
safethe filter to use.safeThe filter tells the template engine that this content is safe and does not need to be HTML escaped.<div class="badges"> {{ "<span style='color: green; margin-right: 5px;'>✓</span>"|repeat:3|safe }} 已通过安全验证 </div>The output effect (assuming the browser parses):
✓ ✓ ✓ 已通过安全验证
Deep understandingrepeatFilter
- Non-string input:
repeatThe filter is designed to process strings. If you try to repeat operations on non-string types (such as numbers, boolean values), they are usually automatically converted to strings before executing the repeat. - The repetition count is 0 or negative:When the repetition count you set is 0 or any negative number,
repeatThe filter outputs an empty string. This is a reasonable default behavior that avoids unnecessary errors. - The string is empty:If a repeated string is empty itself, then regardless of how many times it is repeated, the result is still an empty string.
repeatThe filter provides great convenience for the template development of AnQi CMS in a simple and clear manner.Whether it is used for rapid prototyping, filling test data, or handling simple content display needs, it can help us write more concise and readable template code.
Frequently Asked Questions (FAQ)
Q1:repeatCan the filter output HTML tags repeatedly and allow the browser to parse them normally?A1: It's okay. If your string contains HTML tags and you want the browser to parse these tags instead of displaying them as plain text, you need to use them together.safea filter. For example:{{ "<b>强调文本</b>"|repeat:2|safe }}.
Q2: What will be the result if I set the repetition count to 0 or negative?A2: When you arerepeatWhen the filter is set to 0 or any negative number, it will output an empty string. This is to ensure that no exceptions or errors occur in the page content when the repetition condition is not met.
Q3:repeatFilters andloremWhat are the differences between tags, and what scenarios are they suitable for?A3:repeatFilter is used for repetitionThe string you specify explicitlyRepeated N times, for example, "AnQi CMS" or "★". It is suitable for scenarios that require precise control over repeated content.loremTags are used toGenerate random Latin placeholder contentThis is commonly used in the early stages of designing templates, where a large amount of text is needed to fill the layout, but the content should not have actual meaning to avoid distracting from the design. In simple terms,repeatIs 'repeating fixed content'loremIs 'generating random content'.