Use AnQiCMS template easily to get the 'first' and 'Nth' indices in the loop
In website content operation, we often need to display a series of contents on the page, such as article lists, product displays, navigation menus, etc.In order to better present this content, we often need to finely control each item in the list, such as adding special styles to the first item, or displaying the item's number in the list.For friends using AnQiCMS, understanding how to use templates inforLooping to get the current iteration item index is a very practical skill that can greatly enhance the flexibility of the template.
AnQiCMS uses a template engine syntax similar to Django, which makes it very convenient for content developers to build dynamic pages. When dealing with list data, we usually useforThe loop tag is used to iterate over a data collection. To get the index of each element in the loop, AnQiCMS provides a built-in special variable to make this requirement easy.
Get to knowforloop.CounterYour 1-based loop counter:
When you use in AnQiCMS template:{% for item in collection %}such a structure to iterate over a list (such as an article collection, category list, or image group) when the template engine quietly provides a special variable namedforloopthis special variable.forloopThe variable contains a lot of useful information related to the current loop state, and the most core of which is today's main character -forloop.Counter.
forloop.CounterAs the name implies, it is a counter. Its uniqueness lies in the fact that it starts counting from1This means that when the loop runs for the first time,forloop.CounterThe value is 1; the second is 2, and so on until the loop ends. This is very convenient for scenarios where you need to number list items starting from 1.
Let's look at a simple example, suppose we are displaying a product list and we want to show the serial number for each product:
{% archiveList products with type="list" limit="5" %}
{% for product in products %}
<div class="product-item">
<span class="product-number">{{ forloop.Counter }}.</span>
<h3><a href="{{ product.Link }}">{{ product.Title }}</a></h3>
<p>{{ product.Description }}</p>
</div>
{% endfor %}
{% endarchiveList %}
In this code block,{{ forloop.Counter }}Will display "1." for the first product in the list, "2." for the second product, and so on.This intuitive 1-based counting method perfectly fits human reading habits and many business logic needs.
Masterforloop.Revcounter: Counting backwards from the 'tail' index
In addition to the positive counting, AnQiCMS also provides us withforloop.Revcounterthis variable. Andforloop.CounterOn the contrary,forloop.Revcounterwhat it provides isa reverse count, starting from 1 as wellthe index. This means that the last item in the list, itsforloop.Revcountervalue is 1; the second-to-last is 2, and so on until the first item in the list, itsforloop.Revcountervalue equals the total length of the list.
forloop.RevcounterIt can be very effective in certain specific scenarios, such as when you need to handle the last few items in the list specially, or display the sequence numbers in reverse order.
Combine these two counters to build more complex logic:
{% categoryList categories with moduleId="1" parentId="0" %}
{% for category in categories %}
<li class="category-item {% if forloop.Counter == 1 %}first{% endif %} {% if forloop.Revcounter == 1 %}last{% endif %}">
<span class="index">{{ forloop.Counter }}</span>.
<a href="{{ category.Link }}">{{ category.Title }}</a>
<span class="remaining-items">(还有{{ forloop.Revcounter - 1 }}个)</span>
</li>
{% empty %}
<li>暂无分类。</li>
{% endfor %}
{% endcategoryList %}
In this example, we not only added positive numbering to each category but also utilizedforloop.Counter == 1Added to the first elementfirstClass, utilizingforloop.Revcounter == 1Added to the last elementlastClass. At the same time,forloop.Revcounter - 1Then cleverly displays how many elements are behind the current item.
Practical scenarios and techniques
forloop.Counterandforloop.RevcounterThese variables are very useful in actual development:
- Number display: The most common use is to add numbers to list items, such as “1. Title A”, “2. Title B”, and so on.
- condition style: you can according to
forloop.Counterorforloop.RevcounterThe value to add different CSS classes to elements at specific positions, such as the first, last, or every N elements to add special styles. This istag-/anqiapi-other/3498.htmlandtag-/anqiapi-archive/141.htmlFor example, it is also reflected in the example{% if forloop.Counter == 1 %}active{% endif %}. - Unique ID generation: Combined with other variables, it can generate a unique ID on the page, convenient for JavaScript operations or anchor links.
- Loop exit/limit: Although
forloop.CounterIt does not directly control the loop, but you can combineifstatements, to perform special processing or interrupt the logic when a certain count is reached. - Handling empty data: It is worth mentioning that AnQiCMS's
forLoops also support{% empty %}tag, when the collection being traversed is empty, it will executeemptythe content in the block, at this timeforloopthe variable naturally does not exist.
By flexible applicationforloop.Counterandforloop.Revcounter,AnQiCMS content operators and template developers can easily implement various complex list displays and interactive logic, making the website content more vivid and organized.
Frequently Asked Questions (FAQ)
Q1: AnQiCMS'forthe loop,forloop.CounterIs the provided index starting from 0 or 1?
A1: AnQiCMS'forloop.Counterwhat it provides isCounting starts from 1index. This means that the first element of the list isforloop.CounterThe value is 1, the second is 2, and so on. If you need a 0-based index, you can simply use{{ forloop.Counter - 1 }}to achieve.
Q2: Can I get the reverse index of the list item besides the positive index?
A2: Of course you can. AnQiCMS also providesforin the loop.forloop.RevcounterA variable that can provide you with the reverse index of the current iteration item in the list, which also starts from 1. For example, the last element in the list isforloop.Revcounterthe value is 1, the second last is 2.
Q3: How do I apply special styles to the first or last item in a list?
A3: You can useforloop.Counterandforloop.RevcounterCombineifConditional judgment can be implemented. For example, to add style to the first item, you can use{% if forloop.Counter == 1 %}; To add style to the last item, you can use{% if forloop.Revcounter == 1 %}This can very accurately control the performance of specific position elements.