Is the end symbol `{%- endfor %}` in the AnQiCMS template mandatory?

Calendar 👁️ 56

As an experienced website operations expert, I am well aware of the importance of an efficient and flexible content management system (CMS) for a corporate website.AnQiCMS (AnQiCMS) leverages its high-performance architecture based on the Go language and the powerful syntax of the Django template engine, bringing great convenience and infinite possibilities to our content operations.When using AnQiCMS for template development, we often encounter various issues with tag usage, one of which is a common and slightly confusing question about{%- endfor %}What is the meaning of this closing symbol—is it mandatory or does it have a deeper significance?

Today, let's analyze the AnQiCMS template in detail.{%- endfor %}in its true form.

The foundation of AnQi CMS template syntax: block tags appear in pairs

Firstly, we need to clarify a core principle of AnQi CMS template syntax:All block-level tags must be paired and have clear start and end identifiers.This is like building a house, every wall needs a clear starting and ending point to form a complete structure.

According to the AnqiCMS document description, its template creation is "similar to the Django template engine's tag syntax". Under such a design philosophy, there are conditional judgments such as (if), loop control (for)、variable definition(with)、macro(macro)and template inheritance(extends)such tags used to control template logic and structure all require a clear end tag to define their scope.

For example, when making conditional judgments, we will use{% if 条件 %}to start, and{% endif %}to end. Similarly, when iterating over a data list, we will use{% for item in archives %}to start the loop, then the corresponding{% endfor %}This is an indispensable end symbol. ThisendforThe existence of the tag clearly indicates the boundary of the loop code block to the template engine, ensuring the correct parsing and execution of template logic.

Therefore, from a mandatory perspective,endforasforThe end tag of the loop is indeed mandatory; without it, the template parsing error occurs, and the website page cannot be displayed normally.

{%- endfor %}The 'secret': the art of controlling whitespace

SinceendforIs mandatory, then what does the little one before it do?hyphen-What is its role? This is where many developers are easily confused.

The hyphen in the Anqi CMS template engine (-) is not used to force the end of the tag, but forwhitespace control (Whitespace Control). When rendering HTML templates, the tag itself and the surrounding whitespace characters such as newlines and spaces may sometimes be output as actual content in the final HTML.Although these blank characters may not be visually obvious, they may cause unnecessary trouble in scenarios that require strict layout or compact HTML output, such as generating API responses or optimizing file size.

The document of AnQiCMS explicitly points out in the part of “Removing the logical label occupied line”:

  • When you are in the tag'sOn the leftusing hyphen ({%-) it removes the labelleftAll whitespace characters, including newline.
  • When you are in the tag'sRightusing hyphen (-%}) it removes the labelrightAll whitespace characters, including newline.

So,{%- endfor %}The meaning is:ThisforThe end of the loop tagendforIs mandatory, and the hyphen before it-Indicates that the template engine should remove it when rendering this tagendforLeading whitespace characters (for example, the last element in a loop content and{%- endfor %}any newline characters or spaces between them) to produce a more compact HTML output.

Let us understand its function through a simple example:

Without using whitespace control (default):

<div>
    {% for item in archives %}
    <span>{{ item.Title }}</span>
    {% endfor %}
</div>

The rendered HTML might look like this:\nRepresents a newline character:

<div>
    <span>文章标题1</span>
    <span>文章标题2</span>
    <span>文章标题3</span>
</div>

Please note, each<span>may have a newline character after</span>to</div>there may also be a newline character between, depending on the format of your template file.

Use whitespace control:

<div>
    {%- for item in archives -%}
    <span>{{ item.Title }}</span>
    {%- endfor -%}
</div>

The rendered HTML will be more compact:

<div><span>文章标题1</span><span>文章标题2</span><span>文章标题3</span></div>

By{%- for item in archives -%}We have removed:{% for %}The whitespace around the tags; by passing{%- endfor -%}We have removed:{% endfor %}Label spacing. This makes the entire loop area output more compact, without unnecessary line breaks.

Summary

On the whole, in the AnQiCMS template,{%- endfor %}This writing includes two meanings:

  1. endforIt is mandatory in itself: It acts asforThe end tag of the loop, an indispensable part of the template syntax, used to define the scope of the loop.
  2. hyphen-It is optionalIt is a whitespace control character used to remove whitespace characters from both sides of the tag, making the generated HTML more compact.

As an Anqi CMS template developer, we should always rememberendforIt is the essential terminator for loops, and cleverly use hyphens when fine control of HTML output is needed-To optimize the rendering of the template. This not only helps us build a clear and logical template, but also ensures that the website maintains **status** in terms of performance and SEO.


Frequently Asked Questions (FAQ)

1. If I forget to add in the template{% endfor %}What will happen? Answer:If I forget to add{% endfor %},AnQiCMS template engine will report an error during parsing. This usually leads to the page not being rendered, and users will see an error prompt when accessing, because the template engine cannot determineforThe end position and scope of the loop. It is similar to the effect of forgetting to close parentheses or code blocks in programming languages, and it is a syntax error.

2.{%- endfor %}Can the white space control function be used for other block tags in AnQiCMS? For exampleifsentence? Answer:Yes, the white space control function (i.e., using hyphens within the tag-)Can be applied to all block-level tags in the AnQiCMS template, including{% if %}/{% with %}/{% macro %}such as{%- if 条件 -%}Can be removedifThe blank space before and after the conditional tag, while{%- endif -%}then removeendifLabel spacing. This helps to keep the code neat and the output compact in any logic block that requires precise control of HTML output.

3. In what situations should I pay special attention to using{%- endfor %}blank control? Answer:In most cases, when whitespace characters do not affect page layout or functionality (for example, between most block-level elements), you can choose not to use whitespace control. However, in the following scenarios, it is recommended to use{%- endfor %}Performing whitespace control will be very beneficial:

  • Generate compact HTML/XML/JSON output:For scenarios where it is necessary to maximize the compression of file size and reduce network transmission volume, removing unnecessary whitespaces can effectively reduce the file size.
  • Inline element layout:WhenforLoop to generate a series of inline elements (such as<span>/<a>When, the extra newlines or spaces between tags may be interpreted by the browser as actual spaces, which can affect the spacing and layout of elements.Using whitespace control can avoid these unexpected spaces.
  • CSS selector dependency:Some CSS selectors (such as adjacent sibling selectors+)or JavaScript logic may be affected by whitespace nodes between elements. Whitespace control can ensure that the DOM structure is as expected.
  • Code beauty:For some developers who pursue extreme cleanliness, removing redundant whitespace from the rendered HTML is also a code obsession and engineering aesthetics.

Related articles

How to explain the extra blank lines in the page source code after AnQiCMS template rendering?

As an experienced website operations expert, I am well aware that the neatness of the source code not only affects the loading efficiency of the website, but is also a detail that we cannot ignore in daily maintenance and SEO optimization.When using AnQiCMS such an efficient and flexible content management system, some careful operators may find many unnecessary blank lines in the page source code, which may seem perplexing at first glance.Today, let's delve into the cause of the extra blank lines appearing after the AnQiCMS template rendering and discuss how to solve this problem elegantly.

2025-11-07

In AnQiCMS template, does the `{% comment %}` tag itself produce a blank line?

## Does the `{% comment %}` tag in AnQiCMS templates produce blank lines?—— Deep Analysis and Optimization Practice As an experienced website operation expert, I know that every detail in the template development process of AnQiCMS (AnQiCMS) may affect the final page display effect and user experience.

2025-11-07

Remove AnQiCMS logic tag line will affect the template parsing speed?

## Deep Dive: Does removing logical tag lines in AnQiCMS templates really improve parsing speed?As an experienced website operations expert, I am well aware of the importance of performance for a website.In AnQiCMS such an enterprise-level content management system based on Go language, pursuing efficiency and flexibility, every detail may cause the operator to consider performance.Today, let's delve into a frequently discussed issue: In the AnQiCMS template, logical tags (such as `{% if %}`) can be removed through special syntax

2025-11-07

In AnQiCMS template, how to effectively manage whitespace in nested `for` loops?

In website operation, efficient and tidy template code is one of the keys to improving user experience and page loading speed.AnQiCMS is an enterprise-level content management system developed based on the Go language, committed to providing a high-performance, scalable solution, naturally also considering the code tidiness for template developers.Especially in the common scenario of dealing with nested `for` loops, excessive whitespace in templates often becomes a pesky problem.### Explore the white space issue in nested `for` loops AnQiCMS

2025-11-07

In the AnQiCMS template, will the `{% set %}` tag produce a blank line?

As an experienced website operations expert, I am well aware that every detail in website content management and template development can affect the ultimate user experience and website performance.During the template development process of AnQiCMS (AnQiCMS), a frequently overlooked place that may cause minor problems is the blank line issue caused by various template tags.Today, let's deeply analyze whether the `{% set %}` tag in AnQiCMS templates will produce blank lines, and how we can elegantly handle it.### Deep Analysis

2025-11-07

How to keep the output of AnQiCMS templates consistent and avoid interference from whitespace?

## Ghosting Space Goodbye: AnQiCMS Implementation Guide for Template Output Consistency As an experienced website operations expert, I know that the neatness and consistency of the website front-end pages are crucial for user experience and search engine optimization.AnQiCMS, with its high efficiency and flexibility, has become a powerful assistant for many small and medium-sized enterprises and content operation teams.However, in the process of daily template development, some 'ghost spaces' - those invisible line breaks and spaces, often affect the layout and code beauty of the page without being noticed, and even cause some minor troubles in front-end rendering

2025-11-07

Does the `filters` filter of the AnQiCMS template affect the control of whitespace?

As an experienced website operations expert, I am deeply familiar with the various functions and content operation strategies of AnQiCMS (AnQiCMS). I am willing to delve into the discussion of whether the `filters` filter of the AnQiCMS template is affected by whitespace control?This topic. In content operations, meticulous template control, including the handling of whitespace, is crucial for generating clean and efficient HTML code and optimizing page loading speed.--- ## AnQiCMS template's `filters`

2025-11-07

How is the blank control effect of `{{- obj|filter -}}` in the AnQiCMS template?

## AnQiCMS template's blank control: `{{- obj|filter -}}` How to improve your website experience?As an experienced website operations expert, I know that every detail can affect the final presentation and user experience of the website.In AnQiCMS, such an efficient and flexible Go language content management system, fine control of templates is the key to achieving these goals.

2025-11-07