As an experienced website operations expert, I know that every byte of optimization can affect the overall performance of the website.In such an efficient content management system as AnQiCMS, the simplification and optimization of templates is a key factor in improving website performance and enhancing user experience.Today, let's delve into an often overlooked yet crucial detail - how to quickly find and optimize the redundant blank spaces that exist silently in the AnQiCMS template.

Why do the redundant blanks in AnQiCMS templates deserve our attention?

When a website generates HTML pages, various logical tags in the template (such as conditional judgments{% if %}and loops{% for %}The) as well as the lines they occupy, often leave extra newlines and spaces in the final output HTML code.These seemingly harmless whitespace characters, accumulated over time, will increase the file size of the page and prolong the loading time.Even though modern browsers have strong error-tolerant parsing for HTML, redundant whitespace does not cause page errors, but from the perspective of website performance optimization, they are still 'garbage' that needs to be cleaned up.

Imagine a page that reduces file size by dozens or even hundreds of KB, accumulating little by little. For websites with high traffic, this means saving server bandwidth, faster page response speed, and a more friendly search engine crawling experience.For a system like AnQiCMS that pursues 'lightweight' and 'efficient', the simplification of template output should be a reflection of its philosophy.

AnQiCMS's secret weapon:{%-with-%}Control character

AnQiCMS template engine took full consideration of the control of template output. It provides a concise and powerful mechanism to handle redundant white spaces, that is, using special hyphens at the boundaries of logical tags{%-and-%}.

This-Characters, like a small 'eraser', can intelligently remove the white space at the boundary of the area it acts on. Specifically:

  1. {%- 标签名:For example, when you are at the start of a logical tag,{%- if 条件 %}plus this one.-When the symbol is used, the template engine will remove{%all preceding whitespace characters (including newline and space).
  2. 标签名 -%}:when you are at the end of a logical tag, for example{% endif -%}plus this one.-When the symbol is used, the template engine will remove-%}all trailing whitespace characters.

By skillfully using these two control characters, we can precisely control the white space generated during template rendering, making the final HTML code as compact as possible.

For example, suppose you have a simple conditional judgment, without using whitespace control characters, you might generate such HTML:

<!-- 模板代码 -->
<div>
    {% if user.is_admin %}
        <p>欢迎,管理员!</p>
    {% endif %}
</div>

<!-- 可能生成的 HTML -->
<div>

        <p>欢迎,管理员!</p>

</div>

And when we add whitespace control characters, the effect will be very different:

<!-- 模板代码 -->
<div>
    {%- if user.is_admin -%}
        <p>欢迎,管理员!</p>
    {%- endif -%}
</div>

<!-- 精简后的 HTML -->
<div><p>欢迎,管理员!</p></div>

You can see, by simply adding-We successfully eliminated all redundant whitespaces brought by the logical tags themselves, making the HTML output more compact.

Practice makes perfect: Where to use-the most effective?

Although-Control characters are powerful, but not every place needs to add them blindly. Overuse may make template code look cluttered, and even in some cases where whitespace needs to be preserved, it may produce unexpected results (such as<pre>Code within the tags). Therefore, we should focus on the following common scenarios:

  1. Logic judgment block ({% if %}/{% else %}/{% endif %}):This is the most common place for redundant blank spaces. Inif/elif/else/endifSurrounding the tags with-It can effectively clean up the line breaks it introduces.

    {%- if condition -%}
        <!-- 内容 -->
    {%- else -%}
        <!-- 备用内容 -->
    {%- endif -%}
    
  2. Loop structure ({% for %}/{% endfor %}):Loop tags are also prone to generate a large amount of whitespace, especially when generating lists, menus, and other structures.

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

    This can ensure<li>There should be no extra line breaks between tags.

  3. Template reference ({% include %}/{% extends %}/{% macro %}):When including other template files or macros, if there are blank lines above or below these tags, you can also use-to remove.

    {%- include "partial/header.html" -%}
    <!-- 页面主体内容 -->
    {%- include "partial/footer.html" -%}
    
  4. Comment tag ({# #}/{% comment %}/{% endcomment %}):Template comments themselves are not output to HTML, but the lines they occupy and the surrounding whitespace may have an impact. AnQiCMS of{# #}Single-line comments do not produce output, but{% comment %}Block comments, the line where they are located may produce blank spaces, at this time you can also consider using-.

    {%- comment -%}
    这是一个多行注释,内容不会显示,但使用控制符可以消除其引入的空白。
    {%- endcomment -%}
    

It is worth noting that, for example, between text content,<span>你好</span> <span>世界</span>If spaces in the middle are deliberately deleted, it may cause text sticking together and affect readability. Therefore, in such cases, we should avoid using it-To keep the necessary blank spaces in the template.

Transcend the template: other strategies for optimizing redundant blanks

AnQiCMS template optimization is the first step, but to achieve the ultimate page simplification, we need to consider a more comprehensive strategy:

  1. HTML/CSS/JS Compression (Minification):Before deploying the website, perform unified compression on static resource files, remove all whitespace, comments, and unnecessary characters from the code.This is usually automated through build tools or server plugins.
  2. Server-side Gzip/Brotli Compression:Configure the Web server (such as Nginx, Apache) to enable Gzip or Brotli compression.These technologies can compress the content before transmitting it to the user's browser, significantly reducing file size.AnQiCMS based on Go language, usually also integrates or utilizes these compression capabilities of the server side well.The document mentions that AnQiCMS supports Apache reverse proxy configuration, therefore the server-side compression configuration can be completed at the Apache or Nginx layer.

By combining the blank control mechanism of the AnQiCMS template with the compression optimization in the server-side and front-end construction process, we can provide users with a website that loads faster and offers a better experience.

Summary

Optimizing redundant whitespace is a seemingly minor but not negligible aspect of website performance optimization. AnQiCMS provides{%-and-%}Control characters provide a direct and efficient tool for fine-grained control over HTML output at the template rendering level.As a website operator, understanding and making good use of these features will help to better improve the overall quality of the website, making every visit more smooth and pleasant.


Frequently Asked Questions (FAQ)

Q1: Use-Will it affect the page layout?A1: In most cases, the HTML rendering engine will merge multiple consecutive whitespace characters (including spaces, line breaks, and tab characters) into a single space for processing.Therefore, removing these extra spaces usually will not change the visual layout of the page.Please note if you<pre>/<textarea>Use within the preformatted tags or in certain CSS property scenarios that depend on precise whitespace, which may result in unexpected outcomes, and it is recommended to test carefully.

Q2:-Should all{% %}Are used in labels?A2: Not so. **Practice should only be used where it truly generates unnecessary whitespace and affects file size or code readability. For example, when generating list items in a loop<li>then,{%- for -%}and{%- endfor -%}It makes the HTML structure more compact. But if the tag is used for natural separation between content text (for example<span>Hello</span> <span>World</span>),then it is not necessary to use-Remove the spaces in the middle to avoid word sticking.

Q3: Will the comments in the template generate redundant blank spaces?A3: AnQiCMS'{# 单行注释 #}This form of comment will not generate any output during rendering, therefore it will not produce redundant blank spaces. And{% comment %}...{% endcomment %}This block comment, although its content will not be output, the tag itself may occupy a line in HTML, leaving a blank line. At this point, you cancommentUse within tags-control characters, such as{%- comment -%} ... {%- endcomment -%}Remove the redundant whitespace it introduces.