How to remove extra blank lines or newline characters in the output content by using the `remove` tag in AnQiCMS?

Calendar 👁️ 82

When using AnQiCMS for website template development, we often encounter a detail issue, that is, the logical tags in the template (such asifConditional judgment,forLoops) Sometimes, when rendering output HTML content, unnecessary blank lines or newline characters are generated. These seemingly harmless whitespaces, although they usually do not affect the final visual presentation of the page, may cause some minor troubles in certain scenarios where high code neatness or precise control of element spacing is required, such as affecting the readability of the HTML source code, or even in extremely rare cases, may cause slight layout differences (especially when usingdisplay: inline-blockWhen in the style).

Fortunately, AnQiCMS provides a simple and powerful mechanism to solve this problem, that is, to use template tags.removeLabel, by using a simple-character to remove these extra spaces.

Why do extra blank lines or newline characters appear?

When we are writing an AnQiCMS template, in order to improve the readability and maintainability of the code, we usually place logical tags on separate lines and make appropriate indentation. For example:

<nav>
    {% if user.IsLoggedIn %}
        <a href="/profile">个人中心</a>
    {% else %}
        <a href="/login">登录</a>
    {% endif %}
</nav>

When processing this code in the template engine,{% if ... %}/{% else %}and{% endif %}These tags themselves do not generate any visible content in the final HTML. However, the lines they are in and the line breaks between them may be retained by default, resulting in additional blank lines appearing in the final rendered HTML source code:

<nav>

        <a href="/profile">个人中心</a>

</nav>

It can be seen that innavthe tag inside, as well as before and after the links, unnecessary line breaks appeared.

Skillfully useremovethe tag removes whitespace

AnQiCMS template engine allows us to add a hyphen adjacent to the start or end tag of logical tags{%or%}at the position-to control the white space output around it

  1. Remove the whitespace before the tag:{%-Immediately after the start tag{%Add-, that is{%-, this tells the template engine to remove all whitespace characters (including newline characters) before this tag.

  2. Remove the blank after the tag:-%}immediately before the end tag%}Add-, that is-%}This will tell the template engine to remove all whitespace characters (including newline characters) after this tag.

We can use them separately as needed{%-or-%}They can also be combined to achieve the most compact output effect.

Let's look at some actual examples to see its magic.

Example 1: Eliminating whitespace in conditional judgments

Return to the previous navigation example, if you want to eliminateifYou can modify the blank lines around the conditional block like this:

<nav>
    {%- if user.IsLoggedIn -%}
        <a href="/profile">个人中心</a>
    {%- else -%}
        <a href="/login">登录</a>
    {%- endif -%}
</nav>

After this treatment, ifuser.IsLoggedInIf true, the rendered HTML may become more compact:

<nav>
    <a href="/profile">个人中心</a>
</nav>

This not only makes the HTML source code look cleaner, but also eliminates potential spacing issues.

Example two: controlling blank spaces in loops

InforIn loops, we often need to control the output format of each loop item. Consider a product list:

<ul class="product-list">
{% for product in products %}
    <li class="product-item">
        <img src="{{ product.Thumb }}" alt="{{ product.Title }}">
        <h3>{{ product.Title }}</h3>
    </li>
{% endfor %}
</ul>

By default, each loop starts on a new line,{% for ... %}and{% endfor %}The tag itself also contributes an empty line, resulting in output like this:

<ul class="product-list">

    <li class="product-item">
        <img src="/thumb1.jpg" alt="产品1">
        <h3>产品1</h3>
    </li>

    <li class="product-item">
        <img src="/thumb2.jpg" alt="产品2">
        <h3>产品2</h3>
    </li>

</ul>

To remove the empty line produced by the loop tag itself while retaining each<li>The element is kept on a separate line to maintain readability, we can do this:

<ul class="product-list">
{%- for product in products -%}
    <li class="product-item">
        <img src="{{ product.Thumb }}" alt="{{ product.Title }}">
        <h3>{{ product.Title }}</h3>
    </li>
{%- endfor -%}
</ul>

In this way, the HTML structure will become more compact, and each<li>will still be kept on a separate line:

<ul class="product-list">
    <li class="product-item">
        <img src="/thumb1.jpg" alt="产品1">
        <h3>产品1</h3>
    </li>
    <li class="product-item">
        <img src="/thumb2.jpg" alt="产品2">
        <h3>产品2</h3>
    </li>
</ul>

If you need an even tighter compactness, for example, to render all list items on a single line, you can also use content tags.-:

<ul class="inline-list">{%- for item in items -%}{{- item.Title -}}{%- endfor -%}</ul>

The generated HTML will be:

<ul class="inline-list">标题1标题2标题3</ul>

This extremely compressed usage is usually only considered in very special layout requirements or performance optimization (reducing file size) scenarios.

Considerations in practice

MasterremoveThe use of tags allows us to be more proficient in AnQiCMS template development

Related articles

How to accurately control the timestamp date format display in the `stampToDate` tag of AnQiCMS on the front-end?

Clear, consistent, and beautiful time information display is crucial for user experience in website operation.Whether it is the publication date of the article, the update time of the product, or the deadline of the event, a friendly date and time format always allows visitors to obtain information more quickly.AnQiCMS deeply understands this, providing us with a powerful and flexible template tag - `stampToDate`, which allows us to easily control various date and time formats displayed on the front end.

2025-11-09

How to customize and display category banner images or thumbnails in AnQiCMS to enhance the visual effect?

In website operation, the application of visual elements is crucial for attracting users and enhancing brand image.The category page is an important entry for users to browse content, and if it is equipped with a dedicated Banner image or thumbnail, it will undoubtedly greatly enhance the aesthetics and professionalism of the page.Next, let's see how to achieve this effect in AnQiCMS. ### One, Backend Settings: Configure Exclusive Visual Elements for Categories To add exclusive visual elements to a category, we first need to enter the AnQiCMS backend management interface.

2025-11-09

Does AnQiCMS support displaying a custom shutdown prompt when the website is closed?

The website is in operation, and it is inevitable that there will be situations where the website needs to be temporarily closed, such as system upgrades, data maintenance, or business adjustments, etc.How can you clearly and professionally convey the current status of the website to visitors to avoid them seeing a stiff error page, which is particularly important.For users using AnQiCMS, the good news is that the system has fully considered this point and provided a very flexible setting function for the shutdown prompt information.Yes, AnQi CMS fully supports displaying your custom prompt information when the website is closed.

2025-11-09

How to flexibly control the conditional display and loop output of content in AnQiCMS templates through `if`, `for`, and other logical tags?

In AnQiCMS, templates are the core of building the appearance and displaying content of a website.It is crucial to be proficient in using the logical tags in the template to make the content of the website more flexible and intelligent.This is the two great tools of dynamic content display, `if` conditional judgment and `for` loop output tags.They allow you to display or hide content based on specific conditions, efficiently traverse and output a series of data, thereby creating highly customized and responsive frontend pages to meet user needs.

2025-11-09

How to accurately calculate the word count of article content in AnQiCMS templates?

How to easily count the word count of article content in AnQiCMS template?Practical methods and skills Understanding the word count or number of words in articles is a basic and important indicator in content operation.It is very helpful to accurately count the length of article content for SEO optimization, cost assessment of content creation, or to ensure that the content complies with platform standards.

2025-11-09

How does the `wordcount` filter define the boundary of a 'word' when counting Chinese content?

In daily website content operation, we often need to count and control the length of content, which is crucial for SEO, layout and user reading experience.The AnqiCMS provides a series of practical template filters to help us complete these tasks, where `wordcount` is a tool used to count the number of 'words' in a string.However, how is the boundary of the 'word' defined for Chinese content?This may be a question that many users encounter when using it.

2025-11-09

How to use the `wordcount` filter to display the word count of each article on the article list page?

When operating a website, we often need to understand some basic information about articles, such as reading time, word count, etc.This data not only helps us optimize the content strategy, but also allows readers to have a preliminary judgment of the article while browsing.In Anqi CMS, by using its powerful template filter function, we can easily display the word count of each article on the article list page.

2025-11-09

AnQiCMS blog article detail page, how to display the total word count of the current article in real time?

In content operation, the word count (or number of words) of an article is often an important indicator for measuring the depth of content, the estimated reading time, and even search engine optimization (SEO).For those of us who use AnQiCMS to manage website content, being able to display the total word count of the current article in real-time on the blog article detail page would undoubtedly enhance user experience and provide more intuitive data reference for website operation.AnQiCMS powerful template system and built-in features make it simple and efficient to meet this requirement.

2025-11-09