In AnQiCMS, providing personalized content and experience to users is a key factor in enhancing website stickiness.It is particularly important to understand the details of the user's group membership, whether it is to display exclusive content based on the user's level or to provide differentiated service information.Today, let's delve into how to make full use of the powerful template tag function of AnQiCMS, throughuserDetailTags retrieve users' information.GroupIdand further useuserGroupDetaillabel, display the detailed information of these user groups on the website front end.
Understand the value of user group management in AnQiCMS
In the design philosophy of AnQiCMS, user group management is a core function that supports defining different permission levels, and even building a VIP paid content system.This means you can flexibly control access permissions, function usage scope, and even display different interface elements on the front end according to the user group the user belongs to.For VIP users, display exclusive discount information, or hide specific feature access for ordinary members.To implement these refined operational strategies, it is first necessary to accurately obtain the user group information.
Step 1: ThroughuserDetailTags retrieve users' information.GroupId
userDetailTags are the main tools we use in the AnQiCMS template to obtain user details. It allows us to query various attributes of the user based on the user ID, including the ones we needGroupId.
UseuserDetailWhen labeling, you need to specify a user ID. Suppose we have a user whose ID is known as1(In practice, this would typically be the current logged-in user's ID, which might be passed in through a backend context variable, for example{{ request.user.id }}), and we can get 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 assign the details of the user with IDtargetUserIdto this variable. Subsequently, we can then access thecurrentUserDetailusing the variable.{{ currentUserDetail.GroupId }}To access the user group ID of this user. ThisGroupIdwill be the key to querying the user group details later.
Second step: useuserGroupDetailtags to get user group details
with the user'sGroupIdAfter that, we can continue to useuserGroupDetailTag to get the details of the user group.userGroupDetailLabels allow us to query the name, description, price, and other properties of user groups by their ID or level.
We will get the next step from the previous one.GroupIdas 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 touserGroupInfothe variable. Through{{ userGroupInfo.Title }}/{{ userGroupInfo.Level }}Etc., we can easily display rich information such as the name, level, and introduction of user groups. Especially noteworthy is,SettingThe field is a key-value pair that can store custom configurations for user groups, which provides the possibility of implementing more flexible business logic.
Integrated Application: Display the user group information of the logged-in user on the AnQiCMS front-end
Combine the above two steps, and we can implement a complete user group information display function on the AnQiCMS front-end.This is very practical for building personalized member centers, displaying user privileges, and other scenarios.
This is an example of a hypothetical user who has logged in, and we want to display their group name, level, and privileges.
"twig
{# 假设这里有一个全局可用的变量,存储了当前登录用户的ID。 #}
{# 在AnQiCMS的实际环境中,这可能由后端控制器注入,例如{{ globals.user.id }}` or directly available in the current page context.#}
{# For demonstration purposes, we first assume that the current user ID is 1