In AnQiCMS, we often encounter scenarios where we need to repeatedly output a string, such as displaying star ratings on product detail pages or using special symbols to create separators in articles.Manually copying and pasting is inefficient and very troublesome when you need to modify the number of repetitions.Fortunately, AnQiCMS's powerful template engine provides a simple and efficient solution that allows us to dynamically control the number of times a string is repeated.

KnowrepeatFilter: The core of string repetition output

AnQiCMS template system, based on the powerful features of Go language, integrates syntax similar to Django templates, which includes a template namedrepeatThe built-in filter. This filter is exactly what is needed to solve the problem of repeated string output. Its function is very direct: it repeats a string according to the number you specify.

UserepeatThe filter is very simple. You just need to take the string that needs to be repeated asobjand then through the pipe character|followingrepeat:Filter and specify the repetition count after the colon. For example, if you want the string '安企CMS' to be output 5 times, you can write the template code like this:

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

This code executes and then, the page will display:)

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

The secret to dynamically adjusting the repetition output count

repeatThe strength of the filter lies in the fact that the number of repetitions it accepts is not necessarily a fixed number. This is the key to our implementation of 'dynamic adjustment.' The 'number' parameter can be:

  1. A direct numeric value:As shown in the above example:5.
  2. A defined variable:You can use{% set %}tags to define a variable in the template to store the repetition count.
  3. Data from the database or context:This is the most common scenario of dynamic adjustment, such as obtaining a number from articles, products, or other data models.

Let us delve into how to dynamically adjust the repetition frequency of string output through several practical examples.

Scenario one: Flexible repetition based on variables

Suppose you want to control the repetition of a symbol based on a temporary variable, such as creating a visual emphasis effect. You can first define a variable in the template and then apply it torepeatOn the filter.

{% set starCount = 3 %}
<p>我的评级:{{"★"|repeat:starCount}}</p>

{% set separator = "---" %}
{% set lineLength = 5 %}
<p>这是一段内容。</p>
<p>{{"-"|repeat:lineLength}}</p>
<p>这是另一段内容。</p>

On the page, it will generate:

我的评级:★★★
这是一段内容。
-----
这是另一段内容。

Here, you just need to modifystarCountorlineLengththe value of the variable, and you can easily change the number of asterisks or dashes in the output without modifyingrepeatthe filter itself.

Scene two: Achieve true dynamism by combining content model data

The more common requirement is to dynamically generate duplicate outputs based on different attributes of the website content. For example, in a product list, each product has a rating (assuming it is stored initem.RatingIn the field), we hope to use asterisks to intuitively display this score.

The content model of AnQiCMS allows us to customize fields, such as you can add a 'rating' field (of type number) to the 'product model'.When iterating over the product list in the template, you can directly use the value of this field.

{% archiveList products with moduleId="2" type="list" limit="5" %}
    {% for item in products %}
    <div>
        <h3>{{item.Title}}</h3>
        <p>产品评分:{{"★"|repeat:item.Rating}}</p>
        <p>库存预警:{% if item.Stock < 10 %}{{"!"|repeat:item.Stock}} 低库存!{% endif %}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

In this example:

  • item.RatingIt will dynamically obtain the score value from the data of each product, thereby outputting the corresponding number of asterisks.
  • item.StockAlso used for dynamic judgment and output, when the inventory is below 10, exclamation marks will be repeated based on the inventory quantity to form a visual warning.

In this way, the product ratings and inventory warnings on our website have been automated and dynamicized, greatly enhancing the flexibility of content display and operational efficiency.

Scene three: Combine logical judgment to implement more complex dynamic behavior

Sometimes, we not only need to repeat the output based on a number, but also need to combine more complex logical judgments.For example, if the "importance" level of some content is very high, it is emphasized with special symbols, and the repetition frequency is also determined by the level.

{% archiveDetail article with name="Content" %} {# 假设article是当前文章对象 #}
    <h3>{{article.Title}}</h3>
    {% if article.ImportanceLevel > 3 %} {# 假设文章有一个自定义字段ImportanceLevel #}
        <p>重要提示:{{"🔥"|repeat:article.ImportanceLevel}} 这篇文章非常重要,请仔细阅读!</p>
    {% elif article.ImportanceLevel > 0 %}
        <p>提示:{{"💡"|repeat:article.ImportanceLevel}} 这篇文章值得关注。</p>
    {% endif %}
    <div>{{article.Content|safe}}</div>
{% endarchiveDetail %}

Here,repeatFilter is related toif-elifLogical judgment tags are combined and used to dynamically display different numbers of flame or light bulb icons based on the importance level of the article, providing users with more intuitive information prompts.

Summary

AnQiCMSrepeatThe filter is a small but powerful tool that greatly enhances the flexibility and dynamic content generation capabilities of templates by giving control over the repetition of strings to variables and dynamic data.Whether it's simple visual effects or complex data-driven presentations, mastering this filter can make your website content more expressive and automated.


Common Questions (FAQ)

1.repeatCan the repetition count in the filter be a decimal or non-numeric type? repeatThe filter expects an integer as the repetition count. If you provide a decimal, it usually tries to convert it to an integer (for example,3.7may be considered3)。If the provided is a non-numeric type (such as the string 'five'), the filter may not work as expected when the conversion fails, and may even ignore repeated outputs.repeatThe value of the filter is a valid integer, or if the source data may not be an integer, it can be converted firstintegerby the filter.

2. Can I repeat the content of a variable in addition to a fixed string?Of course you can.repeatFilters can not only repeat fixed strings, but also repeat the content of a variable. For example,{% set myChar = "⚫" %}{{myChar|repeat:5}}It will output⚫⚫⚫⚫⚫Or, if you want to repeat,item.Titlebe careful with the content of the field, as this may produce very long text, affecting page layout and user experience.

3.repeatfilters andloremWhat is the difference between tags? repeata filter is used to separate aspecifiedThe string or variable content is output repeatedly a fixed number of timesRepeated outputFor example, repeat 'Anqi CMS' five times. AndloremTags are used to generaterandom Latin placeholder text[English translation: (usually referred to as "random dummy text"), commonly used in the template design phase, when you need to fill in a large amount of text but do not have actual content yet.]lorem[English translation: The label focuses on generating placeholder text,repeatThe filter focuses on repeating specific content sequentially.