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

Calendar 👁️ 62

In the AnQiCMS management background, user group management (userGroupDetailThe module is the basis for us to divide permissions for different user groups, set levels, 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 daily management.

However, sometimes we want these fields to be more than just cold data, but to have some additional, more descriptive information.For example, we may want to add the unit ' Yuan/Month' after the user group price, or add a prefix explanation for some custom setting field.This is provided by AnQiCMS,addThe filter can be put to good use, it allows us to dynamically add more descriptive information at the template level, making the information display clearer and more user-friendly.

Why do we need dynamic descriptions?

Imagine you are managing different levels of VIP user groups.A user group's "price" field displays "99", and another displays "199".If there is no clear unit, these numbers may not be intuitive.Or perhaps, you have configured a custom setting named "Exclusive Feature" for a certain VIP user group, with the value of "High-definition video".If it can be directly displayed as "Including exclusive features: High-definition video," it will undoubtedly greatly enhance the efficiency of backend administrators in understanding information when viewing or editing user groups.

Hardcoding these descriptions may be feasible, but once the requirements change, or when similar format descriptions are needed in multiple places, it can be麻烦 to modify.addFilter, we can easily combine static text with dynamic data to create more flexible and expressive field descriptions.

addFilter: the magician of numbers and strings

addThe filter is a small and practical tool in the AnQiCMS template engine, its main function is to add two values.Here, 'addition' not only refers to the sum of numbers, but also includes the concatenation of strings.This means, you can use it to connect text, numbers, even mixed type data, so that they can be presented together in a template.

Its usage is very intuitive, just like this:{{ 你的变量|add:要添加的内容 }}.

WhenaddThe filter processes numbers by performing mathematical addition;When it processes strings, it will perform string concatenation.If mixed, it will also try to perform intelligent conversion, and if the conversion fails, it will ignore the parts that cannot be added, which provides us with great flexibility.

Practice in user group managementaddFilter

Let's take a look at how to view the user group details page in AnQiCMS,userGroupDetailThe data displayed by the tag is utilizingaddThe filter adds additional descriptions dynamically to the field

First, we need to go throughuserGroupDetailThe tag retrieves specific user group data. This tag can be accessed through the user group ID(id)or user group level(levelSpecify the user group to retrieve. Usually, this data is automatically provided in the user group detail template, or we can explicitly obtain it:

{# 假设我们正在用户组详情页,或者通过ID获取特定用户组 #}
{% userGroupDetail currentUserGroup with id=1 %}

Now,currentUserGroupContains all information about the user group, such ascurrentUserGroup.Title(User group name),currentUserGroup.Price(Purchase price),currentUserGroup.Level(User group level) etc., even includingcurrentUserGroup.SettingThis user group setting in the form of key-value pairs.

1. Add unit description for numeric fields

Suppose we want to add the description 'per month' after the purchase price of the user group:

<p>
  <strong>用户组名称:</strong>{{ currentUserGroup.Title }}
</p>
<p>
  <strong>购买价格:</strong>{{ currentUserGroup.Price|add:" 元/月" }}
</p>

here,currentUserGroup.Priceis a number, passed|add:" 元/月"AAnQiCMS will automatically convert numbers to strings and then concatenate them with the string " Yuan/month " to display effects like "99 Yuan/month".

2. Combine static text with dynamic data

We can also useadda filter to combine a static text description with the dynamic level information of the user group:

<p>
  <strong>用户组等级:</strong>{{ "此用户组等级为:"|add:currentUserGroup.Level|add:" 级" }}
</p>

In this example, we sawaddthe chained usage of filters. First, the string "This user group level is:" andcurrentUserGroup.LevelConcatenating a number, then appending the string " Level ". Finally, the complete description displayed on the page will be something like "This user group level is: 3 Level".

3. Enhanced custom field display

userGroupDetailA label with a highly flexibleSettingfield, which is a key-value pair allowing us to store various custom user group configurations. Suppose we areSettingIt stored a name calledspecialFeatureAdditional information. We can also add a description for it:

{# 假设Setting中有一个名为'specialFeature'的字段,存储了字符串值 #}
{% if currentUserGroup.Setting.SpecialFeature %}
  <p>
    <strong>特色功能:</strong>{{ "包含专属功能:"|add:currentUserGroup.Setting.SpecialFeature }}
  </p>
{% endif %}

In this way, even for the key-value pairs defined in the background, we can display them in a more user-friendly manner on the front-end or back-end templates.

Conclusion

AnQiCMS'addThe filter may seem simple, but it can play a significant role in content operation and backend management.It allows us to achieve dynamic and personalized field descriptions at a low cost, thereby improving user experience and management efficiency.Whether it is to add units to numbers or to combine multiple pieces of information into a complete sentence,addFilters can help us easily complete tasks. When designing templates, it is advisable to think about how to make use of these flexible filters to make your AnQiCMS site content more vivid and readable.


Frequently Asked Questions (FAQ)

Q1:addCan the filter only be used for numbers and strings? What would happen if I try to add a number to a complex object?

addThe filter is mainly designed for the sum of numbers or the concatenation of strings.When you try to add a number to a string, the number is usually converted to a string for concatenation.If encountered complex objects that cannot be recognized or converted,addThe filter usually ignores the object, only processing parts that can be added or concatenated to avoid errors in template rendering. For example,{{ 5|add:someObject }}it may only display5, while ignoringsomeObject.

Q2:addCan the filter be used for multiple chained operations? For example,{{ "前缀"|add:变量1|add:"中缀"|add:变量2 }}?

Yes,addThe filter fully supports chained operations. As shown in the examples in the article, you can use multiple ones in a row.|add:内容Build the final string or number you want step by step. This method is very convenient and efficient when combining multiple dynamic values with static text.

Q3: In addition to user group management,addWhat are the places where the filter can be used in AnQiCMS?

addThe filter, as a general filter of AnQiCMS template engine, can be used in any template scene where string concatenation or simple summation of numbers is required. For example, in the article detail page, it can be used to add the suffix 'times read' to the article reading volume.{{ archive.Views|add:" 次阅读" }}) Combine the model and name of the product on the product list page ({{ product.Model|add:" - "|add:product.Title }}), or dynamically generating URL fragments with parameters in navigation links, etc. As long as you handle variable display in the template, it may be useful.

Related articles

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

In AnQi CMS daily content operation, we pursue efficient and flexible management and display of website content.The template system is the core of achieving this goal, among which the `macro` macro function and the `add` filter are two very practical tools.Understand how they work together, especially how to use the `add` filter for internal text processing in macro functions, which can greatly enhance the reusability and dynamism of our templates.The AnQi CMS template system uses syntax similar to Django, which allows us familiar with Web development to quickly get started.

2025-11-07

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

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

How to use the `add` filter to dynamically add a uniform prefix or suffix to the single page titles in the `pageList`?

In Anqi CMS, content management is not limited to the backend's add, delete, modify, and query, but also manifests in the flexibility and diversity of front-end display.In the face of common single-page websites (such as "About Us", "Contact Us", etc.), sometimes we hope that they can be unified with specific identifiers when displayed in lists or navigation, such as If you modify the background title one by one, it is not only inefficient but also lacks uniformity and maintainability.At this time, utilizing the powerful template engine function of AnqiCMS, with the clever filter

2025-11-07