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

As an experienced website operations expert, I am well aware that how to elegantly manage and reuse template code in a high-efficiency and flexible content management system like AnQiCMS is the key to improving project efficiency and maintainability. AnQiCMS, based on Go language and Django template engine syntax, provides us with powerful template tag functions, among whichmacroFunctions are the tools to achieve code reuse. However, when facing increasingly complex projects and team collaboration,macroThe problem of naming conflicts in functions may also sneak up, becoming a small obstacle on our path forward.

Today, let's delve into how to import in AnQi CMS.macroThe function sets an alias to cleverly avoid potential naming conflicts, making our template code clearer and more efficient.

macroFunction: The cornerstone of Anqi CMS template reuse.

in the AnQi CMS template world,macroFunctions play a crucial role, they are like parametric code snippets that allow us to encapsulate commonly used HTML structures or logic, and call them repeatedly in the places where we need them. Imagine if you need to display style-unified article cards on different pages of a website, or use similar display components in different content models (such as articles, products),macroIt can be put to good use. It greatly reduces the workload of rewriting code, making our templates more modular and easier to maintain.

Of Security CMSmacroFunctions are often defined in a separate template file, such aspartial/item_card.html. The file may contain definitions like this:

{# partial/item_card.html 文件中的宏定义 #}
{% macro item_card(item) %}
    <div class="item-card">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>{{ item.Description|truncatechars:100 }}</p>
        <span class="views">{{ item.Views }} 次浏览</span>
    </div>
{% endmacro %}

In the main template file, we can use{% import "partial/item_card.html" item_card %}Import and directly call thisitem_cardTo achieve unified display of content

Name challenge in large projects: why do we need aliases?

As the scale of the Anqi CMS project expands, the content model becomes increasingly rich, the team members increase, and the number of template files also rises. At this time,macroFunction naming conflicts are inevitable. Imagine that in a team, there may be multiple developers independently responsible for different modules, and they are accustomed to naming their content card macros for themselves.item_cardorrender_itemWhen these independently developed template files are integrated into the same project or when you introduce some excellent template components from outside, you may encounter the following problems:

  1. Direct conflict:Multiple macro functions have the same name, the system cannot distinguish which one should be called.
  2. Ambiguous semantics:Even if there is no direct conflict, ifitem_cardThe macro renders article information in the article module and product information in the product module, so when it is necessary to handle both types of content on a page, directly callitem_cardIt can be confusing, hard to discern its specific purpose at a glance.
  3. Difficult to maintain:As the project evolves, the responsibilities of macro functions may change, or new, functionally similar macros may need to be introduced.Without an alias, it may be necessary to refactor a large amount of code to avoid naming conflicts, which increases maintenance costs.

To solve these problems, Anqi CMS provides an elegant solution: for importedmacroSet function alias.

How to set alias for in AnQi CMSmacroSet function alias: Practical Guide

AnQi CMS Django template engine syntax allows us to importmacrofunction, usingasTo specify a local, unique alias for a keyword. It's like giving a popular celebrity a nickname, which we can use to refer to them in a specific context. This avoids confusion with other celebrities with the same name and also keeps the code clear and readable.

Assuming we have two macro files:partial/article_macros.htmlis defined incontent_cardto display the article, andpartial/product_macros.htmlare also defined in itcontent_cardto display the product. Their definitions may be as follows:

partial/article_macros.html:

{% macro content_card(article) %}
    <div class="article-card">
        <h4><a href="{{ article.Link }}">{{ article.Title }}</a></h4>
        <p>{{ article.CreatedTime|stampToDate:"2006-01-02" }}</p>
    </div>
{% endmacro %}

partial/product_macros.html:

{% macro content_card(product) %}
    <div class="product-card">
        <img src="{{ product.Thumb }}" alt="{{ product.Title }}" />
        <h5><a href="{{ product.Link }}">{{ product.Title }}</a></h5>
        <span class="price">¥{{ product.Price }}</span>
    </div>
{% endmacro %}

in our main template file (for exampleindex.htmlIn case you need to use both of thesecontent_cardAnd if you don't want them to conflict, we can import and set aliases like this:

{# 在 index.html 文件中导入并设置别名 #}

{# 从文章宏文件中导入 content_card 并重命名为 article_display_card #}
{% import "partial/article_macros.html" content_card as article_display_card %}

{# 从产品宏文件中导入 content_card 并重命名为 product_display_card #}
{% import "partial/product_macros.html" content_card as product_display_card %}

<h2>最新文章</h2>
{% archiveList articles with moduleId="1" limit="3" %}
    {% for article in articles %}
        {# 使用别名调用文章宏 #}
        {{ article_display_card(article) }}
    {% endfor %}
{% endarchiveList %}

<h2>推荐产品</h2>
{% archiveList products with moduleId="2" limit="3" %}
    {% for product in products %}
        {# 使用别名调用产品宏 #}
        {{ product_display_card(product) }}
    {% endfor %}
{% endarchiveList %}

In this way, we successfully gave two functions that are similar in function but of different originmacroa unique local name. When importing, we usecontent_card as article_display_cardsyntax to tell the template engine topartial/article_macros.htmlin the filecontent_cardMacros are recognized in the current template asarticle_display_cardSimilarly, product macros are also givenproduct_display_cardan alias. This way, no matter how the original macro name is, inindex.htmlwe can call them clearly and without conflict.