In AnQiCMS, user group management is one of the core functions for building differentiated services and content monetization systems.It is crucial to display detailed information about user groups accurately in the front-end template, whether it is to provide exclusive content for VIP users or to set access restrictions for users of different permission levels.This not only improves the user experience, but also effectively guides users to understand and upgrade their own permissions.
How can we flexibly obtain and display the detailed information of these user groups in the AnQiCMS template? AnQiCMS provides special template tagsuserGroupDetailHelp us achieve this goal.
UnderstandinguserGroupDetailTag
userGroupDetailThe tag is mainly used to obtain detailed data for a single user group. Its basic usage is very intuitive, allowing us to accurately query through the user group ID or its level.
Basic usage:
{% userGroupDetail 变量名称 with name="字段名称" id="1" %}
Or
{% userGroupDetail 变量名称 with name="字段名称" level="1" %}
Here变量名称Any variable name you define can be used to refer to the user group information obtained.nameThe parameter specifies the specific field you want to retrieve.idandlevelare two key parameters, but remember,they are mutually exclusive, you can only choose one to specify the user group to query. If provided at the same time, the system will prioritize usingid.
The detailed fields that can be obtained include:
IdThe unique identifier ID of the user group.TitleThe name of the user group, such as 'Regular Member', 'Gold VIP', etc.DescriptionThe detailed introduction of the user group.LevelGroup level, usually a number used for permission sorting.PricePurchase price of the user group, if it is a paid group.FavorablePriceDiscount price of the user group, if available.SettingThis is a very flexible field, which stores the custom settings of user groups in the form of key-value pairs, such as "discount ratio", "exclusive service link", and so on.
Perform the actual operation: Retrieve and display user group information
Let's look at a few examples to see how to use templatesuserGroupDetail.
1. Display the name and introduction of a specific user group
Assuming we want to display the name and description of the user group with ID 1:
{# 通过用户组ID获取用户组详情 #}
{% userGroupDetail commonGroup with id="1" %}
<p>用户组名称:{{ commonGroup.Title }}</p>
<p>用户组介绍:{{ commonGroup.Description }}</p>
{% enduserGroupDetail %}
{# 或者通过用户组等级获取,假设等级1对应的也是ID为1的用户组 #}
{% userGroupDetail basicLevelGroup with level="1" %}
<p>基本等级用户组名称:{{ basicLevelGroup.Title }}</p>
<p>基本等级用户组等级:{{ basicLevelGroup.Level }}</p>
{% enduserGroupDetail %}
In this example, we first definedcommonGroupandbasicLevelGrouptwo variables to hold the user group information, and then accessed.using the symbolTitleandDescriptionfield.
2. Display user group pricing information
If our website has VIP paid members, we may need to display prices for different VIP levels:
{# 显示黄金VIP用户组的价格信息,假设ID为2 #}
{% userGroupDetail goldVIPGroup with id="2" %}
<h3>{{ goldVIPGroup.Title }}</h3>
<p>原价:{{ goldVIPGroup.Price }}元</p>
{% if goldVIPGroup.FavorablePrice > 0 %}
<p>优惠价:{{ goldVIPGroup.FavorablePrice }}元</p>
{% endif %}
{% enduserGroupDetail %}
Here we also added a simpleifThe judgment is only displayed when the discount price exists (greater than 0).
3. Flexible handlingSettingCustom settings in the field
SettingThe field is in key-value pair form, which allows us to add various custom attributes for user groups in the background.For example, we can set a "discount ratio" of 0.8 (i.e., an 80% discount) for the "Gold VIP" user group in the background.
{% userGroupDetail goldVIPGroup with id="2" %}
<h3>{{ goldVIPGroup.Title }}</h3>
<p>介绍:{{ goldVIPGroup.Description }}</p>
{# 获取自定义设置中的“折扣比例” #}
{% if goldVIPGroup.Setting.discountRate %}
<p>享有商品 {{ goldVIPGroup.Setting.discountRate * 100 }}% 的折扣!</p>
{% endif %}
{# 也可以遍历所有的设置项 #}
{% if goldVIPGroup.Setting %}
<h4>更多专属特权:</h4>
<ul>
{% for key, value in goldVIPGroup.Setting %}
<li>{{ key }}:{{ value }}</li>
{% endfor %}
</ul>
{% endif %}
{% enduserGroupDetail %}
Please note,goldVIPGroup.Setting.discountRateThis syntax can be accessed directlySettingThe key in the object. If you need to display all custom settings, you canfor key, value in goldVIPGroup.Settingfor traversal.
4. Get the user group information of the currently logged-in user
In practical applications, we often need to display the user group information of the currently logged-in user. This requires combininguserDetailtags to first obtain theGroupId, and then useuserGroupDetailto obtain the user group details.
{# 假设我们已经获取了当前登录用户的ID,例如通过一个全局变量 currentUser.Id #}
{# 或者直接通过userDetail标签获取当前用户的GroupId #}
{% userDetail currentUser with id="当前用户的ID" %} {# 或者如果当前页面是用户中心,可能可以直接获取当前用户ID #}
{% if currentUser.GroupId %}
{# 获取该用户所属的用户组详情 #}
{% userGroupDetail userAssignedGroup with id=currentUser.GroupId %}
<div class="user-group-info">
<p>您当前的用户组是:<strong>{{ userAssignedGroup.Title }}</strong> (等级: {{ userAssignedGroup.Level }})</p>
<p>用户组介绍:{{ userAssignedGroup.Description }}</p>
{% if userAssignedGroup.Setting.vipBenefits %}
<p>您享有的VIP特权:{{ userAssignedGroup.Setting.vipBenefits }}</p>
{% endif %}
</div>
{% enduserGroupDetail %}
{% else %}
<p>您当前未加入任何用户组。</p>
{% endif %}
{% enduserDetail %}
Here, we first go throughuserDetailtag to getcurrentUserA variable that contains the user'sGroupId. Then, we will use thisGroupIdpass touserGroupDetaillabel to successfully retrieve and display the detailed information of the user's group.
Some points to note
- Parameter selection: In use
userGroupDetailAt the time, make sureidandlevelChoose only one of the two parameters to provide. If you have both, it is usually usedidwill be more accurate - Handling empty valuesBefore attempting to retrieve user group information, it is best to check if the user group ID or level exists, or when using
.Titleconsider the possibility that these fields may be empty and addifJudge or use the default value filter to avoid abnormal display on the page. For example{{ userAssignedGroup.Title|default:"无用户组名称" }}. SettingField dynamics:SettingThe content of the field depends entirely on the backend configuration, so when accessing it in the template, it is necessary to clearly understand which key-value pairs the backend has set in order to accurately pass throughuserGroupDetail.Setting.你的键名Get it.
ByuserGroupDetailLabel, AnQiCMS makes the display of user group information simple and powerful, helping us to better build personalized website experiences and business models.
Frequently Asked Questions (FAQ)
If the user is currently not assigned any user group, a call is made in the template
userGroupDetailWhat will be displayed? How should we handle this situation?Answer: IfuserGroupDetaillabel'sidorlevelThe value is empty or invalid (for example, the user is not associated with a user group, or the user group ID does not exist), thenuserGroupDetailThe variable defined by the tag (as in the example above)userAssignedGroupwill not be filled with data, or its internal fields (such asTitlewill be empty. To avoid page display errors, it is recommended to use the template.ifCheck if a variable exists or if its key field is empty. For example, after callinguserGroupDetailyou can judge like this:{% if userAssignedGroup.Title %}Ensure that information is displayed only when the user group title exists, or usedefaultthe filter to provide default text.SettingHow to flexibly retrieve and display custom settings such as 'VIP exclusive discount' in the template?Answer:SettingThe field is an object or a map, you can access specific keys directly through dot notation (.). For example, if the backend is in a user group'sSettingSet in ChinesediscountRateThis key, you can pass through{{ userGroup.Setting.discountRate }}Get the value.