In the daily content operation of AnQi CMS, we strive for efficient and flexible management and display of website content. The template system is the core to achieve this goal, among whichmacromacro functions andaddThe filter is a very practical tool. Understanding how it works together, especially how to use it in macro functionsaddThe filter performs internal text processing, which greatly enhances the reusability of our templates and the dynamism of the content.
The Anqi CMS template system uses syntax similar to Django, which allows us to quickly get started for those familiar with web development. It uses double curly braces{{ 变量 }}Output a variable, through single curly braces and percent signs{% 标签 %}To control logic and call functionsmacroMacros, in the literal sense, are like the small utility functions we define in programming, allowing us to encapsulate a reusable template code and can accept parameters.In this way, complex page structures can be broken down into smaller, more manageable and maintainable components, thereby improving development efficiency.
AndaddA filter, a simple yet powerful feature provided by the template system, mainly used for the 'addition' operation of two values.This 'addition' is not just the sum of numbers in the mathematical sense, it can also be understood as the concatenation of strings.5with2Adding will get7And concatenating a string"安企"with"CMS"Adding will then get"安企CMS". Anqi CMS'saddThe intelligence of the filter lies in its ability to attempt automatic conversion of different types of data for addition, and it will ignore content that cannot be added when the conversion fails, making its use very flexible.
So, whenaddthe filter meetsmacrothe macro function, and needs to perform internal text processing, what kind of sparks will be generated?
Imagine, we want to create a macro function to generate card titles with dynamic prefixes and content.This prefix and content may come from different data sources, or may need to be adjusted according to specific scenarios.macrowithin the function, usingaddthe filter to process the incoming parameters.
For example, we can define a macro function namedrender_card_titlewhich takes two parameters:prefix_text(prefix text) andmain_title(main title).
{# 定义一个宏函数,用于动态生成卡片标题 #}
{% macro render_card_title(prefix_text, main_title) %}
<h3 class="card-title">
{# 在宏函数内部使用add过滤器拼接文本 #}
{{ prefix_text|add:main_title }}
</h3>
{% endmacro %}
Inside this macro function,prefix_textandmain_titleAre passed in as parameters.addThe filter is applied toprefix_textand will be on,main_titleThe value is appended to it. In this way, we can concatenate the input text within the macro function and dynamically generate the complete title content.
In actual use, we can call this macro function at any place on the page and pass in different values:
{# 假设我们有一个文章对象 archive 和一个产品对象 product #}
{% set article = { 'Title': '安企CMS模板开发指南', 'Id': 101 } %}
{% set product = { 'Title': '安企CMS企业版', 'Id': 202 } %}
{# 调用宏函数生成文章卡片标题 #}
{{ render_card_title('文章:', article.Title) }}
{# 调用宏函数生成产品卡片标题 #}
{{ render_card_title('产品名称:', product.Title) }}
{# 甚至可以拼接数字类型的ID #}
{{ render_card_title('ID:', article.Id) }}
In this way,addthe filter works perfectly inmacroThe macro function takes on the role of internal text processing.It allows us to pass the required text fragments as parameters to the macro function, and then combine these fragments into the final output within the macro function according to the business logic.This pattern is particularly efficient and concise when it is necessary to construct dynamic URLs, generate personalized messages, or combine different data fields in repeated components.
For example, in a macro that needs to generate a dynamic link, we can use it like thisadd:
{# 定义一个宏函数,用于生成带有动态参数的链接 #}
{% macro generate_dynamic_link(base_url, param_name, param_value) %}
<a href="{{ base_url|add:'?'|add:param_name|add:'='|add:param_value }}">{{ param_value }}详情</a>
{% endmacro %}
{# 调用宏函数生成链接 #}
{{ generate_dynamic_link('/archive/detail', 'article_id', article.Id) }}
{# 结果可能是:<a href="/archive/detail?article_id=101">101详情</a> #}
here,base_url/param_nameandparam_valueThey are all parameters of the macro function.addThe filter connects these parameters with some fixed strings (such as?and=) and connects them.