How to pass the `add` filter as a parameter to the `macro` macro function and perform internal text processing?

Calendar 👁️ 60

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.

Related articles

When it comes to concatenating a large number of strings, which filter, `add` or `join`, performs better?

In AnQi CMS template development, we often need to combine different text fragments into a complete string to meet the display requirements of the page.AnQi CMS provides various template filters to complete this task, among which the `add` and `join` filters are two commonly used string concatenation tools.However, when faced with the need to concatenate a large number of strings, choosing the right tool becomes particularly important, as this directly affects the speed of page rendering and user experience.###

2025-11-07

How does the `add` filter assist in building dynamic `<img>` tag `alt` or `title` attribute text to optimize SEO?

In Anqi CMS, every detail of the website is related to the final operating effect, especially in search engine optimization (SEO).We all know that high-quality images can attract users, but if these images are not 'understood' by search engines, their value will be greatly reduced.At this time, the `alt` and `title` attributes of the `<img>` tag are particularly important.They can not only improve the accessibility of the website but are also a key window for search engines to convey the content and context of the image.However, manually writing a unique

2025-11-07

How to concatenate `{module}` and `{id}` variables using the `add` filter in Custom URL mode to build a link?

In AnQi CMS, the way websites build links is crucial for SEO and user experience.Although the system provides various predefined pseudo-static rules and automatically generated links, but in certain specific scenarios, we may need to control the URL structure more finely, such as in the custom URL mode, where we concatenate the content model (`{module}`) and content ID (`{id}`) variables to form a unique link.This is not a difficult task, the powerful template engine of Anqi CMS combined with its flexible filter mechanism allows us to easily achieve this goal. Today

2025-11-07

In the AnQiCMS template, can a simple counter dynamic display be realized through the `add` filter?

AnQiCMS is an enterprise-level content management system developed based on the Go language, providing strong support for content operators with its efficient and flexible features.In daily content operation and template creation, we often encounter the need to process and dynamically display data, such as adding numbers to list items and calculating totals.Among them, the template filter is an important tool for realizing such needs.Today, let's discuss the `add` filter in the AnQiCMS template to see if it can help us achieve a simple dynamic counter display.###

2025-11-07

How to use the `add` filter to dynamically add additional descriptions to the field in the AnQiCMS backend user group management (`userGroupDetail`)?

In the AnQiCMS management background, the user group management (`userGroupDetail`) module is the basis for us to divide permissions, set levels for different user groups, and even configure VIP services.Generally, each user group has its core attributes, such as name, level, price, etc., which are directly displayed on the back-end interface for convenient daily management.However, sometimes we hope that these fields are not just cold data, but can also have some additional, more descriptive information. For example

2025-11-07

Can the `add` filter be used to generate dynamic HTML attributes, such as the value of `data-` attributes?

In modern web development, dynamically generating HTML attributes, especially `data-` attributes, has become a common requirement.These properties not only provide additional data support for front-end JavaScript, but also enhance the presentation of elements without affecting the page semantics.AnQiCMS as a content management system that focuses on flexibility, its powerful template engine naturally also provides the possibility to meet this need.So, can the `add` filter in the AnQiCMS template handle the task of dynamically generating `data-` attribute values?

2025-11-07

How to dynamically generate JavaScript code snippets in AnQiCMS templates and use the `add` filter to concatenate variables?

In website operations, we often need to make the page more intelligent and interactive.AnQiCMS as an efficient and flexible content management system not only provides powerful content organization and display capabilities, but also allows us to ingeniously integrate dynamic logic in templates, such as by generating JavaScript code snippets to achieve more complex functions.AnQiCMS's template engine syntax is similar to Django, which provides great convenience for us to dynamically process data.It is developed based on the Go language, runs fast

2025-11-07

How does the `add` filter handle the addition/pasting operation with boolean values `true` and `false` as well as numbers or strings?

In Anqi CMS template development, we often need to process and display data.Among them, the `add` filter is a very practical tool that allows us to add numbers and concatenate strings.However, when the boolean values `true` and `false` are involved in these operations, their behavior may puzzle some users who are new to the subject.Today, let's take a deep dive into how the `add` filter handles addition or concatenation operations with boolean values, numbers, or strings.

2025-11-07