Displaying different user group names and introduction information in Anqi CMS is an important part of website personalized operation and content distribution.No matter if you want to display exclusive identifiers for VIP members or provide customized content for users with different permissions, AnQi CMS's flexible template tag system can help you easily achieve this.
Understanding the core approach to user group information
To display the name and introduction of the user group, it is first necessary to clarify two key points: whether the user is logged in, and which user group the user belongs to.The AnQi CMS provides powerful template tags, allowing you to dynamically retrieve and display these data on the front-end page.
The core idea is: first, obtain the user group ID of the current user through user-related tags, and then use the user group detail tags to obtain the corresponding user group name, introduction, and other custom attributes according to this ID or the level of the user group.
Retrieve user group detail template tag
AnQi CMS provides a specialuserGroupDetailThe tag is used to obtain detailed information about user groups. This tag is very flexible and can accurately obtain the required data based on the user group ID or level.
userGroupDetailThe tag supports specifying which user group information to retrieve via the following parameters:
idThe unique identifier ID of the user group.level: The level of the user group. Usually, you can choose one as a query condition.
Including the information it can return:
Id: The ID of the user group.Title: The name of the user group.Description: The introduction of the user group.Level: The level of the user group.PriceThe purchase price of the user group (if the VIP system is set).FavorablePriceThe discount price of the user group.SettingA key-value pair (map) is used to store any custom attributes you set for the user group in the background.
Understanding this, we can start practicing in the template.
Practice Exercise: Integrate and Display User Group Information
Next, we will demonstrate how to display the names and introductions of user groups in templates through several common scenarios.
Scene one: Display the group name and introduction of the current logged-in user
Assuming you want to display the user group name and its introduction on the personal center or other pages after the user logs in.This requires us to first obtain the user group ID of the currently logged-in user, and then query the details of the user group.
{# 假设当前登录用户的信息已在模板上下文中,并可以通过 {{ user.Id }} 获取到用户ID #}
{% if user.Id %}
{# 1. 获取当前登录用户的用户组ID #}
{% userDetail currentUserGroupId with name="GroupId" id=user.Id %}
{# 2. 根据获取到的用户组ID,查询用户组的详情 #}
{% userGroupDetail userGroup with id=currentUserGroupId %}
{% if userGroup.Title %}
<div class="user-group-info">
<p>您当前的用户组是:<strong>{{ userGroup.Title }}</strong></p>
{% if userGroup.Description %}
<p>用户组介绍:{{ userGroup.Description }}</p>
{% endif %}
</div>
{% else %}
<p>未能获取到用户组信息。</p>
{% endif %}
{% else %}
<p>请登录以查看您的用户组信息。</p>
{% endif %}
In this code, we first go throughuserDetailThe tag retrieved the current logged-in userGroupId, and assigned it tocurrentUserGroupIdvariable. Then, we useuserGroupDetailthe tag, passing this incurrentUserGroupIdObtained the complete user group objectuserGroupPassed in the end{{ userGroup.Title }}and{{ userGroup.Description }}Displayed the name and introduction of the user group
Scenario two: Displaying information of a specific user group based on specified ID or level
Sometimes, you may need to display an overview of all user groups on the page or an introduction to a specific VIP level without first obtaining the information of the currently logged-in user. In this case, you can directly go throughidorlevelSpecify the parameter to query the user group.
Query by user group ID:
{# 假设要查询ID为1的用户组(例如:普通会员) #}
{% userGroupDetail normalMemberGroup with id=1 %}
<div class="member-tier-card">
<h3>{{ normalMemberGroup.Title }}</h3>
{% if normalMemberGroup.Description %}
<p>{{ normalMemberGroup.Description }}</p>
{% endif %}
{% if normalMemberGroup.Price %}
<p>价格:{{ normalMemberGroup.Price }} 元</p>
{% endif %}
</div>
{# 假设要查询ID为2的用户组(例如:VIP会员) #}
{% userGroupDetail vipMemberGroup with id=2 %}
<div class="member-tier-card vip">
<h3>{{ vipMemberGroup.Title }}</h3>
{% if vipMemberGroup.Description %}
<p>{{ vipMemberGroup.Description }}</p>
{% endif %}
{% if vipMemberGroup.FavorablePrice %}
<p>优惠价:{{ vipMemberGroup.FavorablePrice }} 元</p>
{% endif %}
</div>
Query by user group level:
{# 假设要查询等级为1的用户组 #}
{% userGroupDetail levelOneGroup with level=1 %}
<p>等级为1的用户组名称:{{ levelOneGroup.Title }}</p>
{# 假设要查询等级为5的最高级用户组 #}
{% userGroupDetail highestLevelGroup with level=5 %}
<p>最高等级({{ highestLevelGroup.Level }})用户组:{{ highestLevelGroup.Title }}</p>
These examples demonstrate how to directly locate and extract information for specific user groups, allowing you to flexibly display this content in different areas of the website.
Further: Using Custom User Group Settings (Setting)
The user group management of AnQi CMS is not limited to the preset fields, you can also add custom attributes for user groups in the background. These custom attributes will be stored inSettingWithin the field, and provided in the form of key-value pairs for template calls. For example, you can set different 'exclusive discounts' or 'customer service hotlines' for different user groups.
Assume you have set up a custom attribute namedDiscountRate(discount rate) in the background for a certain user group. You can retrieve it in the template like this:
{% userGroupDetail userGroup with id=currentUserGroupId %}
{% if userGroup.Setting.DiscountRate %}
<p>您的用户组专属折扣:{{ userGroup.Setting.DiscountRate }}%</p>
{% endif %}
In this way, you can define an unlimited number of custom information for user groups and flexibly display it on the front end, achieving a highly customized member service.
Summary
The user group management function of AnQi CMS is powerful and flexible, working with intuitive template tags, allowing you to easily display the names, introductions, and custom information of different user groups on the website front-end.From identifying the user's group to obtaining detailed group data, and then utilizing custom settings, the entire process is smooth and efficient, providing solid support for your website content operations and user experience optimization.
Frequently Asked Questions (FAQ)
1. Why do I follow the example code, but I still can't get the group name of the currently logged-in user?Answer: Make sure the user is already logged in on the website. AnQi CMS'suserDetailThe tag needs to depend on whether there is a user ID in the session when retrieving the current logged-in user information. If the user is not logged in, or the login status cannot be correctly passed to the template context (for example, on some non-user center pages), you may not be able to retrieve ituser.IdThus, the subsequent user group information cannot be obtained. Please check your login status and template