How to avoid extra blank lines when template logical tags (such as if, for) are rendered on the page?

Calendar 👁️ 69

When developing templates in AnQiCMS, we often find that even though the template code itself looks neat, the final rendered HTML page may contain some unexpected blank lines.These blank lines do not affect the functionality of the page, but they may make the HTML source code look less neat, and in some extreme optimization scenarios, they may even cause a slight increase in file size.For users who pursue code aesthetics and concise output, how to effectively avoid these unnecessary blank lines is a topic worth exploring.

These extra blank lines are mostly from the logic tags in the template, especially{% if %}condition judgment and{% for %}A loop structure. When these tags occupy a line independently, even if they themselves do not produce visible content, they may leave traces of line breaks during rendering. For example, a simpleforloop, if the list is empty,{% for ... %}and{% empty %}/{% endfor %}the position occupied by these lines of tags may result in multiple blank lines in the final HTML. Similarly, a tag that does not meet the condition,ifThe block of text, it will also cause a blank line due to the existence of the tag itself.

Fortunately, AnQiCMS provides a simple and effective mechanism to eliminate these unnecessary blank lines, which is to use hyphens within template tags(-)Perform whitespace character control.This little symbol, placed at the beginning or end of a template tag, can precisely tell the rendering engine whether to remove the whitespace characters around the tag, including newline characters.

In particular:

  1. {%- tag %}When the hyphen is placed on the left side of the tag (between the percentage sign and the tag name), it will remove the tagBeforeAll whitespace characters, including the newline character from the previous line.
  2. {% tag -%}When the hyphen is placed on the right side of the tag (between the tag name and the percent sign), it will remove the tagAfter thatAll whitespace characters, including the newline character at the end of the line.

By flexibly combining these two usages, we can achieve precise control over blank lines.

Let's see some actual examples:

For conditional judgment tags (if,elif,else,endif): If you haveifJudgment statements and their content should be on separate lines, and you do not want to leave any blank lines when rendering the content, you can modify it like this:

{%- if some_condition -%}
    <p>这是一个满足条件的内容。</p>
{%- elif another_condition -%}
    <p>这是另一个满足条件的内容。</p>
{%- else -%}
    <p>没有任何条件满足。</p>
{%- endif -%}

In this example,{%- if ... -%}It will remove all leading whitespace (including new lines),{%- elif ... -%}It will remove whitespace before and after it,{%- else -%}Similarly, and{%- endif -%}It will remove all the preceding whitespace. So, whether any branch is executed or none is executed, the blank lines generated by the label itself will be removed.

For loop traversal tags (for,empty,endfor):forLoops are the main culprits for producing extra blank lines, especially when the loop list is empty or when no additional line breaks are needed after each item within the loop. By using hyphens, we can make the loop output more compact:

<ul>
{%- for item in archives -%}
    <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{%- empty -%}
    <li>暂无相关内容。</li>
{%- endfor -%}
</ul>

here,{%- for ... -%}It will remove the blank before it,{%- empty -%}and{%- endfor -%}Similarly. Even ifarchivesThe list is empty, or the loop generates multiple<li>items, it will not produce extra blank lines inside the<ul>tags. If<li>Label internal variable{{ item.Title }}Don't want to have an empty line either, you can further refine the variable output:<li><a href="{{ item.Link }}">{{- item.Title -}}</a></li>.

Master this little trick, and your AnQiCMS template will become more streamlined, and the output HTML will also meet higher standards, whether from the perspective of visual neatness or from the perspective of subtle performance optimization, it is a good habit worth cultivating.


Frequently Asked Questions (FAQ)

Q1: Removing blank lines will it affect the rendering speed of the template?A: Usually, such optimization has a negligible impact on rendering speed, almost negligible.Because the template engine handles these hyphens by performing more refined control over whitespace characters during the parsing phase and does not introduce complex calculations.Its main value lies in improving the neatness and readability of generated HTML code.

Q2: Should I use hyphens on all template tags?A: Do not overuse.The hyphen is powerful in function, but overuse may reduce the readability of the template code itself, making the code look too tight and difficult to maintain.Should be selectively applied to those that actually generate extra blank lines, or have strict requirements for page output format.For example, when you need to generate compact JSON or text output in a specific format, hyphens can be very useful.

Q3: This method is only suitable forifandforAm I supposed to use the label?A: This blank character control mechanism (i.e., using before and after tags) is applicable to all logical tags in the AnQiCMS template, including but not limited to-)if/for/with/blockAs long as possible to produce blank line tags, you can try to use hyphens to control.

Related articles

How to loop through a list of data in a template and display it (using for loop), supporting counting and reversing?

In Anqi CMS template, efficiently displaying list data is an indispensable part of website content operation.Whether it is to display the latest articles, product lists, category directories, or customized data sets, flexible looping through these data and fine-grained control can greatly enhance the performance and user experience of the website.The Anqi CMS provides a powerful and easy-to-use template engine, its `for` loop tag is rich in features, supporting not only basic iteration but also easily implementing counting, reversal, and more advanced operations.### Core Concept: `for`

2025-11-08

How to implement conditional (if/else) dynamic display of content and layout in a template?

In website operations and frontend development, we often need to flexibly display content or adjust the page layout according to different situations.This dynamic ability is the core value of the conditional judgment tag (`if/else`) in AnQiCMS templates.The Anqi CMS template engine is simple and powerful, allowing us to set logic on the page like writing program code, making the website content show endless possibilities.### The Dynamic Beauty of AnQi CMS Template

2025-11-08

How to customize the display template for specific articles, categories, or single pages to achieve personalized layout?

In website operation, providing exclusive display methods for specific content can significantly improve user experience and content marketing effectiveness.AnQiCMS (AnQiCMS) is well-versed in this field, providing flexible and diverse template customization features, allowing you to easily create a unique personalized layout for articles, categories, or single pages. The AnQi CMS realizes personalized template customization in two main ways: one is to follow specific **template file naming conventions**, the system will automatically identify and apply;Secondly, it is manually specified in the background management interface to use a custom template file.--- ### One

2025-11-08

How to modularize the header, footer, and other common parts in a template and reference them on each page to display uniformly?

In website operation, maintaining the consistency and efficient management of the website pages is the key to improving user experience and operational efficiency.For AnQiCMS (AnQiCMS) users, modularizing the header, footer, navigation bar, and other common parts not only ensures consistency in the overall visual style of the website but also greatly simplifies the maintenance and update work in the future.Strong and flexible template system provided by AnqiCMS, allowing you to easily achieve this goal.### Understanding AnQiCMS Template Architecture AnQiCMS template files are usually stored in

2025-11-08

How to define and call the `macro` tag in AnQiCMS template to display reusable code blocks?

AnQiCMS provides a flexible and powerful template system, making the display of website content efficient and beautiful.In template development, in order to enhance the reusability and maintainability of code, we often encounter the need to encapsulate a commonly used code snippet so that it can be called in different places.This is when the `macro` tag becomes the key tool to achieve this goal.It allows us to define reusable code blocks, like functions in programming languages.Why do we need the `macro` tag?

2025-11-08

How to optimize page layout and content display by using the template inheritance (`extends`) tag?

In AnQiCMS, templates are the foundation for building the appearance and layout of a website.A well-designed template not only makes the website look professional and beautiful, but also greatly enhances the efficiency of content operation and the convenience of website maintenance.Among many powerful template tags, the `extends` (template inheritance) tag is undoubtedly one of the key tools for optimizing page layout and content display.It can help us build a unified and flexible website structure, making content operation and frontend development more effortless.### Understanding `extends`: The core of template inheritance `extends`

2025-11-08

How to declare a temporary variable in AnQiCMS template and use it to display content, improving template flexibility?

In the template creation of Anqi CMS, we often need to display various dynamic content.It is crucial to be proficient in using temporary variables to make templates more flexible and code more concise.The Anqi CMS template engine provides powerful functionality for declaring temporary variables, which helps us better organize and process data, thereby improving the efficiency and maintainability of content display.### Understanding the Value of Temporary Variables Imagine that you need to display processed data at multiple locations on the page, or that a piece of data is used repeatedly in conditional judgments, or that data obtained from a tag needs to be further processed before it can be presented

2025-11-08

How to obtain and display the title and detailed content on the article detail page?

Manage website content in Anqi CMS, the article detail page is an important window for displaying core information to visitors.Whether it is corporate news, product introduction, or technical articles, clearly presenting the title and detailed content is the key to improving user experience and information communication efficiency.The Anqi CMS provides intuitive and powerful template tags, making content presentation easy and flexible. ### Understanding Anqi CMS Template Structure Anqi CMS template files are usually stored in the `/template` directory and follow a set of simple naming conventions.for the detail page of an article or product document

2025-11-08