What are the application scenarios of AnQiCMS template macro functions (macro) and include tags?

Calendar 78

In AnQiCMS template development, in order to improve code reuse and reduce maintenance costs, we often use some powerful template features, among which the 'macro function (macro)' and 'Include tag' are two powerful assistants.They each have unique advantages and application scenarios. Understanding and appropriately using them can make template development twice as fast and build efficient, easy-to-maintain websites.

Macro: Encapsulates reusable dynamic logic

Macros functions in template development are like functions in programming languages.We can define a reusable code snippet and specify its parameters.When needed, just 'call' it and pass the corresponding parameters, it will render different content based on the data passed in.Macros have their own context, which means they only focus on the parameters passed in, do not accidentally modify or depend on external variables, making the code more robust and predictable.

In AnQiCMS, the application scenarios of macro functions are very extensive, especially suitable for those UI components with similar structures but dynamic content.

Application scenarios:

  • Dynamic list item rendering:Imagine a website with common lists such as articles, product lists, user comments, and so on.Each list item's HTML structure may be very similar (for example, they all contain titles, descriptions, links, and images), but the specific data displayed by each item is completely different.article_item(article)Receiving onearticleAn object is passed as a parameter. When iterating over article data, you only need to call this macro function, passing in the current article's data, to generate the corresponding list item HTML. This is useful for handling things likearchiveList/categoryList/pageListThis tag returns data very efficiently. For example, in the document.tag-tags.mdIt shows how to define and usearchive_detail(archive)macros to render article list items.
  • The encapsulation of complex UI components:When a smaller UI unit, such as a comment section header with user avatar and nickname, or a statistics display module, whose rendering logic includes complex conditional judgments or data processing, encapsulating it as a macro function can effectively reduce code redundancy and improve readability.
  • Configurable general module:If the website has a general module that needs to adjust the rendering based on different parameters (such as display quantity, whether to display thumbnails, sorting method, etc.), for example, "Latest Articles RecommendationBy using macro functions, these parameters can be received to achieve flexible configuration and reuse, without the need to write a new HTML for each configuration.
  • Extracted to a separate file for management:AnQiCMS allows the definition of macro functions to be stored in a separate template file (such asarchive.helper)Among them. When the main template needs to use these macros, it can be done throughimportLabel introduction. This method helps to keep the main template tidy and allows macro function definition files to focus on encapsulating reusable logic, enhancing the maintainability of the template structure.

Include tag: Include static or semi-static page fragments

The Include tag has a relatively direct function, it is used to insert the content of another template file directly into the current position. You can understand it as a simple text replacement operation: the template engine will embed the content of the specified file unchanged intoincludeThe location of the tag. The file being imported can access all variables of the current template, and it can alsowithThe keyword can receive specified variables, and can even be usedonlyKeyword limits access to specified variables only.

In the AnQiCMS template structure,includeTags are the basis for constructing the page skeleton and realizing modular layout.

Application scenarios:

  • Website common structure:This isincludeThe most classic usage of the label.The website's header (header), footer, sidebar (aside), global navigation menu, etc., these contents are fixed or change very little throughout the website.bash.html(Headers, footers, and other common parts) andpartial/The catalog (code snippets), is designed toincludeDesign. Divide these common parts into independent files, and then throughincludeIntroducing tags on all pages can greatly improve development efficiency and ensure the consistency of the overall website style.
  • Common static or semi-static code snippets:For example, a unified breadcrumb navigation (tag-breadcrumb.mdTags can generate data, but the rendering of HTML structure can be usedincludeEncapsulation), website copyright statement, unified CSS or JavaScript inclusion blocks, fixed advertisement placement code, or contact information obtained from the backend settings (tag-contact.mdLabel data retrieval) and system information (tag-system.mdThe rendering module of label data retrieval). These contents usually do not change much, throughincludeIntroduction can facilitate management and reuse.
  • Modular page layout:For complex pages, different areas (such as the "Product Parameters" area and the "Customer Reviews" area on the product detail page) can be divided into independent template files. Then, they can be used in the main template.includeLabel these modules as needed. This allows each module to be developed and maintained independently, improving the modularization and readability of the page.

How to choose: Macro function vs Include tag

The focus is on understanding macro functions and Include tags in AnQiCMS template development:

  • SelectIncludeWhen:You need to insert a relatively fixed HTML structure, or a complete, independent page fragment.The content mainly comes from the file itself, rather than generated dynamically through parameters.StructuredandFile combinationFor example, the header and footer usually change little, directlyincludeJust do it.
  • SelectMacroWhen:You need to encapsulate a reusable rendering logic, this logic needs to receive different data or configuration parameters to generate different HTML outputs. It is more towardsfunctionalizationandDynamically generatedFor example, each item in the article list, although the structure is fixed, the content changes with the article, usingmacropassing in article data to render is very appropriate.

In actual development, both are not exclusive, but are often used together. You canincludea file containing multiple macro definitions, and then call these macros in the main template. Or, aincludewithin the file, may also call othermacroto render the dynamic part inside. This combination is used

Related articles

How to implement page skeleton reuse through inheritance in AnQiCMS templates?

In website construction and operation, we often face a challenge: how to ensure the consistency of the overall style of the website while being able to flexibly modify the specific content of each page, and at the same time, taking into account the development efficiency and the convenience of later maintenance?AnQiCMS provides a powerful and intuitive template inheritance mechanism, which is the core of solving these problems.It's like creating a "template" for your website, allowing you to easily reuse common structures and focus on content creation.

2025-11-07

How to include external HTML fragments in AnQiCMS templates?

When building website templates in AnQiCMS, we often encounter some HTML code blocks that need to be reused, such as the website header, footer, sidebar navigation, advertisement slots, or some general content modules.If each time you repeat writing these codes in different template files, it is not only inefficient but also very麻烦 to find and update them one by one when modifications are needed.

2025-11-07

What is the difference between the variable output and logic control tags in AnQiCMS templates?

In AnQiCMS template development, we often deal with two core template elements: variable output tags and logical control tags.They all serve to present dynamic content to the user, but there are essential differences in their functional positioning, syntax form, and actual effects.Understanding and mastering their similarities and differences is the key to efficiently building AnQiCMS website templates.### Variable output label (`{{ ... }}`): The direct window for data display Imagine that your website is a beautiful showcase, and the variable output label is like the merchandise displayed in the showcase

2025-11-07

How to write AnQiCMS templates according to Django template engine syntax rules?

AnQiCMS (AnQiCMS) is an efficient and customizable content management system, with its powerful template engine being the core for achieving diverse website presentations.The template engine adopted by the system is highly similar to Django's syntax rules, which allows users familiar with web development to get started faster, even beginners can gradually master the skills of template writing through the guidance of this article.

2025-11-07

How to display the current time and formatted timestamp in AnQiCMS template?

Displaying time on the website content, whether it is the publication date of the article, the update time of the product, or the real-time loading time of the page, can greatly enhance the user experience and the timeliness of information.AnQiCMS is a content management system developed based on the Go language, which is very intuitive and flexible in handling time and dates in templates.This article will introduce you to how to display the current time and formatted stored timestamp in AnQiCMS templates.

2025-11-07

How to customize variables and assign values using (with/set) in AnQiCMS templates?

In AnQiCMS templates, flexibly customizing and using variables is the key to achieving dynamic content and efficient template reuse.AnQiCMS's template syntax borrows the characteristics of popular template engines such as Django and Blade, making variable definition and assignment intuitive and powerful.This article will deeply explore the `with` and `set` tags, helping you to better customize variables and utilize them in AnQiCMS templates.### One, the foundation of variable customization: Understanding the core of template syntax In AnQiCMS template

2025-11-07

How to use the `archiveList` tag to display the latest article list on the AnQiCMS homepage?

On AnQiCMS, the homepage is often the first window visitors use to understand the website's content.How to efficiently and dynamically display the latest articles to attract visitors to continue browsing is the key to content operation.AnQiCMS provides a flexible and easy-to-use template tag system, where the `archiveList` tag is the core tool to achieve this goal.

2025-11-07

How to retrieve the list of articles under a specified category and implement pagination in AnQiCMS?

In website content operation, we often need to display articles under specific categories and provide pagination for these lists to enhance user experience and website loading efficiency.AnQiCMS (AnQiCMS) with its flexible template engine and powerful tag system can easily meet this requirement. AnQiCMS template system adopts syntax similar to Django template engine, using double curly braces `{{variable}}` to reference variables, as well as single curly braces with a percent sign `{% tag %}` to call tags.This makes the customization of content display very intuitive

2025-11-07