How to make AnQiCMS template conditional rendering not produce any blank output?

Calendar 👁️ 67

Say goodbye to annoying blank: Zero blank output practice of AnQiCMS template conditional rendering

In website operation and development, we pursue the ultimate user experience and SEO optimization (SEO) as our relentless goal.A seemingly minor detail -- excessive whitespace and blank lines in templates can sometimes affect page loading speed, increase bandwidth consumption, and even cause rendering issues in some strict layouts or JSON-LD structured data.AnQiCMS as an enterprise-level content management system developed based on the Go language is known for its high performance, modular design, and SEO-friendliness.It uses a template engine syntax similar to Django, simple and efficient.However, when performing conditional rendering, how to ensure that these logical tags do not produce any unnecessary blank output is a skill that every AnQiCMS operator or developer pursuing perfection must master.

AnQiCMS's template system provides a series of powerful tags and filters, making content display flexible and versatile.Whether it is multi-site management, a flexible content model, or advanced SEO tools, AnQiCMS strives to provide users with a smooth experience.{% if ... %}/{% for ... %}When such conditions and loop logic are present, the lines they occupy, as well as the possibility of empty internal variables, may leave annoying blank spaces in the generated HTML.

Understanding the source of blankness: logical tags and empty data

Firstly, we need to clarify how these 'blanks' are generated. When we write the following code in the template:

{% if some_condition %}
    <p>这段内容会在满足条件时显示。</p>
{% endif %}

Even ifsome_conditionWithtrue,<p>The label content is rendered,{% if ... %}and{% endif %}These two lines, after being processed by the template engine, may leave an empty line in the final HTML if there is no other content on the line. In addition, if the internal variable in conditional rendering is empty, for example,{{ item.Title }}InitemThere is not orTitleWhen the attribute is empty, it may also render an empty string, which may cause blank lines to appear. These seemingly harmless blanks, accumulated together, may become a problem.

Core Tool: Whitespace Control{%-and-%}

AnQiCMS's template engine provides a very intuitive and powerful solution for this:Whitespace Control. Add a hyphen next to the percentage symbol in the logical tag (%)-We can precisely control whether the template engine removes whitespace (including newline characters) before or after these tags.

In particular:

  • {%-This tag will remove the whitespace.FrontAll whitespace characters, including newline.
  • -%}This tag will remove the whitespace.behindAll whitespace characters, including newline.

Imagine our previous example, if you want to modify it so that it does not produce a blank line, you can do it like this:

{%- if some_condition -%}
    <p>这段内容会在满足条件时显示。</p>
{%- endif -%}

Here, {%- if some_condition -%}It will remove all leading spaces (up to the previous non-space content or the beginning of the file) and remove all trailing spaces (up to the<p>beginning of the tag). Similarly,{%- endif -%}It will delete it before (up to</p>Remove trailing spaces and delete any spaces after it (until the next non-space content or the end of the file)This way, the entire conditional block will not produce any blank lines when not rendered, and even when rendered, extra lines will be controlled accurately.

This technique is particularly suitable for loop tags, such as{%- for item in archives -%}, and it is necessary to<head>insert structured data (such as JSON-LD) within the tag:

{%- jsonLd -%}
<script type="application/ld+json">
{
	"@context": "https://schema.org",
	"@type": "WebPage",
	"name": "{{ archive.Title }}",
	"description": "{{ archive.Description }}"
}
</script>
{%- endjsonLd -%}

Here, {%- jsonLd -%}and{%- endjsonLd -%}The use of it ensures.<script>The newline and whitespace before and after the tag block are not retained, making the JSON-LD content more compact and in line with the specification.

Fine control: combining variables with content

It is not enough to rely solely on blank control at the label level, we also need to consider the possibility that the variable itself may be empty. If a variable{{ some_variable }}After rendering it is an empty string, it can still lead to a blank line. Therefore, it is best to wrap the output variable in a conditional judgment and combine it with a whitespace control character:

{# 假设 item.Title 可能为空 #}
{%- if item.Title -%}{{ item.Title }}{%- endif -%}

So whenitem.TitleWhen it is empty, the whole{%- if -%}The block will not output any content, thereby completely eliminating whitespace.

Moreover, when the conditional rendering is not just text, but the entire HTML container (such as<div>or<img>When, we should render the entire container as the object of the condition. For example, a page banner image may only be displayed after it is uploaded to the backend:

{%- categoryDetail bannerImages with name="Images" -%}
{%- if bannerImages -%}
    {%- set pageBanner = bannerImages[0] -%}
    {%- if pageBanner -%}
        <div class="page-banner" style="background: url({{pageBanner}}) no-repeat;"></div>
    {%- endif -%}
{%- endif -%}

By such multi-layered nesting and white space control, we can ensure that only whenbannerImagesexisting and the first imagepageBanneralso exists,divthe container will be rendered, avoiding the creation of emptydivLabel or unnecessary blank lines.

Pre-check: Avoid rendering of empty data.

In addition to the aforementioned methods, we can also reduce unnecessary rendering logic by pre-checking whether the data itself is empty. AnQiCMS provideslengthFilters that can help us judge the length of a set or string:

{%- if someList|length > 0 -%}
    <ul>
        {%- for item in someList -%}
            <li>{{ item.Name }}</li>
        {%- endfor -%}
    </ul>
{%- endif -%}

This method is especially effective when dealing with lists, as it ensures that onlysomeListthe entire operation is performed when the data is indeed contained.<ul>The structure will be rendered, otherwise, along with<ul>All content within the tags will not be output, naturally avoiding blank spaces.

Maintain code readability and neatness

Although whitespace characters are powerful tools, overuse may make template code dense and affect reading experience. Therefore, it is recommended to prioritize their use in the following scenarios:

  • Areas with strict requirements for performance or SEOFor example<head>within the labelmetaInformation, JSON-LD scripts, etc.
  • Elements requiring a compact layoutFor example, the separator of links in the navigation bar and breadcrumb navigation.
  • Repeatedly rendered small segmentsFor example, list items, tag clouds, ensure that the spacing between each element is controllable.

In other cases, moderate spacing helps with code readability, and we can maintain the logical clarity and visual neatness of the code without affecting the final rendering effect.The ultimate goal is to achieve zero blank output without sacrificing the maintainability of the template.

By practicing these methods, AnQiCMS users can more accurately control the output of templates, achieving zero blank rendering in the true sense, thereby optimizing website performance, enhancing user experience, and presenting a more professional and efficient image to search engines.

Related articles

Can AnQiCMS template's `-` syntax improve page loading speed?

## AnQiCMS template's `-` syntax, can it really make the page fly?In-depth analysis of its impact on loading speed In today's fast-paced internet era, website loading speed has become one of the core indicators of user experience and search engine ranking.As an experienced website operations expert, I know that every optimization detail can bring significant improvement.AnQiCMS (AnQiCMS) boasts its high-performance architecture based on the Go language, excelling in page response speed.However, when mentioning AnQiCMS

2025-11-07

How important is whitespace control when building HTML emails with AnQiCMS templates?

## In AnQiCMS template building HTML email: The importance of white space control cannot be overlooked As an experienced website operation expert, I fully understand the core status of content in digital marketing.And email, as one of the most direct and personalized communication channels, the way its content is presented directly affects user experience and conversion effects.When we apply the powerful template function of AnQiCMS to the construction of HTML emails, a seemingly trivial but extremely critical detail emerges - that is the precise control of blank characters in the template output.

2025-11-07

How to remove unexpected line breaks from the AnQiCMS template rendered HTML structure?

## Say goodbye to redundant spaces: AnQiCMS removal skills of extra line breaks in template rendering Hello everyone, friends of AnQiCMS!As an experienced website operations expert, I know that every detail is crucial on the road to pursuing website performance optimization and user experience.AnQiCMS with its efficient, secure, and SEO-friendly features, has become a powerful assistant for many small and medium-sized enterprises and content operation teams.In daily use, we often take advantage of its flexible template engine to build a variety of rich page layouts.

2025-11-07

What is the difference between `{%- tag %}` and `{% tag -%}` in AnQiCMS templates?

In AnQiCMS template development practice, every detail may affect the final presentation effect and code quality of the page.As an experienced website operations expert, I know that the standardization of template writing is not only related to the beauty of the page, but also closely related to the optimization of website performance and search engine friendliness.

2025-11-07

How to ensure no blank line is generated when processing the `empty` block in AnQiCMS template?

In AnQiCMS (AnQiCMS) template development, we are committed to creating efficient and tidy code.AnQiCMS based on Go language and Django template engine syntax, providing flexible content management and display capabilities.Its template syntax is concise and powerful, allowing content operators and developers to easily build diverse website pages.However, in practice, especially when dealing with dynamic content lists, a common but often overlooked issue is - how to ensure that the `empty` block does not generate extra blank lines in the final rendered HTML when there is no content

2025-11-07

When should you consider removing the line occupied by logical tags in the AnQiCMS template file?

## AnQiCMS Template Refinement: When Should You Skillfully Use the `-` Symbol to Remove Blank Lines Occupied by Logical Tags?As an experienced website operations expert, I am well aware that AnQiCMS, with its efficient Go language architecture, flexible content model, and deep SEO optimization, brings excellent efficiency and stability to enterprises and content operations teams.In the daily use of AnQiCMS, in addition to the basic content publishing and management, we should pay more attention to details, optimize the refined templates to further improve website performance and user experience.

2025-11-07

What is the special meaning of the writing style `{{- variable -}}` in AnQiCMS templates?

As an experienced website operation expert, I fully understand that even the smallest grammar details in a content management system can affect the final page display and development efficiency.AnQiCMS with its efficient architecture based on the Go language and the easy-to-use Django template engine syntax, provides us with great flexibility.Today, let's delve deeply into a seemingly minor but extremely practical syntax feature in AnQiCMS template development - the special meaning contained in the writing style `{{- variable -}}`.

2025-11-07

What is the practice of removing empty lines of `if/for` tags in AnQiCMS templates?

## AnQiCMS template development: Say goodbye to redundant blank lines, the practice of blank control for `if/for` tags** As an experienced website operations expert, I know the importance of an efficient and tidy template system for the long-term healthy development of a website.AnQiCMS (AnQiCMS) boasts its high-performance architecture based on the Go language and its flexible Django-style template engine, bringing an excellent experience to content management.In daily template development

2025-11-07