How to use the `macro` tag in AnQiCMS to define reusable content display components and improve template efficiency?

Calendar 👁️ 64

In AnQiCMS template development, we often encounter situations where we need to repeatedly write the same or similar code blocks, such as article cards on the website, product display blocks, or buttons with specific styles.This repeated code not only reduces development efficiency, but also makes subsequent maintenance and modification cumbersome. 幸运的是,AnQiCMS providedmacroTags, it allows us to define reusable content display components, managing template code as if writing small functions, thereby greatly enhancing template efficiency and maintainability.

What ismacroTag?

macroTags can be understood as 'micro-functions' or 'components' in templates.It allows you to define a template code snippet with parameters.When you need to use this code multiple times, you just need to pass different parameters, like calling a function,macroIt will generate the corresponding HTML content based on these parameters.This is like defining a function in programming to perform a specific task, and calling the function directly each time the task is needed without rewriting all the logic.

Define and usemacrofundamentals

definitionmacroThe syntax is very intuitive:

{% macro 宏名称(参数1, 参数2, ...) %}
    {# 这里是宏要输出的HTML代码 #}
    {# 可以使用传入的参数来动态生成内容 #}
{% endmacro %}

Once defined,macroYou can use it anywhere in the template, just like calling a regular variable and passing the corresponding parameter value:

{{ 宏名称(值1, 值2, ...) }}

It is worth noting that,macroThe scope is independent, it can only access variables passed in through parameters. This meansmacroMore pure, it will not unexpectedly modify or depend on other variables in the external environment, thereby enhancing its reusability and stability.

Actual case: Build a reusable article card component

Assuming our website homepage, category list page, and search results page all need to display article summaries, each summary includes the title, thumbnail, description, and link. If there is nomacroWe may need to rewrite the similar article card HTML structure on each page. WithmacroWe can operate like this:

  1. Create a macro fileTo better organize the code, we can put allmacroDefine separately in a file, for example, in/template/你的模板目录/partial/Create a folder and name it_macros.html.

    In_macros.htmlDefine an article card inmacro:

    {# partial/_macros.html #}
    {% macro article_card(article) %}
        <div class="article-card">
            {% if article.Thumb %}
                <a href="{{ article.Link }}" class="card-thumb">
                    <img src="{{ article.Thumb }}" alt="{{ article.Title }}">
                </a>
            {% endif %}
            <div class="card-content">
                <h3 class="card-title">
                    <a href="{{ article.Link }}">{{ article.Title }}</a>
                </h3>
                <p class="card-description">{{ article.Description|truncatechars:100 }}</p>
                <div class="card-meta">
                    <span>发布于:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
                    <span>阅读:{{ article.Views }}</span>
                </div>
            </div>
        </div>
    {% endmacro %}
    
    {# 如果还有其他类似的宏,可以继续在这里定义,比如 product_card 等 #}
    
  2. Include and use in the main templatemacroNow, whether it isindex.html/article/list.htmlOrsearch/index.htmlThrough, we can all passimportIntroduce and use this tagarticle_cardmacro.

    For example, inindex.htmlIn:

    {# index.html #}
    {% extends 'base.html' %}
    {% import "partial/_macros.html" article_card %} {# 引入 article_card 宏 #}
    
    {% block content %}
        <div class="articles-section">
            <h2>最新文章</h2>
            <div class="article-list-grid">
                {% archiveList latestArticles with type="list" limit="6" order="id desc" %}
                    {% for article in latestArticles %}
                        {{ article_card(article) }} {# 直接调用宏,传递文章数据 #}
                    {% empty %}
                        <p>暂无最新文章。</p>
                    {% endfor %}
                {% endarchiveList %}
            </div>
        </div>
    
        {# 假设另一个地方也需要文章卡片 #}
        <div class="featured-articles">
            <h2>推荐阅读</h2>
            <div class="article-list-flex">
                {% archiveList featuredArticles with type="list" flag="c" limit="3" %}
                    {% for article in featuredArticles %}
                        {{ article_card(article) }} {# 再次调用宏 #}
                    {% empty %}
                        <p>暂无推荐文章。</p>
                    {% endfor %}
                {% endarchiveList %}
            </div>
        </div>
    {% endblock %}
    

In this way, we define the structure and style of the article card only once, but can use it repeatedly in many places. If we need to modify the layout of the article card or add new display elements in the future, we only need to modify_macros.htmlofarticle_cardMacros, all references to it will be automatically updated, which greatly saves the cost of modification.

Further progress: organizationmacroFile

In a large website project, it may be defined many.macro. To keep the template file neat and easy to manage, it is usually a good practice to place different functionsmacroin different files. For example:

  • _article_macros.html: Contains macros related to article display.
  • _product_macros.html: Stores macros related to product display.
  • _form_macros.html: Stores macros related to form elements (such as input boxes, selection boxes).
  • _utility_macros.html: Stores some general utility macros.

When needed, you can introduce one or more macros based on specific requirements, even throughasThe keyword is used to set aliases for the introduced macros to avoid naming conflicts or to make the call more contextual:

{# 引入多个宏,并为其中一个设置别名 #}
{% import "partial/_macros.html" article_card, product_card as p_card %}

{{ article_card(some_article) }}
{{ p_card(some_product) }}

macroBenefits brought

usemacroTag-based template development can bring many advantages to our website:

  • improve development efficiency: Write once, use anywhere, avoid repetitive work, and accelerate development progress.
  • Maintain code consistencyAll repeated components use the samemacroGenerated, ensuring the overall style consistency and interface standardization of the website.
  • Simplify template maintenanceWhen the website design needs to be adjusted, it only needs to be modifiedmacroDefine the file, all pages referencing the component will be automatically updated, greatly reducing maintenance costs
  • Improve the readability of the template: Encapsulate complex HTML structures inmacro, making the main template code more concise, clear, easy to understand and review.
  • Promote teamwork: Team members can focus on their respectivemacroComponent development, then throughimportTags integrated in the main template, improving collaboration efficiency.

On the whole, AnQiCMS'smacroTags are a powerful tool in template development, which helps us achieve componentization and modularization of templates in a simple and effective way, making website construction more efficient and organized. By making reasonable use ofmacroWe can say goodbye to repetitive and chaotic template code, thus having more energy to focus on content creation and website operation itself.


Frequently Asked Questions (FAQ)

1.macroandincludeWhat are the differences between tags?

macroandincludeAll are used for code reuse in templates, but they have different focuses and behaviors.

  • macro(Macro)It is more like a 'function'. It accepts parameters and has an independent internal scope, accessible only through the data passed in as parameters. This makesmacroVery suitable for creating configurable, logically structured UI components (such as article cards, buttons), which can generate different content each time they are called according to the parameters passed in.
  • include(including)It is more like a simple 'copy and paste'. It will directly insert the content of the specified file at the current position.includeThe default inherits all context variables of the current template. It is often used to introduce fixed parts in the page (such as headers, footers, sidebars), which usually do not change much or need to access all the data of the current page.

In simple terms,macroIt is a good choice to create 'smart components', andincludeSuitable for inserting a "static segment".

2.macroWhat types of data can the parameter pass?

macroThe parameter is very flexible, it can pass almost all data types supported in the AnQiCMS template environment, including but not limited to:

  • string(String): such as{{ macro_name("这是一个字符串") }}.
  • Number(Integer/Float):such as{{ macro_name(123) }}or{{ macro_name(3.14) }}.
  • Boolean(Boolean):such as{{ macro_name(true) }}or{{ macro_name(false) }}.
  • Object/Structure(Object/Struct):This is the most common way, you can directly pass a complete article object or category object, such as{{ article_card(item) }}.
  • array/slice(Array/Slice):Can pass a list of data.

**3. Can I use other AnQiCMS tags and filtersmacrowithin it?

Related articles

How does AnQiCMS implement template inheritance through the `extends` tag to simplify the maintenance of page structure display?

In website operation and content management, maintaining a clear, unified, and easy-to-maintain website structure is an important challenge.Especially when there are many pages on a website, repeated page elements (such as headers, footers, navigation bars) would undoubtedly require a lot of time and effort to modify one by one.AnQiCMS (AnQi CMS) is precisely through its powerful template inheritance mechanism, especially the use of the `extends` tag, that helps us efficiently solve this problem.

2025-11-09

How to reuse the code for displaying the common header or footer module in AnQiCMS templates through the `include` tag?

In AnQiCMS template design, efficient code reuse is the key to improving development efficiency and maintenance convenience.Faced with commonly used common elements on the website, such as headers, footers, navigation bars, sidebars, etc., if each page is rewritten repeatedly, it is not only time-consuming and labor-intensive, but also once modified later, it is a problem that affects the whole body.AnQiCMS is well-versed in this, providing users with a powerful template mechanism, where the `include` tag is the tool to achieve this goal.

2025-11-09

How does AnQiCMS's `lorem` tag assist in generating placeholder text for page display testing in template development?

During the development and testing phase of a website template, we often encounter a tricky problem: how to accurately evaluate the layout, style, and visual performance under different content lengths without real content?Manually filling in dummy data is not only inefficient but also difficult to simulate the richness of real content.AnQiCMS (AnQi CMS) provides a very practical auxiliary tag for template developers - `lorem`, which can easily generate placeholder text and greatly simplify the work of page display testing.### `lorem`

2025-11-09

How to control the display and SEO impact of links in the AnQiCMS friend link list according to the `nofollow` attribute?

In website operation, friend links are one of the important ways to improve website weight and expand external link resources.However, as search engine algorithms continuously evolve, how to finely manage these links, especially the application of the `nofollow` attribute, has become a topic that web administrators cannot ignore.AnQiCMS as a content management system focusing on SEO optimization, provides users with flexible link management functions, allowing you to effectively control the display and SEO impact of links.###

2025-11-09

How does the `ContentTitles` tag in AnQiCMS help generate and display the table of contents on the article detail page?

In daily content creation and website operation, we often encounter long articles with a large amount of information.This kind of article is rich in content, but without a clear navigation structure, readers are easily lost in the ocean of text and find it difficult to quickly locate the part they are interested in.This not only affects the reading experience, but may also reduce the effective communication of the content.

2025-11-09

How to add categories for image resources in AnQiCMS and manage the front-end display according to categories?

In AnQiCMS (AnQiCMS), effectively organizing and managing your website image resources is the key to improving operational efficiency and front-end display quality.The image classification function not only makes the background management work orderly, but also provides a flexible foundation for the presentation of front-end page content.Below, let's take a detailed look at how to add categories for image resources in AnQiCMS and cleverly manage the display on the front end.

2025-11-09

How does AnQiCMS automatically filter or process external links in article content and control their display on the front end?

In content operation, properly handling external links in article content is an important link, it not only concerns the health of the website's SEO, but also affects the reading experience of users.AnQiCMS provides a flexible mechanism to help users automatically filter or process external links, and supports control over the front-end display methods. ### Core Function: Automatic Handling Strategy for External Links AnQiCMS provides convenient settings in the background, allowing users to manage external links in article content according to their own operational strategies.

2025-11-09

How to use the `truncatechars` or `truncatewords_html` filter to truncate text or HTML content and add an ellipsis for display?

When operating a website, we often encounter situations where we need to display long content, but the page space is limited.For example, on the article list page, we usually only want to display the abstract of the article; on the product detail card, we may only want to show a brief description.If this content is not processed, it may overflow the container, destroy the page layout, and affect the user experience.The AnQi CMS knows the importance of content display and provides us with powerful template functions.

2025-11-09