In AnQiCMS, we often need to repeat certain texts, code snippets, or dynamic content multiple times, whether it is for layout design, placeholder filling, or for list display.Implementing this feature, AnQiCMS provides flexible template tags and filters to make content operation efficient and personalized.

Understanding the need for repeated text display

The repetition of text can be divided into two main cases: one is simply to output a fixed text or variable N times; the other is to traverse a data set (such as a list of articles, product categories) and display each entry, forming dynamic repeated content.AnQiCMS's template system can well meet these two needs.

Implement the batch repetition of fixed text:repeatFilter

If you need to simply repeat a static text or the value of a variable a specified number of times, AnQiCMS'srepeatThe filter is an ideal choice. This filter can be directly applied to any string or any variable that can be converted to a string, and it will concatenate and repeat it according to the number of times you set.

The usage is very intuitive:{{ 需要重复的文本或变量 | repeat:次数 }}

For example, if we want to repeat the word "AnQi CMS" 5 times on the page, we can directly write it like this in the template:

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

This code's output will be: 安企CMS安企CMS安企CMS安企CMS安企CMS.

Actual application scenarios:

  • Visual separator or decorative pattern:For example, repeated display---/***or#Create simple visual separation effects.
  • Placeholder filling:In the early stages of development, you need to quickly fill in some text to preview the layout, you can use"Placeholder Text "|repeat:10to generate a repeated text block.
  • A brief dynamic data repetition:If the value of a variable itself needs to be displayed repeatedly, for example, if a feature of a product needs to be emphasized multiple times.

Implementing batch repetition of dynamic content:forLoop tags

In website operation, the more common and more powerful 'batch repeated display' is for dynamic data sets.For example, display the latest 10 articles, show all product categories, list related Tag tags, etc.forLoop tag, used with various data retrieval tags to iterate and display content.

forThe basic structure of the loop is as follows:

{% for item in 集合 %}
    <!-- 在这里编写重复显示的内容,可以使用 item 来访问集合中的每个元素 -->
{% empty %}
    <!-- 如果集合为空,将显示这里的内容 -->
{% endfor %}

Combined with content tags for dynamic repetition display:

  1. Repetition display of article or product lists:archiveListtagsIf you want to repeat a series of articles or products, you can usearchiveListtags to get a data set, then throughforLoop traversal.

    {% archiveList archives with type="list" categoryId="1" limit="5" %}
        {% for item in archives %}
        <div>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>{{item.Description|truncatechars:100}}</p>
            <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
        </div>
        {% empty %}
        <p>当前分类下没有文章。</p>
        {% endfor %}
    {% endarchiveList %}
    

    This code will fetch the latest 5 articles with category ID 1 and repeat their titles, descriptions, and publication times.

  2. Repeat display of category list:categoryListtagsIf you want to display the classification structure of the website, such as repeating all top-level categories or subcategories in the navigation bar or sidebarcategoryListtags will be very useful.

    {% categoryList categories with moduleId="1" parentId="0" %}
        <ul>
            {% for item in categories %}
            <li><a href="{{item.Link}}">{{item.Title}}</a></li>
            {% endfor %}
        </ul>
    {% endcategoryList %}
    

    This will list all top-level categories under the article model (moduleId="1").

  3. Loop counting and conditional judgment:InforIn the loop, you can also useforloop.CounterGet the current loop count (starting from 1), or useforloop.RevcounterGet the number of remaining items. Combineiftags to achieve more complex conditional display.

    {% archiveList archives with type="list" limit="3" %}
        {% for item in archives %}
        <div {% if forloop.Counter == 1 %}class="first-item"{% endif %}>
            第{{ forloop.Counter }}篇文章:{{item.Title}}
        </div>
        {% endfor %}
    {% endarchiveList %}
    

    Here is the first article'sdivaddedclass="first-item".

Combine other features to enhance the repeated display effect

  • Placeholder text:loremtagsWhen there is no actual content but a large amount of text is needed for layout testing,loremLabels can quickly generate random Latin placeholder text. Can be used withrepeatorforcombined with loops.
    
    <p>{% lorem 3 p %}</p> // 生成3段随机段落
    
  • Text formatting: filter combinationsWhen displaying text repeatedly, it is often necessary to format the content. For example, usingtruncatecharsFilter to truncate long descriptions, usingsafeFilter to ensure that the HTML content is rendered correctly, avoiding escaping.
    
    <p>{{item.Description|truncatechars:100|safe}}</p>
    
  • Repeated display of custom fields in the content model:archiveParamstagsIf your content model has customized multiple repeatable fields (such as multiple parameters for products or multiple detail images), you can access these fields viaarchiveParamstags, and thenforLoop through and display.
    
    {% archiveParams params with id=item.Id %}
        {% for param in params %}
        <p>{{param.Name}}: {{param.Value}}</p>
        {% endfor %}
    {% endarchiveParams %}
    

Where to implement these features?

The use of all these template tags and filters will be integrated into the AnQiCMS template files (usually).html文件,存放在/templateUnder the directory)。You can edit the template code online via the 'Template Design' feature of AnQiCMS backend, or upload and modify files directly via FTP/SFTP.design-director.md中所述)和基本约定(如design-convention.md中所述)将帮助您更有效地组织和编写模板代码。

By using flexibilityrepeat过滤器和强大的forLoop tags, you can easily achieve the batch repeated display of text content in AnQiCMS Chinese, whether it is simple static repetition or complex data-driven dynamic lists, you can handle them with ease.


Common Questions (FAQ)

1.repeatFilter can repeat dynamic data, such as an article title?Yes,repeatFilter can repeat dynamic data. Just input the variables you need to repeat as the filter input, for example{{ item.Title | repeat:3 }}It will repeat the article title 3 times.Please note that it will repeat the current value of the variable, rather than traverse a list.forLoop.

2. I want to apply different styles to different entries in a loop, can AnQiCMS do that?Absolutely. Inforthe loop, AnQiCMS provides specialforloopVariable, which containsforloop.Counter(Current loop count, starting from 1),forloop.Revcounter(Remaining items count)、forloop.First(Is it the first loop)、forloop.Last(Is it the last loop) etc. You can combineifLogic judgment label to apply different CSS classes or different HTML structures to specific conditions, for example{% if forloop.First %} <div class="highlight-item"> {% endif %}.

What is the difference between the "Full Station Content Replacement" function and the "Batch Duplicate Display" discussed here?These two functions have an essential difference. "Full Site Content Replacement" is an advanced operation tool in AnQiCMS backend, mainly used formodify and updateThe keywords, links, or other specific text that has been published on the website, which directly changes the content in the database, thereby affecting all pages that reference this content. 'Batch Repeat Display' isTemplate-level display logicIt does not modify the original data of the website, it only controls how existing content or specific text strings are presented on the front-end page (repeatedly displayed).In simple terms, one is 'modify data' in the background, and the other is 'display data' on the front end.