As a senior website operations expert, I fully understand the importance of an efficient and flexible content management system (CMS) for a corporate website.The AnQiCMS (AnQiCMS) brings great convenience and endless possibilities to our content operation with its high-performance architecture based on the Go language and the powerful syntax of the Django template engine.{%- endfor %}Such an ending symbol——is it mandatory, or does it have a deeper meaning?

Today, let's delve into the AnQiCMS template{%- 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 (Block-level) tags must appear in pairs and have clear start and end identifiers.This is like building a house, every wall needs to have a clear starting and ending point to form a complete structure.

According to the document description of AnQi CMS, its template creation is "similar to Django template engine tags". Under such a design philosophy, conditional judgments such asif)、loop control(for)、variable definition(with)、macro(macro)and template inheritance(extends)etc. such tags used for controlling template logic and structure, all require a clear end tag to define their scope.

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

Therefore, from a mandatory perspective,endforasforThe end tag of the loop is indeed mandatory, not adding it will cause template parsing errors and the website page will not display normally.

{%- endfor %}The 'secret' in it: the art of white space control.

SinceendforIt is mandatory, then what is the role of the small one in front of it?hyphen-What is the role? This is where many developers are likely to misunderstand.

The hyphen in the Aanqi CMS template engine is-)Not used to force the end of the tag, but to performWhitespace Control.When rendering HTML in templates, the tag itself and the surrounding newline characters, spaces, and other whitespace characters may sometimes be output as actual content in the final HTML.Although these whitespace characters may not be visually noticeable, they may cause unnecessary trouble in scenarios where layout requirements are strict or compact HTML output is needed (such as generating API responses, optimizing file size).

The document of Anqi CMS explicitly states in the section of 'Removing logical tag line occupation':

  • When you are in the tagon the leftUsing hyphen({%-), it removes the labelLeftall whitespace characters, including newline characters.
  • When you are in the tagOn the right sideUsing hyphen(-%}), it removes the labelRightall whitespace characters, including newline characters.

So,{%- endfor %}The meaning is:)ThisforThe end of the loop tagendforis mandatory, and the hyphen preceding it-indicates that the template engine should removeendforLabel left all whitespace characters (such as, the last element of the loop body and{%- endfor %}any newline characters or spaces that may exist between them), to produce a more compact HTML output.

Let us understand its function with a simple example:

Without using white space 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 that each<span>there may be a newline character at the end, and</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>

Pass{%- for item in archives -%}We have removed{% for %}whitespace before and after the tag; through{%- endfor -%}We have removed{% endfor %}Label before and after spaces. This makes the entire loop area output more compact, without unnecessary line breaks.

Summary

Overall, the AnQiCMS template{%- endfor %}This style includes two meanings:

  1. endforIt is mandatory.: It acts asforThe end tag of the loop, an indispensable part of template syntax, used to define the scope of the loop.
  2. hyphen-It is optional.It is a blank control symbol used to remove whitespace characters on both sides of the label, making the generated HTML more compact.

As template developers for the AnQi CMS, we should always keep in mindendforIt is a necessary terminator for loops, and it is巧妙地运用连字符巧妙地运用连字符 when we need to finely control HTML output-This will optimize the rendering results of the template. Not only can it help us build templates with clear structure and rigorous logic, but it can also ensure that the website maintains **status** in terms of performance and SEO.


Common Questions (FAQ)

1. If I forget to add in the template{% endfor %}What will happen? Answer:If I forget to add{% endfor %}AnQiCMS's template engine will report an error during parsing. This usually leads to the page not being rendered, and the user will see an error prompt when accessing it, 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 statement blocks in programming languages, which is a syntax error.

2.{%- endfor %}Can the blank control feature be used for other block tags in AnQiCMS? For example,if? Answer:Yes, the blank control feature (i.e., using hyphens inside the tag)-This can be applied to all block-level tags in the AnQiCMS template, including{% if %}/{% with %}/{% macro %}such as.{%- if 条件 -%}can be removedifThe blank before and after the conditional tags,{%- endif -%}should be removed.endifThe spaces before and after the label. This helps to keep the code clean and the output compact in any logical 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, using{%- endfor %}Performing blank control will be very beneficial:

  • Generate compact HTML/XML/JSON output:For scenarios that require maximizing the compression of file size and reducing network transmission volume, removing unnecessary whitespace can effectively reduce the file size.
  • Inline element layout:WhenforLoop to generate a series of inline elements (such as<span>/<a>)When additional line breaks or spaces are present between tags, the browser may interpret them as actual spaces, which can affect the spacing and layout of elements.Using whitespace control can avoid these unexpected spacing.
  • CSS Selector Dependency:Certain 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 more in line with expectations.
  • Code aesthetics:For developers who strive for extreme cleanliness, removing redundant whitespace from the rendered HTML is also a code洁癖 and engineering aesthetics.