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.