As an experienced CMS website operation personnel, I fully understand the importance of a clear and efficient template structure for content management and website maintenance. In AnQi CMS, the template engine provides a variety of auxiliary tags to help us better organize and reuse template code, among whichinclude/extendsandmacroThey are the three great tools to build flexible template architectures. Each one bears different responsibilities, but they all serve to enhance the maintainability and development efficiency of templates.
Modular content reuse:includetags
includeThe main function of the tag is to embed the content of another template file in a template file.Imagine your website has a universal header, footer, or sidebar that appears on almost every page.If you copy code every time, it is not only cumbersome, but also once you need to modify it, you have to change all pages one by one, which is time-consuming and labor-intensive and prone to errors.
PassincludeTags, we can extract these common modules into independent template files (for examplepartial/header.htmlThen simply include them in the pages where you need to display them.By default, the templates imported will inherit all variables from the current main template.withParameter; if you want it to use only specific variables without inheriting the entire context of the main template, you can useonlyParameter limitation.This approach promotes code modularization, making it efficient to reuse common content, greatly simplifying the daily maintenance and updates of the website.When a public component needs adjustment, we only need to modify a file, and all pages that reference it will be updated synchronously.
Define the page skeleton:extendstags
extendsTags are the foundation of the template inheritance system, allowing us to define a basic layout (or 'master template'), which other templates can then extend and fill in.This is like designing a master slide template when creating a presentation, from which all other slides inherit the basic style and layout, only needing to fill in the specific content.
In Anq CMS, we will create a page skeleton likebase.html[en]This basic template 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 useblockTag definition can be overridden or filled by sub-templates (for example{% block title %}or{% block content %})。Sub-templates{% extends 'base.html' %}declare it inherits frombase.html,then the same-namedblockLabel to overwrite or append the corresponding content defined in the parent template. If the child template does not overwrite a certainblockcontent, the default content of the parent template will be retained.
It is worth noting that,extendsThe label must be the first label in the sub-template, which specifies the inheritance relationship of the template.extendsThe advantage lies in its mandatory unification of the overall layout and style of the website, ensuring visual consistency, and also making it exceptionally simple to create new pages. We only need to focus on the unique content of the page, without having to rewrite the entire page structure.
Encapsulate dynamic components:macrotags
macroTags are more focused on defining parameterizable, reusable code snippets or functions, similar to functions in programming languages.macroAllow us to encapsulate an HTML structure with specific logic and parameters, and then call it multiple times at any position in the template, passing different parameters each time to render different content.
For example, if your website has a list of articles, each article has the same display style but different content (title, link, abstract, etc.), then you can encapsulate the rendering logic of a single article.macro. Definemacrowhen, we will specify a name and a set of parameters for it (for example,{% macro archive_detail(archive) %}...{% endmacro %}). When it is needed, we pass the data object as we call a function (for example,{{ archive_detail(item) }}). Unlike,include,macroIt has a stricter scope, accepting only explicitly passed parameters, thereby providing better encapsulation and isolation, and reducing the possibility of variable conflicts. Moreover,macroCan also be saved to a separate file (for example)archive.helper), then throughimportTags can be imported and used as needed in other templates.
Comprehensive application and differentiation
Understood the functions of these three tags, we can organize the template structure of the security CMS more effectively:
extendsUsed to build the macro layout of the website, determining the overall framework of the page.includeUsed to insert independent, general content segments in fixed layouts, such as page headers, footers, side navigation, etc. These contents usually do not change much or change little.macroIt is used to create dynamic, parametric UI components such as article cards, product list items, form input boxes, etc. These components have a fixed display logic, but the display data can vary.
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 content area of the page may call multiplemacroRender data lists or complex components. By reasonably utilizing these three, we can build a clear, efficient, easy to maintain and scalable safe CMS template system.
Common Questions and Answers (FAQ)
When should I choose to use it?includeInsteadmacro?
ChooseincludeIt is usually to reuse those relatively fixed and not frequently changed HTML fragments, such as the website's navigation bar, footer copyright information, or a pure static advertisement block that does not depend on external variables.includeInherits the entire context of the parent template, which is also convenient if the fragment needs a small amount of variables from the parent template.macroIt is more suitable for creating dynamic components that require different parameters to render different content. It provides stricter parameter isolation, avoids global variable pollution, and makes component reuse safer and more controllable.
2.extendsTagsblockWithincludeWhat is the difference between tags?
extendsthe tag inblockIt is used to define a region that can be filled or overwritten by child templates, focusing on the "holes" or "placeholders" of the overall page layout. Child templates rewrite theseblockto inject their own specific content, thereby forming a complete page.includeTags are instructions that directly insert the content of another template file at the current position, and they are more like 'copy and paste' a segment of code.extendsDefine the inheritance hierarchy and structure of the template,includeJust simply insert content segments, without involving inheritance relationships.
3. Can I call anothermacroin onemacroorincludea file?
Yes,macroIt has certain flexibility. You can call anothermacrothe definition internally calls another already defined or importedmacro, which helps to build more complex nested components. Similarly, you can also usemacroinincludeother template files as long asincludethe file does not depend onmacroThe variable that is not explicitly passed can be used. But remembermacroscope characteristics, any variable that is not passed through parameters,macroThe contents are invisible unless they are globally accessible context variables.