`macro` tag definition code snippet can include other built-in template tags of Anqi CMS?

Calendar 👁️ 67

As an experienced website operations expert, I am well aware of the importance of a powerful and flexible content management system (CMS) for corporate websites.AnQiCMS (AnQiCMS) leverages its high-performance architecture based on the Go language and Django template engine syntax, providing great convenience for content operations.In daily content management and website maintenance, we often use the built-in template tags to build dynamic pages.Today, I want to delve deeply into a topic about the practical but easily confusing aspect of Anqi CMS template development: macroCan the code snippet defined by the tag include other built-in template tags of AnQi CMS?


AnQi CMS template: In-depth analysismacroThe nesting capability and application strategy of tags

In the Anqi CMS template system, we will mainly encounter two types of structures: one is the double curly brace variable used for output data, such as{{变量}};Another is used for implementing logical control, loop traversal, and even data acquisition for more complex tasks, such as{% 标签 %}。AnQi CMS provides a rich set of built-in tags, ranging from obtaining system configurations(system), navigation lists(navList) to document details(archiveDetail), document lists(archiveListThese tags greatly simplify the process of building page content.

AndmacroA tag, as a member of the Anqi CMS template auxiliary tag, its core value lies in helping us define reusable code snippets or functions. It is like a function in a programming language, which can encapsulate a repeated HTML structure and the corresponding variable logic, and can be called in the place where it is needed.macroTo render, it avoids code redundancy, improves the maintainability and development efficiency of templates. For example, if we want to define a unified display style for each article list item,macroCan be put to use. The classic usage given in the document is to define it as receiving parameters, as shown in the following code snippet, which accepts aarchiveobject as a parameter and uses it internally{{archive.Id}}and{{archive.Title}}Show the article ID and title.

{% macro archive_detail(archive) %}
<li class="item">
    <a href="/archive/{{archive.Id}}" class="link">
        <h5 class="title">{{archive.Title}}</h5>
    </a>
</li>
{% endmacro %}

Then,macroCan the tag inside contain other built-in template tags of AnQi CMS? This is a key issue that directly relates to how we design and organize templates.

Macro-defined code snippets should not generally contain built-in template tags used for independent data retrieval, such asarchiveList/system/categoryListetc.This is becausemacroThe design philosophy of the tag is to provide an encapsulated rendering logic, which expects to receive the required data from the outside, rather than initiating new data queries internally or obtaining global system configurations. According to the conventions of the Anqi CMS template tag,macroThe function “can only call variables passed as parameters” clearly defines its scope and data source.

This means that when you try to call in amacrodirectly.{% archiveList archives with limit="5" %}When trying to retrieve the latest list of articles, it usually fails. The macro's internal environment is relatively independent, and it does not have the ability to directly access the entire application context to perform such data queries.

However, this does not meanmacroThere should be no tags inside. In fact, it can completely contain conditional judgment tags ({% if ... %}), loop iteration tags ({% for ... %}), and even auxiliary tags likeincludeThe data for these label operations are all derived from the parameters of the macro itself, or from global variables available in the template inheritance chain, but do not require additional queries within the macro.

For example, if you want toarchive_detailin a macro, based onarchivea property of an object (such asarchive.HasImage), to decide whether to display an image, you can do this:

{% macro archive_detail(archive) %}
<li class="item">
    <a href="/archive/{{archive.Id}}" class="link">
        {% if archive.HasImage %}
            <img src="{{archive.Thumb}}" alt="{{archive.Title}}" />
        {% endif %}
        <h5 class="title">{{archive.Title}}</h5>
    </a>
</li>
{% endmacro %}

here,{% if archive.HasImage %}and{{archive.Thumb}}all depend on the inputarchiveThe parameter belongs to the parameter scope and is therefore completely legal.

**Practice and Suggestions

In order to make better use ofmacroLabel and maintain the clarity and efficiency of template code, we should follow the principle of 'Data and Display Separation':

  1. Data acquisition is external, rendering logic is inmacro.IfmacroNeed to display a specific list of articles, system configuration, or category information, which should be accessed by callingmacrothrough the parent template.archiveList/system/categoryListtags. The data obtained is then passed as a parameter tomacro.

    • Incorrect example (it is not recommended to perform data retrieval directly within a macro)
      
      {% macro latest_articles_macro() %}
          {% archiveList latest_articles with limit="5" %}
              {% for article in latest_articles %}
                  <li>{{ article.Title }}</li>
              {% endfor %}
          {% endarchiveList %}
      {% endmacro %}
      
    • Correct approach (retrieve data externally and pass it to the macro for rendering)
      
      {# template.html #}
      {% archiveList latest_articles with limit="5" %}
          {% import "macros.html" as my_macros %}
          {{ my_macros.render_article_list(latest_articles) }}
      {% endarchiveList %}
      
      {# macros.html #}
      {% macro render_article_list(articles) %}
          <ul>
              {% for article in articles %}
                  <li>{{ article.Title }}</li>
              {% endfor %}
          </ul>
      {% endmacro %}
      
  2. macroFocus on receiving parameters and structuring rendering.It should focus on how to display data consistently, rather than where to get the data. This makesmacroMore generic and reusable, can be used in different

Related articles

When it is necessary to dynamically generate HTML structures based on data, can the `macro` tag effectively simplify template logic?

In an efficient and customizable Go language content management system like AnQiCMS, the flexibility and maintainability of the template level have always been the focus of developers and operators.Especially when facing the need to dynamically generate complex HTML structures based on background data, we often think about how to effectively simplify template logic while ensuring rich and diverse content, and avoid code redundancy?Today, let's delve deeply into a powerful auxiliary tag in AnQi CMS, `macro`, and see if it can be used in the generation of dynamic HTML structures

2025-11-07

Can the `macro` function call other already imported or defined `macro` functions within it?

## The Mystery of Nested Macro Function Calls in AnQiCMS Templates: Building Efficient and Maintainable Front-end Code In the daily operation and template development of AnQiCMS (AnQiCMS), we often make use of its powerful and flexible template engine to construct dynamic content.Among them, the `macro` function has become a powerful tool for front-end developers due to its code reuse capability.It allows us to define reusable code snippets, like functions that accept parameters and return rendered HTML.

2025-11-07

What are the advantages of using the `macro` tag when building complex or nested UI components compared to writing logic directly on the page?

In modern website operations, efficiency and flexibility are the key to success.When it comes to building a feature-rich, complex website, how to efficiently manage and maintain a large number of UI components is a great challenge facing website operators and front-end developers.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, deeply understanding this field, its powerful template engine and various auxiliary tags are exactly designed to solve these pain points.

2025-11-07

How to set an alias for the imported `macro` function to avoid naming conflicts in large projects?

As an experienced website operation expert, I am well aware that how to elegantly manage and reuse template code in an efficient and flexible content management system like AnQiCMS is the key to improving project efficiency and maintainability.The AnqCMS based on Go language and Django template engine syntax provides us with powerful template tag functions, among which the `macro` function is a tool for code reuse.

2025-11-07

The `macro` tag provides what conveniences in template debugging and error troubleshooting?

## Debugging and Troubleshooting of AnQi CMS Templates: The Unsung Hero of `macro` Tags In the fast-paced digital age, the stable operation and efficient iteration of websites are the foundation of operational success.AnQi CMS is an enterprise-level content management system developed based on the Go language, with its high performance, high concurrency features, and flexible template system, providing solid technical support for small and medium-sized enterprises and content operators.However, even the most powerful system is bound to encounter some tricky debugging problems during the development and maintenance of actual templates. Today

2025-11-07

In AnQi CMS template, how to create a basic layout skeleton (master template) for all pages to inherit?

## Advanced AnQi CMS Template: The Art and Practice of Building an Inheritable Basic Layout Skeleton (Master Page) Efficiency and consistency are two core elements in the daily operation of website management.Imagine if every page of a website needed to be individually designed and maintained for navigation bars, footers, and header Meta information, it would be a time-consuming and error-prone task.This is one of the problems that excellent content management systems like AnQiCMS (AnQiCMS) are committed to solving.

2025-11-07

The `extends` tag must be placed at which position in the template file to work and parse correctly?

AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, which provides strong support for content operations with its efficient and flexible features.In the process of template development, proficiently using its built-in Django style template engine is the key to improving efficiency.The `extends` tag is a powerful tool for implementing template inheritance and building a unified website layout.However, for this powerful feature to work properly and be correctly parsed by the template engine, its placement has strict conventions.

2025-11-07

How to overwrite a specific content area or `block` in a child template inheriting from a parent template?

Advanced Anqi CMS Template: Flexibly Rewrite Specific Content Areas of Parent Templates As an experienced website operations expert, I fully understand the importance of a flexible and efficient content management system for corporate operations.AnQiCMS (AnQiCMS) with its high-performance architecture based on the Go language and the Django-style template engine, has provided us with great convenience.In daily content operations, we often need to maintain a consistent style of the website, but on certain pages, we also need the local content to be different.

2025-11-07