In the template development practice of AnQiCMS, every detail may affect the final page presentation effect and code quality.As an experienced website operation expert, I know that the standardization of template writing is not only related to the beauty of the page, but also closely related to website performance optimization and search engine friendliness.{%- tag %}and{% tag -%}They are the tools for controlling whitespace output in AnQiCMS (based on Django-like template engine).

Template output hidden issue: extraneous whitespace

Firstly, we need to understand why such control is required. During the template rendering process of AnQiCMS, template tags (such as{% if ... %}/{% for ... %})It may leave the trailing whitespace characters (such as newlines, spaces, tabulators, etc.) in the generated HTML code after processing the logic.Imagine if you were in a loop that dynamically generated multiple list items, and each list item might have an extra blank line between them due to the processing of these template tags.

For example, a simple list rendering code may look like this:

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

During actual rendering, since{% for ... %}and{% endfor %}Labels each take up a line, and after processing, there is no explicit removal of the whitespace characters they generate, the generated HTML source code may look like this:

<ul>

    <li>文章标题1</li>

    <li>文章标题2</li>

    <li>文章标题3</li>

</ul>

Although this usually will not affect the final display effect of the page (browsers will automatically ignore most extra whitespace), for developers and operators who pursue ultimate optimization and have a code obsession, this will lead to:

  1. HTML source code redundancy:Added unnecessary characters, causing the HTML file size to slightly increase. In large websites or high-concurrency scenarios, this can accumulate into significant bandwidth consumption and loading time.
  2. Readability reduced:For those who directly view the HTML source code for debugging or SEO analysis, too many blank lines and whitespace characters may interfere with vision and reduce the readability of the code.
  3. Potential layout issues:Even though it is not common, in certain CSS or JavaScript processing scenarios that are sensitive to whitespace, these additional whitespace characters may cause unexpected layout or behavior issues.

Solution: Magic control of whitespace characters ——{%- tag %}and{% tag -%}

AnQiCMS template engine provides a concise and powerful way to solve this problem, that is, in the template tags of%Symbol inside add a hyphen (-). This hyphen can be placed at the beginning of the tag, at the end of the tag, or both.

1.{%- tag %}: Remove all whitespace before the tag

When you are in the template tagfrom the beginningadd a hyphen,{%-the template engine will remove iton the left (before)This includes all whitespace. This includes all newline characters, spaces, and tabs that are located before the tag on the same line.

For example:

<!-- 这是一个注释,标签在这里开始 -->{%- for item in archives %}
    <li>{{ item.Title }}</li>
{% endfor %}

If{%- for ... %}Labels before any newline or space will be removed. This is very useful when it is necessary to tightly connect the template tags with the previous HTML element.

2.{% tag -%}:Remove all whitespace after the tag

Similar to the leading hyphen, when you are in theendadd a hyphen,- %}the template engine will remove itright (after)of all whitespace. This usually refers to the newline character at the end of the line where the tag is located.

Continue the above example:

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

Here are the{% for item in archives -%}It will remove itself and the following newline characters, making<li>the tag follow<ul>the tag, without any blank lines.

3.{%- tag -%}: Full control, remove whitespace around the tag

when you add hyphens at both the beginning and end of a template tag{%- ... -%}it will remove the tag at the same timefrom both the left and rightThe content of all whitespace. This is the most thorough way to control whitespace, usually used in scenarios where it is necessary to completely 'hide' the line where the template tag is located, without producing any whitespace output.

Return to our list example, if we want the output HTML code to have no extra blank lines, we can write it like this:

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

The generated HTML source code will be:

<ul><li>文章标题1</li><li>文章标题2</li><li>文章标题3</li></ul>

It can be seen that the HTML code becomes very compact with no extra blank lines.

Control of the white space for variable output:{{- variable }}and{{ variable -}}

It is worth mentioning that this whitespace control mechanism is not only applicable to template tags, but also to variable output.

  • {{- variable }}: Remove variable outputpreviouslywhitespace.
  • {{ variable -}}: Remove variable outputafterwhitespace.

For example, if you dynamically generate text within a loop and want them to be closely connected:

{% for word in words %}
    <span>{{- word -}}</span>
{% endfor %}

This will ensure each<span>the words within a tag are separated, as well as<span>The label itself and the content before and after it do not have any unnecessary spaces.

Actual application scenarios and benefits

After mastering these whitespace control techniques, the template development of AnQiCMS will become more refined and efficient in English.

  • Optimize HTML output:Reduce page file size, which has a positive impact on improving website loading speed and reducing CDN costs.
  • Improve SEO friendliness:Search engines can usually handle redundant whitespace during page crawling and parsing, but cleaner HTML code is always considered to be structurally better.
  • Avoid front-end conflicts:In some complex CSS layouts (such as flexbox or grid layouts, or certain inline-block elements), unexpected whitespace may cause spacing issues, and precise control can effectively avoid these pitfalls.
  • Code maintainability:Consciously controlling whitespace can also cultivate more rigorous coding habits, making the template code itself more neat and orderly.

In short,{%- tag %}and{% tag -%}It is a feature that seems simple in the AnQiCMS template engine but contains deep optimization logic.They provide developers with unprecedented control over the final HTML output, and are an indispensable tool for building high-performance, high-quality websites.Proficiently apply these skills, and your AnQiCMS website will be able to better serve users and perform exceptionally well in search engines.


Common Questions (FAQ)

Q1: Use{%- tag -%}Do you always**practice**to remove all whitespace characters?A1: It is not always so.Although removing all whitespace can make HTML output the most compact, but sometimes in order to maintain the readability of the HTML source code, especially in complex nested structures or debugging stages, you may want to retain some whitespace.**Practice is used when precise control of the output structure (such as lists, table rows) is required, or when layout problems caused by whitespace are indeed encountered, and in other non-sensitive areas, restrictions can be appropriately relaxed to balance the compactness and readability of the code.

Q2: If I forget to use these whitespace control symbols, will the AnQiCMS website report an error?A2: Usually, the template will not report an error due to this.The browser has a strong error-tolerance in parsing HTML, most of the extra whitespace characters will be ignored.But as mentioned in the article, this may lead to redundant HTML source code, reduce readability, and in very few cases, cause specific front-end layout issues.Therefore, this is not an error, but a state of "unoptimized".

Q3:{{- variable }}and{{ variable -}}Does it have other practical uses besides beautifying the output?A3: In addition to making the output HTML more compact and tidy, the control of whitespace on both sides of the variable output is also very critical in certain specific scenarios.For example, when you need to embed dynamically generated text or values directly into JavaScript code or CSS property values, any unexpected whitespace can lead to syntax errors.By precise control, it can ensure that the variable values are inserted accurately without any potential front-end logic problems.