As an experienced website operation expert, I am well-versed in the template mechanism of AnQiCMS.In daily content operation and website optimization work, the details of the template often determine the final user experience and the neatness of the front-end code.Today, we will delve deeply into a seemingly minor yet often questioned symbol in AnQiCMS template creation, the minus sign (-How can it spark with template tags? Is it really suitable for all types of tags?

The core composition of AnQiCMS template syntax

In AnQiCMS, we mainly deal with syntax similar to the Django template engine. The template tags can be roughly divided into three categories:

  1. Variable output tags({{ 变量 }})These tags are used to render backend data directly onto the front-end page, for example{{ archive.Title }}It will display the article title.
  2. Logical control tags ({% 标签名 参数 %})such tags are used to implement logical judgments, loop traversal, file inclusion, and other control flow functions, for example{% if 条件 %}...{% endif %}/{% for item in list %}...{% endfor %}, as well as{% include "partial/header.html" %}They usually appear in pairs, enclosing a block of template code.
  3. comment tags ({# 注释内容 #}or{% comment %}...{% endcomment %}): Used to add comments in the template, this content will be completely ignored during final rendering and will not be output to the front-end page.

Understanding these three types of tags is to explore-The foundation of symbol application boundary.

-The core function of the symbol: precise control of whitespace characters

Especially in the template rendering process, handling{% %}When handling such logical control tags, the template engine often outputs the whitespace characters such as newline and spaces before and after the tags as well.This could lead to a large number of unwanted blank lines in the generated HTML code, making the code bulky and difficult to read, and even affecting the page layout in some strict layouts.

Introduced by AnQiCMS-The symbol is to solve this 'ghostly blank line' problem. When we place-the symbol inside the logical control tag, close to%When a symbol becomes a "trimmer":

  • {%-: It means to remove the tagBeforeAll whitespace characters (including newline characters).
  • -%}: It means to remove the tagAfter thatAll whitespace characters (including newline characters).

The core purpose of this feature isTo optimize the HTML output structure, not to change the variable content. It is like a scalpel in a surgeon's hands, precisely trimming away the redundant 'tissue', making the code look more compact and professional.

Then, which types of tags can handle this-What symbol?

Understood-Understanding the principle of the symbol, we can clearly define its applicable scope in the AnQiCMS template.

1.{% ... %}Logical control tag: main battlefield

Undoubtedly,Logical control tag is-The main stage where symbols really shineThese tags do not produce any visible output themselves, but their presence, especially when they occupy a line alone, is likely to introduce unnecessary blank lines.

For example, aforLoop:

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

Without using-In the case where, if{% for ... %}and{% endfor %}Each tag occupies a line, and it may generate two extra blank lines<li>Outside the list that is rendered.

But when we cleverly introduce-after the symbol:

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

After such processing,{%- for ... -%}it will remove all previous whitespace, including newline characters, and{%- endfor -%}It will remove all whitespace after it. This ensures that the start and end of the loop block do not produce extra blank lines, making the HTML output more compact.

This also applies to other logical control tags, such as:

  • Conditional judgment tag:{%- if condition -%}...{%- elif otherCondition -%}...{%- else -%}...{%- endif -%}
  • File inclusion tag:{%- include "partial/header.html" -%}
  • Macro definition tag:{%- macro my_macro(arg) -%}...{%- endmacro -%}

In this way, we can effectively control the whitespace before and after these logic blocks, avoiding unnecessary line breaks and spaces, making the final HTML code cleaner and neater.

2.{{ ... }}Variable output label: the effect is minimal, usually unnecessary

ForVariable output tag{{ 变量 }}, using-The effect of the symbol is usually negligible, even to the point of being unnecessary.

{{ 变量 }}The role of the label is to directly output the value of the variable. It itself will not introduce additional line breaks like logical control labels due to their block structure. Even if you{{- 变量 -}}This uses it, it merely removes the whitespace around the variable tag itself, and in most cases, these whitespaces have little impact on the final rendered HTML structure.

It is more important, if the variableTitlevalue itself contains leading or trailing spaces (for example" AnQiCMS "){%- Title -%}This cannot help you remove this content. To handle the internal white space of variable values, AnQiCMS provides a more professional tool——Filter (Filters)For example{{ archive.Title|trim }}It can remove the white space at both ends of the variable value.

Therefore, it is not recommended to use this type of tag-symbols to avoid increasing the complexity of the template without any actual benefits.

3. `

` Comment tag: useless

As forComment tag{# ... #}or{% comment %}...{% endcomment %}, using-The symbol is completely meaningless.

The characteristics of comments determine that they will be completely removed in the template parsing phase, producing no front-end output. Since there is no output, there is naturally no issue with introducing whitespace characters.-The symbol loses its value of existence.

Summary and **practice**

AnQiCMS template in-The symbol is an efficient mechanism for dealing with excessive blank characters generated during template rendering, it mainly acts onLogical control tags ({% %}).By adding inside the tag-symbols, we can precisely control the white space output before and after these tags, thereby generating a tighter, clearer HTML code.

It is crucial to understand its working principle: it is a tool for optimizing HTML structure, not for processing variable content.Therefore, when using it, remember that its main purpose is to optimize the HTML structure, rather than change the variable content.For formatting variable content, please consider using the various filters provided by AnQiCMS.

Reasonable use-The symbol can help us write more elegant and efficient AnQiCMS templates, providing a better front-end experience for the website.


Frequently Asked Questions (FAQ)

Q1: Use-Will the symbol significantly improve the performance of my website?

A1:Generally speaking,-The direct improvement to website performance by symbols is negligible. It mainly reduces the blank characters in HTML files, slightly reducing the file size, and theoretically can slightly increase the transmission speed.But this optimization is usually not noticeable, its main value lies in improving the cleanliness and readability of front-end code, making it easier for developers to maintain and debug.

Q2: If I do not use-What will happen to the symbol? Will there be any negative impact?

A2:If I do not use-The symbol has the most direct impact that it may cause extra blank lines or whitespace characters to appear in the rendered HTML file. This