How to implement the batch repetition display function of text in AnQiCMS?

Calendar 👁️ 89

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

Understanding the need for text repeat display

Text repetition can be divided into two main cases: one is simply repeating a fixed text or variable N times;Another approach is to iterate over a dataset (such as a list of articles or product categories) and display each entry, forming dynamic repetitive content.AnQiCMS's template system can well meet these two needs.

Implement fixed text batch repetition display:repeatFilter

If you need to simply repeat a static text or the value of a variable a specified number of times, in 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 repeat the concatenation according to the number you set.

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

For example, if we want to display the word 'AnqiCMS' 5 times on the page, we can write it directly in the template like this:

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

This code's output will be: Anqi CMS Anqi CMS Anqi CMS Anqi CMS Anqi CMS.

Actual application scenarios:

  • Visual separator or decorative pattern:For example, repeated display---/***or#To create a simple visual separation effect.
  • 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 segment.
  • Repeated short dynamic data:If the value of a variable itself needs to be displayed repeatedly, for example, to emphasize a characteristic of a product multiple times.

Implementing the batch display of dynamic content:forLoop tags

In website operation, the more common and powerful 'batch repeat display' is for dynamic data sets.For example, display the latest 10 articles, show all product categories, list related Tag tags, etc.AnQiCMS uses a syntax similar to Django template engine, its core beingforLoop tag, used in conjunction with various data acquisition tags to iterate and display content.

forThe basic structure of the loop is as follows:

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

Combine the content tag for dynamic repeated display:

  1. Repeat the list of articles or products:archiveListTagIf you want to repeat a series of articles or products, you can usearchiveListtags to get the data set, and then go throughforlooped through.

    {% 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 retrieve the latest 5 articles with category ID 1 and repeat their titles, descriptions, and publication times.

  2. Repeat display of category list:categoryListTagIf you want to display the classification structure of the website, such as repeating all top-level categories or subcategories in the navigation bar or sidebar,categoryListtags 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 the 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 remaining item count. 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".

Enhance repeated display effect with other features

  • Placeholder text:loremTagWhen there is no actual content but a large amount of text is needed for layout testing,loremTags can quickly generate random Latin placeholder text. It can be used withrepeatorforlooping.
    
    <p>{% lorem 3 p %}</p> // 生成3段随机段落
    
  • Text formatting: Filter combinationWhen displaying text repeatedly, it is often necessary to format the content. For example, usingtruncatecharsa filter to truncate long descriptions, usingsafeA filter to ensure that HTML content is rendered correctly, avoiding escaping.
    
    <p>{{item.Description|truncatechars:100|safe}}</p>
    
  • Repeat display of custom fields in the content model:archiveParamsTagIf your content model customizes multiple repeatable fields (such as products with multiple parameters or multiple detail images), you can access these fields byarchiveParamsthen label them, andforDisplay in loop.
    
    {% 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.htmlThe file is stored in/templateUnder the directory). You can edit the template code online through the "Template Design" feature of the AnQiCMS backend, or directly upload and modify files via FTP/SFTP.Understand the directory structure of the template file (such asdesign-director.mdAs described and basic conventions (such asdesign-convention.mdAs described will help you organize and write template code more effectively.

By flexible applicationrepeatFilters and powerfulforLoop tag, you can easily implement batch repetition of text content in AnQiCMS, whether it is simple static repetition or complex data-driven dynamic lists, you can do it with ease.


Frequently Asked Questions (FAQ)

1.repeatCan the filter repeatly display dynamic data, such as an article title?Yes,repeatThe filter can repeatly display dynamic data. Just input the variables that need to be repeated as the filter input, for example{{ item.Title | repeat:3 }}It will repeat the article title 3 times. But please note that it will repeat the current value of the variable, not iterate through a list.If you need to traverse the list and display the title of each element, you still need to useforLoop.

2. I want to apply different styles to different entries in a loop, can AnQiCMS do that?Absolutely. Inforthe loop, AnQiCMS provides specialforloopVariables that containforloop.Counter(Current loop count, starting from 1),forloop.Revcounter(Number of remaining items),forloop.First(Whether it is the first loop),forloop.Last(Whether it is the last loop) and other properties. You can combineifLogical judgment tags to apply different CSS classes or output different HTML structures for specific conditions, for example{% if forloop.First %} <div class="highlight-item"> {% endif %}.

What is the difference between the "full site content replacement" function and the "batch repeated display" discussed here?These two functions have an essential difference. "Full Site Content Replacement" is an advanced operation tool in the AnQiCMS backend, mainly used formodify and updateKeywords, links, or other specific text that has been published on the website, which directly changes the content of the database and affects all pages referencing this content. 'Batch repeat display' isTemplate level display logicIt does not modify the original data of the website, it only controls how the existing content or specific text strings are presented on the front-end page.In simple terms, one is 'modify data' behind the scenes, and the other is 'display data' on the front end.

Related articles

How to repeat the same string a specified number of times in AnQiCMS templates?

In website design and content display, we often encounter scenarios where we need to repeat a string.Whether as a separator, decorative element, or placeholder for generating specific patterns, such needs are very common.AnQiCMS with its flexible template engine makes such operations extremely simple.This article will introduce you to how to use the built-in Filter function in the AnQiCMS template to easily repeat the same string.

2025-11-08

What Markdown syntax standards and extensions does the AnQiCMS Markdown editor support?

The AnQiCMS Markdown editor brings great convenience and powerful expressiveness to content creation.It is not just a text input box, but also an intelligent tool that can structure and visualize complex information, aiming to help users build high-quality content more efficiently and intuitively.On the basis of Markdown syntax support, AnQiCMS follows the mainstream standards and provides comprehensive functions, allowing users to focus on the content itself without paying too much attention to layout details.You can easily use the hash `#` to create headings of different levels

2025-11-08

How to render the custom field `introduction` in Markdown content to HTML?

In AnQiCMS, custom fields provide us with great flexibility, allowing us to expand the content structure according to actual business needs.For example, you may have set up a custom field named `introduction` for articles or products and want to write a Markdown-style introduction in this field.How can you render Markdown content correctly on the website front-end so that it presents rich formatting on the page?This article will introduce how to easily achieve this goal.### Understand

2025-11-08

What is the purpose of the `escapejs` filter in handling JavaScript code snippets in Markdown content?

In the daily operation of Anqi CMS, we often use the Markdown editor to enrich content display, which allows us to conveniently format articles, insert code examples, and even mathematical formulas and flowcharts.However, this convenience is also accompanied by potential security risks, especially when handling Markdown content submitted by users and dynamically embedding it into JavaScript code snippets on web pages.At this moment, understanding and correctly using the `escapejs` filter is particularly important, as it acts like an invisible barrier, silently guarding the security of our website.

2025-11-08

What is the practice of using the AnQiCMS `repeat` filter to repeat the output of a string?

In AnQiCMS template development, flexibly using various filters is the key to improving content display efficiency and code conciseness.Among them, the `repeat` filter, with its intuitive function of repeating outputting strings, can play an unexpected role in certain scenarios.It may seem simple, but if we master its**practical application, it will help us build website interfaces more elegantly.

2025-11-08

Does the `repeat` filter in the AnQiCMS template throw an error when processing non-string input?

In Anqi CMS template development, the `repeat` filter is a concise and practical tool that allows us to quickly replicate a string multiple times.But many developers may be curious, if the filter is passed a non-string type of data, such as a number, boolean, or even more complex objects, will it report an error?This is the issue we will discuss in detail today, aimed at helping everyone use the `repeat` filter more clearly and safely.

2025-11-08

How to dynamically adjust the number of repeated string outputs in AnQiCMS?

In AnQiCMS, we often encounter situations where we need to repeat a string, such as displaying product star ratings on product detail pages, or creating separators with special symbols in articles.Manually copying and pasting is not efficient and it becomes very麻烦 when you need to modify 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 know the `repeat` filter: the core of string repetition output AnQiCMS template system

2025-11-08

How to add two numbers or concatenate strings in AnQiCMS templates?

In AnQiCMS template design, dynamically handling data is a key element in building a feature-rich website.Whether it is to calculate the total price of goods or to concatenate user-friendly prompt information, the template engine provides a concise and efficient way to complete these tasks.This article will delve into how to implement the addition of numbers and the concatenation of strings in the AnQiCMS template, helping you better utilize the template features.

2025-11-08