In AnQiCMS management backend, user group management (English)userGroupDetailThe module is the foundation for us to divide permissions, set levels for different user groups, and even configure VIP services.通常,每个用户组都有其核心的属性,比如名称、等级、价格等,这些信息会直接显示在后台界面上,方便我们进行日常管理。

However, sometimes we hope that these fields are not just cold data, but also have some additional, more descriptive information.For example, we may want to add the unit 'per month' after the user group price, or add a prefix description for a custom setting field.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 is it necessary to have dynamic description?

Hardcoding these descriptions may be possible, but once the requirements change, or similar formats of descriptions are needed in multiple places, it will become麻烦 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 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 data types, so that they can be presented together in the template.

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

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

In practice in user group managementaddFilter

Let's take a look at how to view the user group detail page in AnQiCMSuserGroupDetailthe data presented by the tag) and make use ofaddFilter adds additional description to the field dynamically.

Firstly, we need touserGroupDetailTag retrieves specific user group data. This tag can be accessed through user group ID (id) or user group level (level)to specify the user group to be retrieved. Typically, this data is automatically provided as context within the user group detail template, or we can explicitly retrieve 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 key-value pair form.

1. Add unit description for numeric fields

Assuming we want to add the description 'per month' to 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, through|add:" 元/月",AnQiCMS will automatically convert numbers to strings and then concatenate them with the string ' Yuan/month', resulting in an effect similar to '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 user groups:

<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 the result is concatenated with the string "Level". Finally, the complete description displayed on the page will be something like "This user group level is: 3 Level".

3. Enhance the display of custom setting fields

userGroupDetailA very flexible one in the labelSettingField, which is a key-value pair, allowing us to store various custom user group configurations. Assuming we areSetting存储了一个名为specialFeature的额外信息。我们同样可以为其添加描述:

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

Through this method, even the key-value pair data defined in the background, we can display it in a more user-friendly manner in the front-end or back-end template.

Concluding remarks

AnQiCMSaddThe filter may seem simple, but it can play a significant role in actual content operation and backend management.It allows us to dynamically and individually customize field descriptions at a very low cost, thus enhancing user experience and management efficiency.addFilters can help us easily complete tasks.When designing templates, it's a good idea to think about how to make use of these flexible filters to make your AnQiCMS site content more vivid and easy to read.


Common Questions (FAQ)

Q1:addFilter can only be used for numbers and strings? What if I try to add a number to a complex object?

addFilter is primarily designed for the summation 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.addThe filter will usually ignore the object, only processing parts that can be added or concatenated to avoid template rendering errors. For example,{{ 5|add:someObject }}it may only display5and ignoresomeObject.

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 filters 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: Besides user group management,addFilter can be used in which places in AnQiCMS?

addFilter as a general filter of AnQiCMS template engine, which can be used in any template scene that requires string concatenation or simple arithmetic summation of numbers. For example, adding the suffix 'times read' to the article reading volume on the article detail page ({{ archive.Views|add:" 次阅读" }}In the product list page, combine the model and name of the product ({{ 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 come in handy.