In Anqi CMS template, what is the specific function of the symbols `{%-` and `-%}`, and what common layout problems can they avoid?

Calendar 👁️ 71

In the development of AnQi CMS templates, we often strive for the beauty of the page and the neatness of the code.Sometimes, we may encounter some "hidden" layout issues, such as unnecessary blank lines between page elements, or excessive whitespace in the HTML source code.These issues may not always be obvious in the browser, but they can make our code redundant, and even cause actual errors in certain situations sensitive to whitespace (such as generating XML or CSV files).

Luckyly, AnQi CMS's template engine provides a very practical solution, which is to add in the template tags-symbols, specifically represented as{%-and-%}These little symbols have powerful control over whitespace characters, which can help us accurately "trim" away unnecessary whitespace, making the template output more compact and clean.

The issue is: extra blank caused by template tags

To understand{%-and-%}The role, we must first understand why these extra spaces appear. In the Django style template engine used in Anqi CMS, like{% if ... %}/{% for ... %}This logical tag itself does not generate visible content. But when it is parsed and executed, the lines it occupies in the template file and the surrounding whitespace characters (such as newline characters, spaces, and tab characters) are often output to the final generated HTML unchanged.

Imagine you are building a navigation menu:

<nav>
    {% for link in navLinks %}
    <a href="{{ link.url }}">{{ link.text }}</a>
    {% endfor %}
</nav>

This code looks clear in the template file, but the final HTML source code generated may not be as compact as you expect:

<nav>

    <a href="/link1">链接1</a>

    <a href="/link2">链接2</a>

</nav>

You will find each<a>Labels have extra line breaks, evennavThe tag also contains additional blank lines. Although modern browsers usually ignore the effect of these extra HTML whitespace characters on page layout, in some cases, these blanks may:

  1. Causes redundant source code:Increases file size, although the impact is negligible, it is still worth optimizing in large-scale, high-concurrency websites.
  2. Affects debugging experience:When checking page elements or debugging CSS, redundant blank lines make the HTML source code look cluttered and reduce readability.
  3. Destroying a specific layout:For some CSS layouts that require precise control of whitespace (such as in flex or grid layouts, the spaces between elements may be parsed as text nodes), excessive whitespace can lead to unexpected layout issues.
  4. Affects non-HTML output:If you use a template to generate XML, JSON, or plain text files, these additional whitespace characters may directly破坏 data structure or file format.

Solution:{%-and-%}of precise control

To solve the above problem, the Anqi CMS template engine introduced{%-and-%}these two special tags. Their function is very direct:

  • {%-:this tag will remove itsfrontAll adjacent whitespace characters (including newline and space).
  • -%}:this tag will remove itsAfterAll adjacent whitespace characters (including newline and space).

You can use them individually or combine them to achieve precise control over whitespace characters.

Let's go back to the navigation menu example above and see how we can optimize it:

<nav>
    {%- for link in navLinks -%}
    <a href="{{ link.url }}">{{ link.text }}</a>
    {%- endfor -%}
</nav>

Just in the{% for ... %}and{% endfor %}The HTML source code becomes completely different when a hyphen is added next to the percentage sign inside the tag:

<nav><a href="/link1">链接1</a><a href="/link2">链接2</a></nav>

All extra blank lines and whitespace characters have been cleared, making the code very compact and clean.

Specific application scenarios and effects

  1. Clearing the blank lines generated by logical labels:

    • forLoop:As shown above, this is the most common use case, especially when generating lists(<ul><li>...</li></ul>), navigation(<nav><a>...</a></nav>) or tables(<table><tr><td>...</td></td></table>) when.
    • if/elseConditional judgment:Similarly, the conditional judgment tag itself does not output content, but it is added before and after-This can avoid introducing unnecessary blank lines.
      
      <p>欢迎访问我们的网站!
      {%- if user.is_authenticated -%}
      您好,{{ user.username }}。
      {%- else -%}
      请登录。
      {%- endif -%}
      </p>
      
      This can ensure<p>The text within the tag is displayed continuously, without beingifbroken by the tag.
    • Other auxiliary tags:like{% set ... %}/{% with ... %}/{% include ... %}and tags, their main function is logical processing or introducing other template fragments, they should not produce extra output. Use-They can avoid leaving traces in the document flow.
  2. Generate smaller HTML files:By removing unnecessary whitespace, you can slightly reduce the size of the HTML file.This can improve the page loading speed to some extent, especially on websites with large amounts of content and frequent visits.Although the optimization effect of a single page may not be obvious, accumulating it will have a positive effect on server bandwidth and user experience.

  3. Improve code readability and maintainability:Clean HTML source code, without extra blank lines to interfere, making it easier for developers to view and debug.When it is necessary to locate an element or understand the page structure, concise code can be understood at a glance.

  4. Avoid layout surprises:In some CSS or JavaScript scenarios sensitive to whitespace, precise control of whitespace can avoid some subtle layout errors or script execution anomalies. For example, in CSS, a space between two inline block-level elements may be rendered as a small gap by the browser, and using-Can eliminate this gap, achieving seamless connection.

When to use and precautions

Although{%-and-%}Very powerful, but not suitable for all scenarios.

Recommended usage scenarios:

  • All logical control tags: for/if/set/withetc.
  • includeandextendsTags:Ensure that the content of the template being introduced can seamlessly integrate into the current template

Related articles

How to accurately remove extra blank lines and newline characters generated by logical tags in the AnQi CMS template?

When developing website templates, especially when using systems like AnQiCMS that are based on Django template engine syntax, a common problem is logical tags (such as `{% if ...`)}] %}` or `{% for ...After processing, sometimes unnecessary blank lines or newline characters may remain.These seemingly insignificant blanks may affect the final generated HTML structure, and even in some cases cause minor interference to the front-end style layout.

2025-11-08

How does AnQiCMS template call the contact parameters of the background custom settings?

In website operation, a clear and effective contact method is the bridge for users to communicate with us and also an important foundation for building trust.For users who use AnQiCMS to manage websites, how to flexibly call the contact information parameters set in the background in the website template is the key to improving operational efficiency and website maintainability.AnQiCMS powerful background customization features, combined with its intuitive template tags, make everything easy.

2025-11-08

How to set the scheduled publication function in AnQiCMS to control the display time of content?

Today, with the increasing refinement of content operation, how to manage content release efficiently and strategically is an important issue facing every website operator.AnQiCMS knows this very well and has provided a powerful scheduling publication feature to help users accurately control the timing of content going live, thereby achieving a more intelligent and automated operation.This feature not only allows you to maintain the rhythm of your website content publishing, but also effectively reduces the daily operation pressure, allowing you to focus more on the quality of the content itself.### Easily control the content release rhythm

2025-11-08

How does AnQiCMS display website traffic statistics and crawler scraping information?

During the operation of a website, a comprehensive understanding of the website's access situation and interaction with search engines is the key to improving website performance.AnQiCMS (AnQiCMS) understands this point and therefore provides a set of built-in traffic statistics and crawler monitoring functions to help us intuitively grasp the operation status of the website, thereby making more intelligent operational decisions.In the AnQiCMS backend management interface, this data is concentrated in the "Data Statistics" function module.

2025-11-08

How to use the `trim` filter of AnQi CMS to batch clean up spaces and newline characters at both ends of article content?

In the daily content operation of Anqi CMS, the neatness and consistency of content are the key to improving user experience and website professionalism.Sometimes, when editing or importing article content, some extra spaces or line breaks may be inadvertently introduced. These seemingly minor details may affect the layout of the content and even cause slight interference with the crawling and parsing of search engines in some cases.Luckyly, Anqi CMS's powerful template engine provides a rich set of filters, among which the `trim` filter is a powerful assistant for solving such problems

2025-11-08

What are the differences and application scenarios of the `trimLeft` and `trimRight` filters in AnQi CMS when processing leading/trailing spaces in strings?

During the operation and template creation of website content, we often encounter situations where we need to deal with extra spaces or specific characters in strings.These seemingly minor details actually relate to the neatness of the content, user experience, and even the SEO effect.AnQi CMS as an efficient content management system, deeply understands these needs, and provides a powerful and flexible string filter, especially `trimLeft` and `trimRight`, which can help us accurately control the leading and trailing parts of the string.###

2025-11-08

Want to remove all specific characters from a string, including spaces and newline characters?

When using Anqi CMS to manage website content, we often encounter scenarios where we need to refine the text content.For example, when using the article title as part of a URL or when displaying a brief introduction, we may want to remove extraneous spaces, line breaks, or other specific characters to ensure the neatness of the data format, improve SEO friendliness, or improve the page layout.AnQi CMS, with its powerful and flexible Django template engine, provides a variety of convenient template filters to help us easily implement these string operations

2025-11-08

How can I batch delete the specified keywords or phrases from the content of Anqi CMS articles?

In website content operations, we sometimes encounter situations where we need to make unified adjustments to a large number of articles, such as changing brand names, removing outdated information, or cleaning up keywords or phrases that are no longer needed.When facing hundreds or even more articles, manually modifying them one by one is obviously inefficient and prone to errors.AnQiCMS (AnQiCMS) fully understands the pain points of operators, and therefore built-in powerful "document keyword replacement" function, which can help us efficiently implement the batch modification of article content, of course, including the batch deletion of specified text.

2025-11-08