In website content operation, we often encounter situations where it is necessary to repeat certain information, such as repeating the copyright information in the footer, repeating a greeting phrase in the welcome area, or adding visual separators between list items.Copying and pasting manually is not only inefficient but also very麻烦 when it needs to be modified.Fortunately, AnQiCMS's powerful template engine provides a simple and efficient method to solve this problem, one very practical feature being thatrepeatfilter.

AnQiCMS template mechanism basics

When using AnQiCMS to build a website, we often need to display content flexibly.AnQiCMS uses a template engine similar to Django style, which allows us to call and process website data with concise syntax.In this template system, in addition to various 'tags' (such as{% system %}/{% archiveList %}An equal sign, for example, is a very practical concept called "Filter". The Filter can perform various transformations, formatting, or processing on variable values, such as formatting dates, truncating strings, and so onrepeatThe filter is one of them, specifically used for repeating the output string.

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 to repeat the string “AnQiCMS” 5 times, you can write it like this:

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

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

Practical scenario: Repeat the welcome message

Now, let's go back to the example mentioned at the beginning of the article - repeating the welcome message.Suppose we want to display a welcome message repeatedly in a certain block of the website, such as the footer or an advertisement spot.You can first define a variable to store the welcome message, then userepeatRepeat it with a filter.

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

This code will first assign the string "Welcome to our website!" to a variablewelcome_messageand then output it 3 times. This way, when you need to change the greeting, just modify thewelcome_messageThe value of the variable is sufficient, very convenient.

More than just a greeting: more application scenarios.

repeatThe application of filters is not just about repeating welcome messages, it can also 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 a phrase to emphasize?repeatThe filter can be easily implemented.

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

  4. Combined with backend data, to achieve 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 Feature Settings" in the backgroundWelcomeRepeatTimeswith a value of5. Then use it in the template like this:

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

    This allows the number of repetitions to be flexibly controlled through backend settings, without modifying the template file, greatly improving operational efficiency.

Cautionary notes and **practice

  • Performance consideration: ThoughrepeatThe filter is very convenient, but when used in practice, especially when dealing with very long strings or a large number of repetitions, one must still pay attention to its potential impact on page loading performance.Avoid unnecessary ultra-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 the number of repetitions to a variable first, and then use the filter, which can make the template code clearer and more readable.
  • Avoid overusing HTML tags:If you want to repeat content with HTML tags, for example,<span>欢迎!</span>),and expect the browser to correctly parse these repeated HTML,please be sure to userepeatafter the filter usessafefor example:{{ "<span>欢迎!</span>"|repeat:3|safe }}But also, be sure to ensure that the repeated HTML content is safe to prevent XSS attacks and other security risks.

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


Frequently Asked Questions (FAQ)

Q1:repeatCan the filter repeat HTML code?A1: Okay. If the repeated string contains HTML tags, in order for the browser to parse them as HTML instead of plain text, you need torepeatafter the filtersafeFilter, for example{{ "<strong>你好!</strong>"|repeat:2|safe }}Please note that when usingsafeMake sure the HTML content is reliable and secure when using the filter to avoid potential XSS attack risks.

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

Q3: How to control dynamicallyrepeatThe repetition count of the filter, rather than hard-coded directly in the template?A3: AnQiCMS supports fetching dynamic data from the backend management interface. You can define a parameter (for example, a parameter namedWelcomeRepeatTimesThe numeric type field, then use{% system repeat_count with name="WelcomeRepeatTimes" %}tag to get this dynamic value and use it asrepeatthe number of times parameter for the filter, such as{{ "哈喽"|repeat:repeat_count }}. You can adjust the repetition frequency at any time without modifying the template code.