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:
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.
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").
Loop counting and conditional judgment:In
forIn 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's
divaddedclass="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, using
truncatecharsa 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.