How to remove empty lines generated by conditional judgment or loop tags in the template to optimize the display cleanliness of the page source code?

Calendar 👁️ 65

In website operation, we all hope that the website is not only powerful in function and rich in content, but also the source code behind it should be neat and orderly.However, when developing with a flexible template system like AnQiCMS, one may find that the generated HTML source code contains many blank lines, which are mainly caused by conditional judgment or loop tags in the template.Although these blank lines usually do not have a substantial impact on the functionality of the website, they do indeed reduce the readability of the source code and make subsequent maintenance and debugging more inconvenient.It is more important that for operators who pursue extreme optimization, reducing page redundant content and making the code more compact is always a detail worth paying attention to.

An enterprise CMS deeply understands this, and to solve this problem, it provides a very elegant and practical solution in the template syntax.

Explore the root cause of blank line generation: template logic tags

In AnQiCMS, we use a template engine syntax similar to Django for template creation, which means we can embed in the HTML structure{% if ... %}/{% for ... %}logic tags to achieve dynamic content display.These tags usually occupy a single line, or their internal content is empty when rendered. When the template engine processes this logic, even if they themselves do not output any visible content, they may leave a blank line in the final generated HTML.

For example, a simple conditional judgment:

<div>
{% if article.Title %}
    <h1>{{ article.Title }}</h1>
{% endif %}
</div>

Ifarticle.TitleIf it is empty, then<h1>The tag will not render, but{% if ... %}and{% endif %}The line occupied by the tag, by default, may still leave two blank lines in the output HTML.The loop tag is also the same, when there is no content output inside the loop body, it may also produce an empty line.

An elegant solution of Anqi CMS: whitespace control characters-

To solve the problem of these redundant blank lines, Anqi CMS provides a simple and powerful blank control symbol:Dash-By placing this symbol cleverly at the beginning or end of the template tag, we can precisely control whether whitespace characters (including newline characters) around the tag are removed.

This blank control character can act in two positions:

  1. Remove the blank characters on the left side of the tag:{%- tag ... %}For example, when you place the dash inside the percentage sign on the left side of the tag,{%- if article.Title %}It will tell the template engine to remove this tagBeforeAll whitespace characters (including newline characters). If this tag is also the starting tag of the content it contains, it will also remove the whitespace characters between the tag and the content.

  2. Remove the whitespace on the right side of the tag:{% ... tag -%}Similarly, if the dash is placed inside the percentage sign on the right side of the tag, for example{% endif -%}it will remove this tagAfter thatAll whitespace characters (including newline characters). If this tag is also the end tag of the content it contains, it will also remove the whitespace characters between the content and the tag.

Let's take a look at how it works with an example:

Optimize the blank line of the conditional judgment tag

Suppose the following template code is given:

<nav>
    <a href="/">首页</a>
    {% if user.IsLoggedIn %}
    <a href="/profile">个人中心</a>
    {% else %}
    <a href="/login">登录</a>
    <a href="/register">注册</a>
    {% endif %}
    <a href="/contact">联系我们</a>
</nav>

Ifuser.IsLoggedInIs true, or the wholeif-elseThe content of the block ultimately ends up empty on the page, then in<nav>Extra blank lines may appear inside the tag. By adding whitespace control characters, we can make the output more compact:

<nav>
    <a href="/">首页</a>{%- if user.IsLoggedIn -%}
    <a href="/profile">个人中心</a>{%- else -%}
    <a href="/login">登录</a>
    <a href="/register">注册</a>{%- endif -%}
    <a href="/contact">联系我们</a>
</nav>

After such processing, regardlessifwhether the condition is met,{%- if ... -%}/{%- else -%}and{%- endif -%}the blank lines produced by the tag will be removed, ensuring that the generated HTML code is more compact.

Optimizing the blank lines of loop tags

Loop tags are another common source of blank lines. Consider a loop in a navigation list:

<ul>
{% for item in navs %}
    <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% endfor %}
</ul>

IfnavsThe list is empty or each<li>The content inside the tag is not rendered due to certain conditions, and extra blank lines will also be generated by default.

Optimize using whitespace control characters:

<ul>{%- for item in navs -%}
    <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>{%- endfor -%}
</ul>

Thus,{%- for ... -%}and{%- endfor -%}The line occupied by the tag will not leave an empty line in the final HTML, even if the loop body does not produce any output.

Furthermore, it is worth noting that for the variable output tag{{ 变量 }}In some cases, you may also want to control the surrounding whitespace. Anqi CMS (based on Pongo2) allows whitespace control characters to be applied to variable output:

<div>{{- archive.Title -}}</div>

This will removearchive.TitleVariable output content with spaces on both sides.

Why is this important? Code neatness and maintainability.

Removing these blank lines generated by logical labels brings many benefits:

  1. Improve code readability: Tightly packed code is easier to glance over and understand its structure, especially during debugging or team collaboration.
  2. Easier to debugWhen encountering rendering issues, a clean source code view can help you locate the problem faster, rather than being distracted by irrelevant blank lines.
  3. Minor performance improvement: Although a single blank line has a negligible impact on the size of a webpage, for websites with high traffic, complex content, or those aiming for extreme optimization, accumulating small savings can lead to a slight advantage in loading speed and also helps save bandwidth.
  4. Consistency and professionalism: Maintaining clean source code reflects the attention to detail of operators and developers, which helps to form good coding habits and project specifications.

Practical suggestions

During AnQiCMS template development, it is recommended to develop the habit of using whitespace control characters, especially in the following scenarios:

  • Start and end of HTML structureFor example<div>{%- if ... %}and{% endif -%}</div>.
  • Loop tags:{%- for ... -%}and{%- endfor -%}.
  • Conditional judgment tag:{%- if ... -%}/{%- elif ... -%}/{%- else -%}and{%- endif -%}.
  • Dynamic content that needs to be tightly connected with adjacent HTML elementsEnsure there are no unexpected blank spaces between elements.

By consciously using this little em dash, you will be able to build a cleaner, more efficient AnQiCMS website template, making the page source code as pleasing to the eye as a well-tended garden.


Frequently Asked Questions (FAQ)

1. Removing blank lines from the template helps the website's SEO, right?Remove the blank lines generated by these template logic tags,

Related articles

How to display the view count of articles in the article list or detail page?

In website operation, the number of article views is a very intuitive and important indicator, which not only reflects the popularity of the content but also provides data support for the optimization of subsequent content strategies.For websites built using AnQiCMS, displaying the number of views on article lists or detail pages is a relatively simple operation. With the powerful template tag features provided by AnQiCMS, we can easily achieve this requirement.Understanding the "Page Views" feature of AnQiCMS AnQiCMS is built with powerful traffic statistics functionality

2025-11-08

How to display a custom Banner image group on the category page or single page?

It is crucial that visual content is attractive in website operation.Especially for category pages and standalone single pages, displaying a set of carefully designed banner images can effectively attract visitors' attention, strengthen the brand image, and convey specific information.AnQiCMS provides a straightforward and flexible mechanism that allows you to easily configure and display custom Banner image groups on these key pages.

2025-11-08

How to display recommended attributes (such as headlines, recommendations, sliders) in article lists or detail pages?

In AnQi CMS, in order to make your website content more attractive and able to flexibly display articles of different importance levels or display forms, the system provides a "recommended attribute" feature.By cleverly utilizing these properties, you can easily add "headlinesThis guide will take you in-depth to understand how to set and use these recommended properties in AnQiCMS, making your website content management more convenient.

2025-11-08

How to set up the website to display specific notification information on the front end when it is in "closed site" status?

The website will always encounter times when it needs to be maintained, upgraded, or temporarily adjusted.At this moment, it is crucial for users to see a friendly and clear prompt instead of a cold error page, which is vital for the image and user experience of the website.Fortunately, AnQiCMS provides a very convenient and flexible "shutdown" function, allowing you to easily manage the maintenance status of the website and display customized notification information to front-end visitors.Next, let's take a look at how to set the "shutdown" status of the website in the AnQiCMS backend

2025-11-08

How to render Markdown content into HTML in AnQiCMS?

In a content management system, Markdown is a lightweight markup language that allows content creators to write in plain text and format it with simple symbols, which can then be easily converted into structured HTML.For AnQiCMS users, using Markdown to write content not only improves efficiency but also ensures consistency and maintainability of content formatting.

2025-11-08

What is the specific usage of the `render` filter in Markdown content rendering?

In Anqi CMS, we often encounter the need to convert text into colorful web elements.Especially when content is written in Markdown format, how to ensure that it can be presented correctly and beautifully to the user is a problem that requires a deep understanding.Today, let's talk about how the `render` filter works in the Anqi CMS Markdown content rendering.

2025-11-08

How to manually control the Markdown to HTML rendering of the `Content` field of the document?

In a content management system, Markdown, as a lightweight markup language, is widely popular for its simplicity and efficiency.AnQiCMS (AnQiCMS) fully understands the importance of content flexibility to users and therefore provides a flexible mechanism for handling the Markdown rendering of document content.When you need to manually control the Markdown to HTML rendering of the `Content` field in the document, the system provides an intuitive and powerful method.### Understanding the AnQi CMS Markdown processing mechanism First

2025-11-08

How to enable or disable Markdown rendering in the category detail page's `Content` field?

AnQi CMS provides high flexibility for users, especially in terms of content display.For the `Content` field on the category detail page, the system allows us to finely control whether it is rendered in Markdown format.This means that website operators can choose the most suitable processing method according to different content types and display requirements.### Understanding the `Content` field and Markdown rendering on the category detail page In AnQi CMS, each category has a `Content` field

2025-11-08