As a senior website operations expert, I know the importance of a powerful and flexible content management system (CMS) for a corporate website in English.AnQiCMS (AnQiCMS) provides great convenience for content operation with its high-performance architecture based on Go language and Django template engine syntax.In daily content management and website maintenance, we often use its built-in template tags to build dynamic pages.macroIs the code snippet defined by the tag allowed to include other built-in template tags of AnQi CMS?
AnQi CMS Template: In-depth AnalysismacroNesting Ability and Application Strategy of Tags
In the template system of AnQi CMS, we mainly encounter two types of structures: one is used to output data with double curly braces variables, such as{{变量}}Another is the single curly brace percentage tag used to implement more complex tasks such as logical control, loop iteration, and data acquisition,{% 标签 %}。English CMS provides a rich set of built-in tags, from obtaining system configuration (system)、navigation list (navList)to document details (archiveDetail)、document list (archiveListThese tags greatly simplify the process of building page content.
whilemacroTags, as a member of the auxiliary tags of the Anqi CMS template, its core value lies in helping us define reusable code snippets or functions. It is like a function in a programming language, which can encapsulate a repeated HTML structure and the corresponding variable logic, and call it in the place where it is needed.macroRender automatically, avoiding code redundancy and improving the maintainability and development efficiency of templates. For example, if we want to define a unified display style for each item in the article list,macroIt can be put to use. The classic usage given in the document is to define it in the form of receiving parameters, as shown in the following code snippet, which accepts an object as a parameter and uses it internally.archiveobject as a parameter and uses it internally.{{archive.Id}}and{{archive.Title}}Show the article ID and title.
{% macro archive_detail(archive) %}
<li class="item">
<a href="/archive/{{archive.Id}}" class="link">
<h5 class="title">{{archive.Title}}</h5>
</a>
</li>
{% endmacro %}
So,macroIs it possible to include other built-in template tags of Safe CMS inside the label? This is a critical issue, directly related to how we design and organize templates.
Code fragments defined by macros, in general, should not directly contain built-in template tags used for independent data retrieval,archiveList/system/categoryListetc.This is becausemacroThe design concept of the label is to provide an encapsulated rendering logic, which expects to receive the required data from the outside, rather than initiating new data queries or obtaining global system configurations internally. According to the conventions of the Anqi CMS template labels,macroThe function 'Can only call variables passed in as parameters' explicitly limits its scope and data source.
This means that when you try to call a functionmacrodirectly within a function{% archiveList archives with limit="5" %}When trying to retrieve the latest list of articles, it usually fails. The macro's internal environment is relatively independent, and it does not have the ability to directly access the entire application context to perform such data queries.
However, this does not meanmacrothe internal structure cannot contain any tags. In fact, it can completely include conditional judgment tags ({% if ... %})、loop iteration tags ({% for ... %}),or even auxiliary tags likeincludeThe data for these label operations are all derived from the parameters of the macro itself, or global variables available in the template inheritance chain, but do not require additional queries within the macro.
For example, if you want toarchive_detailin a macro, based onarchivean attribute of an object (such as)archive.HasImage) to decide whether to display an image, you can do it like this:
{% macro archive_detail(archive) %}
<li class="item">
<a href="/archive/{{archive.Id}}" class="link">
{% if archive.HasImage %}
<img src="{{archive.Thumb}}" alt="{{archive.Title}}" />
{% endif %}
<h5 class="title">{{archive.Title}}</h5>
</a>
</li>
{% endmacro %}
Here,{% if archive.HasImage %}and{{archive.Thumb}}all depend on the inputarchiveParameters, belonging to the parameter scope, are completely legal.
[en] Practical Tips
To better utilizemacroTags and maintaining the clarity and efficiency of template code, we should adhere to the principle of 'separation of data and presentation':
Data retrieval is external, rendering logic is in
macro.IfmacroSpecific article lists, system configurations, or category information need to be displayed, and these data should bemacropassed through the parent template in the callarchiveList/system/categoryListEnglish tags. The data obtained is then passed as a parameter tomacro.Error example (it is not recommended to execute data retrieval directly within a macro)
{% macro latest_articles_macro() %} {% archiveList latest_articles with limit="5" %} {% for article in latest_articles %} <li>{{ article.Title }}</li> {% endfor %} {% endarchiveList %} {% endmacro %}Correct practice (obtain data externally and pass it to macro for rendering)
{# template.html #} {% archiveList latest_articles with limit="5" %} {% import "macros.html" as my_macros %} {{ my_macros.render_article_list(latest_articles) }} {% endarchiveList %} {# macros.html #} {% macro render_article_list(articles) %} <ul> {% for article in articles %} <li>{{ article.Title }}</li> {% endfor %} </ul> {% endmacro %}
macroFocus on receiving parameters and structuring rendering.It should focus on how to display data consistently, rather than where to obtain the data. This makesmacromore general and reusable, and can be used in different