What are the different roles of the three auxiliary tags `include`, `extends`, and `macro` in template structure organization?

Calendar 👁️ 44

As an experienced AnQi CMS website operator, I am well aware of the importance of a clear and efficient template structure for content management and website maintenance. In AnQi CMS, the template engine provides various auxiliary tags to help us better organize and reuse template code, among whichinclude/extendsandmacroThey are the three great tools for building flexible template architecture. They each have different responsibilities, but they serve together to enhance the maintainability and development efficiency of the template.

Modular content reuse: includeTag

includeThe main function of the tag is to embed the content of another template file in a template file.Imagine that your website has a universal header (header), footer (footer), or sidebar (aside) that appears on almost every page.If you copy the code every time, it is not only繁琐, but also once it needs to be modified, you have to change all pages one by one, which is time-consuming and laborious and easy to make mistakes.

ByincludeLabel, we can isolate these common modules into independent template files such aspartial/header.htmlThen simply introduce them in the page where they need to be displayed.By default, the template that is introduced will inherit all variables from the current main template.If you need to pass specific data to the template being imported, you can usewithParameters; if you want it to use only specific variables without inheriting the entire context of the main template, you can useonlyLimit the parameters. This promotes the modularization of the code, making the common content reusable and greatly simplifying the daily maintenance and update of the website.When a public component needs to be adjusted, we only need to modify a file, and all the pages that reference it will be synchronized updated.

Define the page skeleton:extendsTag

extendsTags are the foundation for building a template inheritance system, allowing us to define a basic layout (or "master template"), which other templates can then extend and fill out based on.This is like designing a master slide template when making a presentation, where all other slides inherit its basic style and layout, only filling in specific content.

In AnQi CMS, we will create a likebase.htmlSuch a basic template, which includes the overall structure of the website, such as<html>/<head>/<body>tags, as well as fixed areas such as headers, footers, and main navigation. Within these fixed areas, we useblockThe tag defines an area that can be overwritten or filled by a sub-template (for example{% block title %}or{% block content %}). A sub-template declares it inherits from{% extends 'base.html' %}, then it can use the same namedbase.html.blockLabel to override or append the corresponding content defined in the parent template. If the child template does not override someblock, it will retain the default content of the parent template.

It should be noted that,extendsThe tag must be the first tag in the sub-template, which clarifies the inheritance relationship.extendsThe advantage lies in its mandatory uniformity of the overall layout and style of the website, ensuring visual consistency, while also making it exceptionally simple to create new pages, as we only need to focus on the unique content of the page without having to rewrite the entire page structure.

Encapsulate dynamic components:macroTag

macroTags are more focused on defining parameterizable, reusable code snippets or functions, which are similar to functions in programming languages.macroAllow us to encapsulate a piece of HTML structure with specific logic and parameters, and then call it multiple times at any position in the template. Each time, different parameters are passed in to render different content.

For example, if your website has a list of articles, each article has the same display style, but the content (title, link, abstract, etc.) is different, then you can encapsulate the rendering logic of a single article into amacro. DefinemacroWhen, we will specify a name and a series of parameters (for example{% macro archive_detail(archive) %}...{% endmacro %}). When needed, we pass the data object as if calling a function (for example{{ archive_detail(item) }}). And withincludeThe difference is,macroIt has a more strict scope, which only accepts explicitly passed parameters, thus providing better encapsulation and isolation, reducing the possibility of variable conflicts. Moreover,macroCan be saved to a separate file (for examplearchive.helperThen proceedimportTags can be imported and used as needed in other templates

Comprehensive application and differentiation

Understanding the roles of these three tags, we can organize the template structure of AnQi CMS more effectively:

  • extendsUsed to build the macro layout of the website, determining the overall structure of the page.
  • includeUsed to insert independent, general content segments in a fixed layout, such as page headers, footers, side navigation, etc., which usually do not change much or change little.
  • macroThese are used to create dynamic, parameterizable UI components, such as article cards, product list items, form input boxes, etc., whose display logic is fixed, but the displayed data is variable.

In actual development, they often work together. A basic template (extends) defines the page layout, which may include multiple imported common parts (include),while the page content area may call multiplemacroRender data lists or complex components. By reasonably utilizing these three elements, we can build a clear, efficient, maintainable, and scalable Anqin CMS template system.

Frequently Asked Questions (FAQ)

1. When should I choose to useincludeinstead ofmacro?

SelectincludeIt is usually to reuse those relatively fixed and unchanged HTML fragments, such as the website navigation bar, footer copyright information, or a purely static ad block that does not depend on external variables.includeIt inherits the entire context of the parent template, and it is also very convenient if the fragment needs a small amount of variables from the parent template. AndmacroIt is more suitable for creating dynamic components that need to accept different parameters to render different content, it provides stricter parameter isolation, avoids global variable pollution, and makes component reuse safer and more controllable.

2.extendslabel'sblockwithincludeWhat are the differences between tags?

extendsin the labelblockIt is used to define an area that can be filled or overwritten by a sub-template, focusing on the 'holes' or placeholders of the overall page layout. Sub-templates rewrite theseblockCome to inject your own specific content, thereby forming a complete page. AndincludeThis is an instruction that directly inserts the content of another template file at the current position, as if it were 'copy and paste' a piece of code.extendsIt defines the inheritance hierarchy and structure of the template, andincludesimply insert a content snippet without involving inheritance.

3. Can I call another onemacroin it?macroorincludeCan I call a file?

Yes,macroIt has certain flexibility. You can call onemacroThe definition internally calls another already defined or importedmacroThis helps to build more complex nested components. Similarly, you can also inmacroinincludeother template files, as long as they areincludethe file does not depend onmacroThe variable can be passed implicitly. But remembermacroscope feature, any variable not passed through parameters, inmacroInternally, they are invisible unless they are globally accessible context variables.

Related articles

How to define a temporary variable and perform assignment operations in a template?

Hello! As an experienced AnQi CMS website operator, I am very happy to be able to explain in detail how to define and assign temporary variables in AnQi CMS templates.This is crucial for achieving the flexibility, readability, and maintainability of templates, enabling us to organize and display website content more efficiently.In the Anqi CMS template system, we often encounter scenarios where we need to store data, calculate results, or prepare content for specific components locally.To meet these requirements, Anqi CMS provides a concise and powerful variable definition mechanism, mainly realized through two tags

2025-11-06

How to format a timestamp from a database into a readable date and time string?

As an experienced CMS website operation personnel of AnQi, I know that the content of the website should not only be rich, but also presented in a user-friendly manner.Time information is an important part of the content, but the timestamps (Unix timestamps) stored in the database are often just a string of numbers, which can be confusing to display directly.Therefore, formatting these timestamps into easily readable date and time strings is a key step in enhancing user experience and increasing content readability.

2025-11-06

How to use a `for` loop to iterate over data and handle `empty` data situations?

As an experienced CMS website operations personnel, I fully understand the importance of content in attracting and retaining users.Dynamic, responsive website content display is the key to improving user experience, and effectively handling data sets, especially when the data may be empty, is a basic skill in operation work.AnQiCMS (AnQiCMS) powerful template engine provides us with flexible tools to achieve these goals, where the `for` loop tag and its `empty` block are our powerful assistants for content display.

2025-11-06

How does the Anqi CMS template support `if`, `elif`, `else` logical judgments?

In Anqi CMS template design, conditional logic judgment is an indispensable part for realizing dynamic content display and complex layout control.As a website operator, I am well aware that flexibly using `if`, `elif`, `else` and other logical judgment tags can allow us to present a wide variety of page content based on different data states or business requirements, thereby enhancing user experience and the interactivity of the website.The Anqi CMS template engine supports syntax similar to the Django template engine, which allows users familiar with other mainstream CMS template languages to quickly get started

2025-11-06

How to use built-in filters (such as `truncatechars`, `upper`) to process and convert the data format of template variables?

As an experienced AnQiCMS website operation person, I am fully aware of the importance of content in attracting and retaining users.High-quality content not only requires careful creation, but also needs to be presented in an appropriate format and optimized to enhance user experience and readability.AnQi CMS, with its flexible Django template engine syntax, provides us with powerful built-in filter functions, allowing us to easily process and convert the data format of template variables.Next, I will give a detailed introduction on how to use the built-in filter of Anqi CMS

2025-11-06

What file extension should AnQiCMS template files use?

As an experienced AnQi CMS website operation personnel, I know that template files play a core role in building a flexible and efficient website.They not only carry the visual presentation of the website, but also serve as a bridge for content and user interaction.For Anqi CMS, understanding the composition and usage specifications of its template files is the foundation for efficient content management and website optimization.According to the official document of Anqi CMS, template files uniformly use the file extension `.html`.

2025-11-06

How should static resources (CSS/JS/images) be stored and referenced in the AnQiCMS template?

As a website manager who is deeply familiar with AnQiCMS operation, I know that proper management of website static resources is crucial for improving user experience and website performance.In AnQiCMS template development and maintenance, reasonably storing and referencing CSS, JavaScript, and images, etc., is the foundation for building an efficient and stable website.Below, I will elaborate on this key link.

2025-11-06

How to properly define and use variables in the AnQiCMS template?

As an experienced security CMS website operation person, I know that high-quality and clear content is the key to attracting and retaining users.In AnQiCMS template development, correctly defining and using variables is the foundation for dynamic content display and improving user experience.Below, I will elaborate on how to define and use variables in AnQiCMS templates, to help you better master content display.

2025-11-06