In AnQiCMS template, what is the potential relationship between `forloop.Counter` and the blank control?

Calendar 👁️ 62

AnQiCMS template inforloop.CounterWith whitespace control: refine your list output

As an experienced website operations expert, I know that the flexibility of templates and the neatness of output in a content management system are crucial for the long-term maintenance of the website and search engine optimization (SEO).AnQiCMS with its efficient architecture based on the Go language and support for Django-like template engine syntax, provides us with powerful content display capabilities.Today, let's delve into a seemingly minor but significant skill in the AnQiCMS template that can significantly improve the quality of the template:forloop.CounterThe potential association between whitespace control.

In daily content presentation, we often need to iterate through list data, such as article lists, product lists, or navigation menus.At this time, we may need to add numbers, special styles to each list item, or just make sure the generated HTML structure is compact and without redundancy.forloop.CounterAnd whitespace control ({%-and-%}) is the powerful tool for dealing with these scenarios.

forloop.Counter: Smart counter in the loop

AnQiCMS template inforLoops can not only help us iterate over data sets but also come with a very practical variable:forloop.Counter. It will automatically increment in each loop, providing a serial number starting from 1. It is also complemented byforloop.RevcounterIt provides a countdown starting from the total number of items in the collection.

For example, when we want to generate a numbered list for a series of articles,forloop.Counterit can be very useful:

{% archiveList archives with type="list" limit="5" %}
<ul>
    {% for item in archives %}
    <li>
        <span>{{ forloop.Counter }}.</span>
        <a href="{{ item.Link }}">{{ item.Title }}</a>
    </li>
    {% endfor %}
</ul>
{% endarchiveList %}

This counter is very flexible, we can use it to achieve various display effects, such as:

  • Style ControlAdd a special CSS class to the first or last list item, such as:{% if forloop.Counter == 1 %}active{% endif %}.
  • Conditional rendering: Display different content or logic according to the loop count.
  • Progress display: Show the current step in some step-by-step tutorials or progress bars.

Blank control: Optimize your HTML structure

However, template engines often introduce extra blank lines or whitespace characters when parsing tags.This may not be obvious visually, but if we check the page source code, we will find a large number of blank lines.Although modern browsers can handle these extra spaces well, a bloated HTML structure may lead to:

  1. Code readability decreases: Especially for complex templates, additional blank lines make debugging and understanding the source code difficult.
  2. The file size has slightly increased: Although the impact is small, reducing every byte is meaningful for websites that pursue ultimate optimization.
  3. Potential impact on some tools or scriptsIn rare cases, some libraries or parsers that heavily depend on HTML structure and whitespace may be affected.

The AnQiCMS template provides a powerful blank control mechanism to solve this problem. By adding a hyphen in the template tag's{or}afterwards(-We can tell the template engine to remove the whitespace characters generated by this tag:

  • {%- tag %}Remove all whitespace (including newline characters) before this tag:
  • {% tag -%}Remove all whitespace after the tag (including newline characters).
  • {{- variable }}Remove all whitespace before the variable output.
  • {{ variable -}}Remove all whitespace after the variable output.

For example, in most cases, oneforLoops may generate some extra blank lines:

正常下
{% for item in archives %}
{{ item.Id }}
{% endfor %}

The generated HTML may look like this:


1

2

3

And after using whitespace control:

紧凑:
{%- for item in archives %}
{{- item.Id }}
{%- endfor %}

Or even more succinctly, only controlling the tag itself:

不带换行
{% for item in archives -%}
{{ item.Id }}
{%- endfor %}

These concise writing styles can make the final HTML cleaner, for example:

1
2
3

forloop.CounterSynergy with white space control

Now, we willforloop.CounterCombine with whitespace control and imagine you are usingforLoop through a list of articles and add a serial number to each article item, while ensuring that the generated HTML source code is clean and tidy with no extra blank lines.

If used simplyforloop.Counterand includes within the loopifJudging or other tags that may produce blank spaces can cause unnecessary blank lines in the final HTML output. For example, if you want to add a special style to even items in a list:

{% archiveList archives with type="list" limit="5" %}
<ul>
    {% for item in archives %}
    <li>
        {{ forloop.Counter }}.
        <a href="{{ item.Link }}">{{ item.Title }}</a>
        {% if forloop.Counter % 2 == 0 %}
            <span class="even-item">(偶数项)</span>
        {% endif %}
    </li>
    {% endfor %}
</ul>
{% endarchiveList %}

This code is in{% if ... %}Blank lines may occur before and after the tag, causing each<li>or within the tag<li>Excessive line breaks between tags.

To solve this problem, we can cleverly combine whitespace control:

{% archiveList archives with type="list" limit="5" %}
<ul>
    {%- for item in archives -%}
    <li>
        {{- forloop.Counter -}}.
        <a href="{{- item.Link -}}">{{- item.Title -}}</a>{%-
        if forloop.Counter % 2 == 0 -%}
            <span class="even-item">(偶数项)</span>{%-
        endif -%}
    </li>
    {%- endfor -%}
</ul>
{% endarchiveList %}

Add around the loop label, variable output label and conditional judgment label of{or}Add around the loop label, variable output label and conditional judgment label of-We enforce the template engine to remove the whitespace generated by these tags. This makes the generated HTML source code as compact as possible, each<li>The content inside the tag should be tightly packed, avoiding unnecessary line breaks, thus achieving a finer and more optimized list output.

This fine control not only makes the rendered HTML logically clear, but also maintains a very high level of neatness in the source code, which is undoubtedly a great boon for the maintenance and debugging of large projects.

Consideration in practice and **practice

In AnQiCMS template development, masterforloop.CounterThe combination with whitespace control allows us to build more efficient and maintainable templates. Although whitespace control mainly affects the neatness of HTML source code

Related articles

AnQiCMS template, what are the usage scenarios of `{%- elif -%}`?

As an experienced website operations expert, I am well aware of the importance of the flexibility of content management system templates for website operations efficiency and user experience in my daily work.AnQiCMS (AnQiCMS) provides us with great convenience with its efficient architecture based on the Go language and Django-like template engine syntax.During the template creation process, understanding and skillfully using various tags is the foundation, and logical judgment tags like `{%- elif -%}` are crucial for implementing dynamic content display and enhancing user experience.

2025-11-07

Does the `lorem` tag of the AnQiCMS template generate text with additional blank lines?

In AnQiCMS (AnQiCMS) template development, we often use various tags to quickly build pages, among which the `lorem` tag is favored by front-end developers for its ability to generate random placeholder text.However, whether the text generated by the `lorem` tag includes additional blank lines is indeed a question worth delving into, especially for operators who strive for pixel-perfect and tidy code.

2025-11-07

In AnQiCMS template, how do `{%- block %}` and `{% block %}` affect the rendering result?

As an experienced website operations expert, I know that every detail can affect the ultimate user experience and website performance in my practice at AnQiCMS.Today, let's delve into two seemingly similar yet subtly different tags in the AnQiCMS template: `{%- block %}` and `{% block %}`, and see how they affect the rendering of our website.

2025-11-07

How to quickly find and optimize redundant white spaces in AnQiCMS templates?

As a senior website operations expert, I know that every byte of optimization can affect the overall performance of the website.In AnQiCMS such an efficient content management system, the simplification and optimization of templates is a key factor in improving website performance and enhancing user experience.Today, let's delve into a detail that is often overlooked but is of great importance - how to quickly find and optimize the redundant blank spaces that exist quietly in the AnQiCMS template. ### AnQiCMS template redundant blank spaces: Why should we pay attention to it?

2025-11-07

How to prevent whitespace issues during the development stage of AnQiCMS templates?

How to elegantly prevent whitespace issues in AnQiCMS template development?In the era of refined website operation, every detail may affect user experience and search engine performance.For users and developers of AnQiCMS (AnQi CMS) who pursue efficiency, security, and customization, the quality of the HTML code output by the template is crucial.AnQi CMS, this enterprise-level content management system based on Go language has won the favor of many users with its powerful functions and flexible template engine.

2025-11-07

In AnQi CMS, how does the `stampToDate` tag convert a 10-digit timestamp to a common date format?

## Time Magician: How to easily handle 10-digit timestamps in AnQi CMS `stampToDate` tag In the world of content operation, the way time is presented often directly affects users' understanding and perception of the content.A clear and readable date format that allows users to quickly obtain key information while browsing articles, products, or comments, enhancing the overall browsing experience.However, at the bottom level of databases and systems, time is often represented by a series of 'mysterious' numbers - a 10-digit timestamp (Unix timestamp).

2025-11-07

How to use the `stampToDate` tag to display the publication date of each document on the article list page?

## Enabling content timeliness: The efficient way to display publication dates on the article list page of AnQi CMS In today's fast-paced digital world, the timeliness of content is crucial for the attractiveness of a website and the user experience.Whether it is news reports, technical articles, or product updates, clearly displaying the publication date can help readers quickly judge the value of the information and enhance the professionalism of the website.

2025-11-07

How to format `CreatedTime` obtained from the `archiveDetail` tag using `stampToDate` to the "YYYY-MM-DD" format?

## Unlock the AnQi CMS Time Magic: How to beautifully format `CreatedTime` to "YYYY-MM-DD" As an experienced website operations expert, I am well aware of the importance of content timeliness and display methods for user experience.In AnQiCMS (AnQiCMS) such a powerful content management system based on Go language, we can not only efficiently manage content, but also convert technical information into user-friendly display through flexible template tags.Today, let's talk about a common demand in operation

2025-11-07