In AnQiCMS, we often encounter scenarios where we need to repeatedly output a string, such as displaying star ratings on product detail pages or creating separators with special symbols in articles.Copying and pasting manually is not only inefficient but also very麻烦 when you need to change the number of repetitions. 幸运的是,AnQiCMS's powerful template engine provides a concise and efficient solution, allowing us to dynamically control the number of times a string is output repeatedly.

Get to 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 namedrepeatThe built-in filter. This filter is the key to solving the problem of repeated string output. Its function is very direct: it repeats a string according to the specified number of times.

UserepeatThe filter is very simple. You just need to specify the string to be repeated asobjand then use the pipe character|following thatrepeat:Filter, and specify the repetition count after the colon. For example, if you want the string "AnQiCMS" to be output repeatedly 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 mystery of dynamically adjusting the number of repeated outputs

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

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

Let's delve deeper into how to dynamically adjust the repetition frequency of string outputs 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, you can easily change the number of asterisks or dashes in the output without modifyingrepeatthe filter itself.

Scenario two: Implement true dynamism by combining content model data

The more common requirement is to dynamically generate repeated output 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 rating.

The AnQiCMS content model allows us to customize fields, for example, you can add a 'rating' field (of type number) to the 'product model'.When traversing 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.StockIt is also used for dynamic judgment and output. When the inventory is below 10, exclamation marks are repeated according to the inventory quantity to form a visual warning.

In this way, the product rating and inventory warning on our website have been realized through automation and dynamism, 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, special symbols are used to emphasize it, and the repetition frequency is also determined according to 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, repeatthe filter meetsif-elifThe logical judgment tag is used in combination, dynamically displaying different numbers of flame or bulb icons based on the importance level of the article, providing users with more intuitive information prompts.

Summary

AnQiCMS'repeatA filter is a small but powerful tool that delegates the control of string repetition to variables and dynamic data, greatly enhancing the flexibility of templates and the generation of dynamic content.Whether it is a simple visual effect or a complex data-driven display, mastering this filter can make your website content more expressive and automated.


Frequently Asked Questions (FAQ)

1.repeatCan the number of duplicates 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 a non-numeric type (such as the string "five") is provided, the filter may not work as expected, and may even ignore repeated outputs.Ensure provided to the template before using itrepeatThe value of the filter is a valid integer, or if the source data may not be an integer, it can be used firstintegerthe filter to convert.

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

3.repeatFilters andloremWhat are the differences between tags? repeatThe filter is used to take aspecifiedstring or variable content a fixed number of timesRepeat outputFor example, repeat "AnQi CMS" five times. AndloremTags are used to generateRandom Latin placeholder text(usually called "random dummy text"), often used in the template design stage, when you need to fill in a large amount of text but have not yet provided actual content.loremThe tag focuses on generating placeholder text,repeatThe filter focuses on repeating specific content sequentially.