repeatFilter.

AnQiCMS template mechanism basics

When building a website with AnQiCMS, we often need to flexibly display content.AnQiCMS uses a template engine similar to Django, which allows us to call and process website data with concise syntax.{% system %}/{% archiveList %}(auto),还有一个非常实用的概念叫做“过滤器”(Filter)。过滤器可以对变量的值进行各种转换、格式化或处理,例如格式化日期、截取字符串,而repeatFilter is one of them, specifically used for repeated output of strings.

Core Function:repeatFilter

repeatThe filter, as the name implies, is to repeat a string according to the specified number of times. Its usage is very intuitive and simple.

Basic Usage

repeatThe basic syntax of the filter is: {{ 变量名或字符串 | repeat:次数 }}.

For example, if you want the string 'AnQi CMS' to be displayed 5 times, you can write it like this:

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

The output result will be:安企CMS安企CMS安企CMS安企CMS安企CMS

Useful scenarios: repeat the welcome message

Now, let's return to the example mentioned at the beginning of the article - repeating the welcome message.Suppose we want to repeat a welcome message in a certain block of a website, such as the footer or an advertisement area.repeatFilter to repeat it.

{% set welcome_message = "欢迎来到我们的网站!" %}
<p>{{ welcome_message|repeat:3 }}</p>

This code will first assign the text "Welcome to our website!" towelcome_messagethe variable, and then output it 3 times. This way, when you need to modify the welcome message, just modifywelcome_messageThe value of the variable can be used, very convenient.

More than just a welcome message: more application scenarios

repeatThe application of filters is far more than just repeating welcome messages; it can enhance the flexibility and operational efficiency of content display in many places:

  1. Creating visual separators:When you need to add a simple visual separator between different content blocks,repeatfilters come in handy.

    <div class="content-block">
        <!-- 内容 -->
    </div>
    <div class="separator">
        {{ "-"|repeat:50 }}
    </div>
    <div class="content-block">
        <!-- 更多内容 -->
    </div>
    
  2. Create emphasized text:Do you need to add some decorative characters before or after the title or some phrase to emphasize?repeatFilters can be easily implemented.

    <h1>{{ "✨"|repeat:3 }} 最新活动 {{ "✨"|repeat:3 }}</h1>
    
  3. Dynamic placeholder generation:In some scenarios where specific quantities of content need to be filled in,repeatThe filter can be used as a quick tool to generate placeholder content.

  4. Combined with backend data, to realize dynamic repetition:AnQiCMS allows you to configure various parameters in the background.You can configure the number of repetitions as a system parameter or a custom field in the content model, and then dynamically retrieve this value in the template.

    For example, you can add a custom parameter named in the "Global Function Settings" in the backgroundWelcomeRepeatTimeswith a value of5. Then use it like this in the template:

    {% system repeat_count with name="WelcomeRepeatTimes" %}
    {% set welcome_message = "感谢您的关注!" %}
    <p>{{ welcome_message|repeat:repeat_count }}</p>
    

    Thus, the number of repetitions can be flexibly controlled through backend settings, without the need to modify the template file, greatly improving operational efficiency.

Attention Points and **Practice

  • Performance considerations:AlthoughrepeatThe filter is very convenient, but it still needs to be noticed for its potential impact on page loading performance, especially when dealing with very long strings or very frequent repetitions.Avoid unnecessary high frequency repetition.
  • Code readability:When the repeated string is long or the number of repetitions is dynamically obtained, it is recommended to assign the string or number to a variable first, and then use the filter. This will make the template code clearer and more readable.
  • Avoid overusing HTML tags:If you want to repeat content with HTML tags (such as)<span>欢迎!</span>),and expects the browser to correctly parse these repeated HTML, please be sure to inrepeatuse it after the filtersafeFilter, for example:{{ "<span>欢迎!</span>"|repeat:3|safe }}. But make sure that the duplicated HTML content is safe to prevent XSS attacks and other security risks.

By making reasonable use ofrepeatFilter, you will be able to manage and display website content more flexibly and efficiently, making your AnQiCMS website more vivid and interesting.


Common Questions (FAQ)

Q1:repeatCan the filter repeat HTML code?A1: It's okay. If the repeated string contains HTML tags, to make the browser parse them as HTML instead of plain text, you need torepeatafter the filtersafeFilter, for example{{ "<strong>你好!</strong>"|repeat:2|safe }}。But please note that when usingsafethe filter, make sure that the HTML content is reliable and secure to avoid potential XSS attack risks.

Q2: How do I repeat a content block instead of a string?A2: If you want to repeatedly display a content block containing complex structures (such as multiple HTML tags, images, etc.), rather than a simple string,forLoop tags would be a more appropriate choice. You can place the content that needs to be repeated between{% for %}and{% endfor %}, and set the number of repetitions, for example{% for i in "1,2,3"|split:"," %}<div>这是重复内容块</div>{% endfor %}.

Q3: How to dynamically controlrepeatThe repetition number of the filter, instead of writing it directly in the template?A3: AnQiCMS supports getting dynamic data from the back-end management interface. You can define a parameter (such as a parameter namedWelcomeRepeatTimesThe numeric type field), then use the template to{% system repeat_count with name="WelcomeRepeatTimes" %}tag to obtain this dynamic value, and use it asrepeatthe number of times parameter for the filter, such as{{ "哈喽"|repeat:repeat_count }}English translation: Thus, you can adjust the repetition frequency at any time without modifying the template code.