What are the core differences between `macro` tags and `include` tags in terms of variable scope and code reuse?

Calendar 👁️ 66

As an experienced website operations expert, I have a deep understanding of the powerful functions and content operation strategies of AnQiCMS (AnQi CMS).AnQi CMS, with its efficient architecture based on the Go language, flexible content model, and friendly SEO support, has become the preferred tool for many small and medium-sized enterprises and content operation teams.In template development, how to efficiently and safely reuse code is the key to improving operational efficiency, andmacrowith the tag andincludeLabels are the two great tools to achieve this goal.

However, many developers who are new to the Anqi CMS template often feel confused about the usage scenarios of these two tags, especially the core differences in variable scope and code reuse methods.Today, let's delve into the unique features of these 'twin brothers'.

includeLabels: 'Legos' for template assembly.

Imagine that your website has many pages, and they all share some common elements, such as the header (Header), footer (Footer), or sidebar (Sidebar).These parts of the code are often fixed or only slightly different in a few places. At this time,includeLabels are like pre-fabricated Lego blocks, allowing you to directly insert these independent functional modules into any required template file.

Its core idea is 'Direct Embedding': when you use{% include "partial/header.html" %}the system will convertheader.htmlThis file's content is loaded unchanged into the corresponding position of the current template. This method is concise and efficient, especially suitable for those static code blocks that are relatively stable and not subject to frequent changes.

In terms of variable scope,includeThe tag has a very important feature:By default, it will inherit all available variables in the current templateThis means that any data you define in the main templateincludecan be directly accessed in the incoming sub-template. For example, if your main template defines{{ siteName }}this website name variable, thenheader.htmlEven if it is not explicitly received, it can be used directly{{ siteName }}to display.

Of course, if you want to pass an additional, module-specific variable toincludethe template, you can usewithKeywords. For example, if you want to display a special title in the header of a page you are introducing, you can write it like this:{% include "partial/header.html" with pageTitle="安企CMS官网" %}. At this point,header.htmlIt can be accessed through{{ pageTitle }}to get this value.

Further, if you worry about beingincludethe template accidentally accessing variables in the main template that you don't want it to know, or to maintain the purity of the code, you can also addonlyKeyword:{% include "partial/header.html" with pageTitle="安企CMS官网" only %}. This way,header.htmlcan only access throughwithExplicitly passed inpageTitleVariables, any other variables in the main template will not be visible. It's like setting up an independent power supply for the Lego bricks, only to light up its own bulbs.

macroLabel: Flexible Customized "Function Factory"

withincludeDirectly embedded is different,macroThe label is more like a 'template function' or 'code factory'.It allows you to define a parameterized, reusable code snippet that can then be used to generate different HTML content as needed.This is especially powerful when it is necessary to display different data in the same structure, such as each product card in a product list, each article summary in an article list.

macroThe key to the tag is itsStrict variable scope isolation. OnemacroMacros defined internally,Can only access variables explicitly passed through parameters. It does not automatically inherit any variables from the parent template, even if you define them in the main templatesiteName,macroIt cannot be used internally directly. This sandbox mechanism ismacroThe most distinctive feature of the tag, and the foundation for its high modularity and avoidance of variable conflicts.

For example, suppose you need to display an article list on multiple pages, with the same layout and style for each list item, but different content displayed for each article. In this case, you can define amacroRender a single article item:

{# 定义在单独的文件如:archive.helper中 #}
{% macro article_card(item) %}
<li class="article-item">
    <a href="{{ item.Link }}">
        <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
        <h5 class="article-title">{{ item.Title }}</h5>
        <p class="article-description">{{ item.Description|truncatechars:100 }}</p>
        <span class="article-date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
    </a>
</li>
{% endmacro %}

Then in your main template, you need to useimportto include this macro, and then use it like a function:

{# 在主模板中引入宏文件 #}
{% import "archive.helper" article_card %}

{# 调用 archiveList 标签获取文章数据 #}
{% archiveList archives with type="list" limit="6" %}
    <ul class="article-list">
        {% for article in archives %}
            {# 将每篇文章数据 item 传入 macro 进行渲染 #}
            {{ article_card(article) }}
        {% endfor %}
    </ul>
{% endarchiveList %}

here,article_cardThe macro only knows it throughitemThe data received as a parameter, it is not necessary to care about itarchivesHow variables are obtained, also not affected by any other variables in the main template. This clear division of responsibilities makes the template code easier to understand, maintain, and collaborate with the team.

The core difference: variable scope and code reuse philosophy

In summary,macroandincludeThe fundamental difference lies inVariable scopeandThe philosophy of code reuse.

  1. Variable scope:

    • include:By default, it shares all variables of the parent template. It can be accessed throughwithPass by value, throughonlyLimit to using the passed variables. Its scope is wider, more 'open'.
    • macro:Strict isolation. Can only access variables explicitly passed as parameters. Its scope is narrower and more "enclosed", eliminating the possibility of variable name conflicts.
  2. Code reuse method:

    • include:Focuses on "structural reuse". It embeds a complete or partial template file into another template, suitable for the overall layout of websites, fixed blocks (such as navigation, advertising slots), and so on.can be considered as modular static content.
    • macro:Focuses on 'functional reuse'. It defines a parameterizable logic snippet, like a function, that receives data and generates HTML, suitable for components that need to render the same structure repeatedly but with different data (such as list items, cards, form fields).Can be considered as a component of dynamic content.

In simple terms, when you need to insert an "independent HTML snippet" on a page, chooseincludeWhen you need to 'repeat generating the same HTML structure with different data' on the page, choosemacro.

Why are these differences important?

Understand and properly applymacroandincludeThe difference is crucial for content operators and template developers of AnQi CMS:

  • Improve code quality and maintainability: macroThe strict scope avoids variable conflicts, reduces the risk of introducing unexpected bugs, and has obvious advantages in large and complex templates or when working in teams.
  • Optimize the structure and readability of the template:A logically clear template is easier to understand and reduces the cost of later maintenance. UsemacroEncapsulate repeated rendering logic to make the main template focus more on data flow and component composition.
  • Enhance the flexibility and extensibility of the template: macroCan handle data in a parameterized manner, allowing the same macro to be applied to different data sources, greatly enhancing the reusability and adaptability of templates.When you need to adjust the style or logic of a component, you only need to modify the macro definition, and all the places that call the macro will update automatically.
  • Improve development efficiency:Avoids rewriting a large amount of similar code, generating complex content through simple parameter passing, greatly speeding up template development speed.

The reason why AnQi CMS provides two different template reuse mechanisms is to meet various development needs and scenarios.As operation experts, we know that in efficient content management, the neatness, maintainability, and flexibility of code are equally important. Make good use ofmacroandincludeCan it give wings to your Anqí CMS website template development, easily building high-quality websites that are professional and easy to manage.


Frequently Asked Questions (FAQ)

Q1: Can I define it inside theincludefile definition.macroThen use this in the parent template that includes this filemacro?

A1: No.macroThe definition must be passed throughimportThe tag must be explicitly introduced to use. If you define it in a file that isincludedefined inmacromacro only in theincludeVisible within the file (if it uses itself), or needs to be referenced by other templatesimportmerelyincludeA file does not make its internally definedmacroautomatically available in the parent template.

Q2: If myincludefile needs a specific variable, but I don't want it to inherit all the variables from the parent template, what should I do?

A2: That is exactlywithandonlyThe place where the keyword is used. You can write it like this{% include "partial/my_specific_component.html" with data=my_data only %}Thus,my_specific_component.htmlThe template can only access todataThis variable, will not inherit other properties from the parent template

Related articles

How can you correctly import and use multiple `macro` functions defined in different files?

As an experienced website operations expert, I am well aware of the importance of clean, efficient, and maintainable template code for the long-term operation of a website.In such a flexible and powerful content management system as AnQiCMS, fully utilizing the various functions of its template engine, especially the macro function (`macro`), can greatly enhance development efficiency and the quality of content display.Today, let's delve into how to巧妙ly import and utilize multiple `macro` functions defined in different files.

2025-11-07

How does a `macro` tag-defined code snippet receive and process external variables or parameters?

The AntQue CMS template tool: how to elegantly handle external parameters with `macro` tag?In AnQiCMS template development, to improve code reusability and maintainability, we often use some auxiliary tags, and one particularly powerful and worth in-depth exploration is the `macro` tag.It is like a function in a programming language, allowing us to define reusable code snippets and customize their behavior by passing in different parameters when needed. Today

2025-11-07

In AnQi CMS template, how to define a reusable custom function or code snippet (such as looping to render article list items)?

Behind the powerful functions of AnQi CMS, the flexible use of templates is the key to improving efficiency and ensuring website consistency.As an experienced website operations expert, I know that in daily work, we always hope to achieve 'efforts for twice the results'.Today, let's delve deeply into a topic that can greatly enhance the efficiency of template development and maintenance: how to define and use reusable custom functions or code snippets in Anqi CMS templates, especially for common scenarios like looping to render article list items.Why do we need reusable code snippets?In the digital age, content is king

2025-11-07

Does the `include` tag support dynamic filenames, i.e., deciding which template file to include based on the variable value?

## Unveiling the `include` tag of AnQi CMS: Can the dynamic template filename work?As a senior website operations expert, I have a deep understanding of the template mechanism of the content management system (CMS).In the daily use and deep customization of AnQiCMS (AnQiCMS), the flexibility and efficiency of the template are one of the key factors determining the success or failure of website operations.

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

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

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

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