In AnQiCMS, providing personalized content and experience to users is a key factor in enhancing website stickiness.Whether it is to display exclusive content based on user level or to provide differentiated service information, understanding the details of the user's group is particularly important.userDetailTag retrieval of usersGroupId, and further use it to.userGroupDetailLabel, display the detailed information of these user groups on the front-end of the website.
Understand the value of user group management in AnQiCMS
In AnQiCMS design philosophy, user group management is a core feature that supports defining different permission levels, and even can build a VIP paid content system.This means you can flexibly control the access permissions, feature usage scope, and even display different interface elements on the front end based on the user group to which the user belongs.For VIP users, show exclusive discount information, or hide specific feature access for ordinary members.To implement these refined operational strategies, it is first necessary to accurately obtain user group information.
Step 1: ThroughuserDetailTag retrieval of usersGroupId
userDetailLabels are the main tools we use in AnQiCMS templates to obtain user details. They allow us to query various attributes of a user based on the user ID, including the ones we need.GroupId.
UseuserDetailLabeling, you need to specify a user ID. Suppose we have a user whose ID is known as1(This is typically the current logged-in user's ID in practical applications, which may be passed in through backend context variables, for example{{ request.user.id }}),We can obtain its details in this way:
{# 假设我们已知要查询的用户ID是1。在实际网站中,您会从后端获取当前登录用户的ID。 #}
{% set targetUserId = 1 %}
{% userDetail currentUserDetail with id=targetUserId %}
{# 检查是否成功获取到用户详情 #}
{% if currentUserDetail.Id %}
<p>用户ID: {{ currentUserDetail.Id }}</p>
<p>用户名: {{ currentUserDetail.UserName }}</p>
<p>用户邮箱: {{ currentUserDetail.Email }}</p>
<p>用户组ID: {{ currentUserDetail.GroupId }}</p>
{% else %}
<p>未找到ID为{{ targetUserId }}的用户信息。</p>
{% endif %}
{% enduserDetail %}
In the above code snippet, we use{% userDetail currentUserDetail with id=targetUserId %}tag to set the details of the user with IDtargetUserIdto this variable.currentUserDetailThen, we can use this variable to{{ currentUserDetail.GroupId }}This is the user group ID for visiting this user. ThisGroupIdwill be the key to query the user group details later.
Second step: UtilizeuserGroupDetailTag to get user group details
with the user'sGroupIdAfter that, we can continue to useuserGroupDetailtags to get the detailed information of the user group.userGroupDetailTags allow us to query the name, description, price, and other attributes by the user group ID or level.
we obtained in the previous stepGroupIdas a parameter touserGroupDetailLabel. Continue with the above example, assuming we have already obtainedGroupId:
{# 假设我们从userDetail标签中获取到了用户组ID,并将其赋值给userGroupId变量 #}
{% set userGroupId = 1001 %} {# 这里的1001只是一个示例值,实际应用中会是动态获取的 #}
{% userGroupDetail userGroupInfo with id=userGroupId %}
{% if userGroupInfo.Id %}
<p>用户组名称: {{ userGroupInfo.Title }}</p>
<p>用户组等级: {{ userGroupInfo.Level }}</p>
<p>用户组介绍: {{ userGroupInfo.Description }}</p>
<p>用户组购买价格: {{ userGroupInfo.Price }}</p>
<p>优惠价: {{ userGroupInfo.FavorablePrice }}</p>
{# 如果用户组有特殊的设置,例如“VIP内容访问权限”,可以在Setting字段中定义并获取 #}
{# 假设“VIPContentAccess”是Setting中的一个键,且值为"true" #}
{% if userGroupInfo.Setting.VIPContentAccess == "true" %}
<p><strong>此用户组拥有VIP内容访问权限!</strong></p>
{% endif %}
{% else %}
<p>未找到ID为{{ userGroupId }}的用户组信息。</p>
{% endif %}
{% enduserGroupDetail %}
Here,{% userGroupDetail userGroupInfo with id=userGroupId %}Assign the detailed information of the specified user group ID touserGroupInfoVariable. By{{ userGroupInfo.Title }}/{{ userGroupInfo.Level }}We can easily display rich information such as the name, level, and introduction of user groups. Particularly worth mentioning is,SettingThe field is a key-value pair that can store custom configurations of user groups, which provides the possibility for more flexible business logic.
综合应用:在AnQiCMS前端展示登录用户的用户组信息 English
Combine the above two steps, and we can implement a complete user group information display function on the front end of AnQiCMS.This is very useful for scenarios such as building personalized member centers and displaying user privileges.
The following is an example where it is assumed that the user is already logged in, and we hope to display the user group name, level, and privileges in an integrated manner:
"twig
{# 假设这里有一个全局可用的变量,存储了当前登录用户的ID。 #}
{# 在AnQiCMS的实际环境中,这可能由后端控制器注入,例如{{ globals.user.id }}` or directly available in the current page context.