It is crucial to have flexibility and accuracy in website operation in AnQi CMS.Especially when we need to display content based on user identity or specific requirements, ensuring that the user group data obtained matches the context of the current page accurately is the foundation for personalized experience and permission control.userGroupDetailLabel, ensure that we can always obtain the correct user group data.
userGroupDetailThe core function of the tag
userGroupDetailThe label is a very practical tool in AnQi CMS template, which allows us to obtain detailed information of specific user groups, such as the name of the user group, description, level, purchase price, and custom settings, etc.This information is crucial for building a member system, permission management, or displaying pages with different user benefits.
Its basic usage is usually like this:{% userGroupDetail 变量名称 with name="字段名称" id="1" %}Here,变量名称is the alias you define for the user group data you obtain.nameThe parameter is used to specify the specific fields you want to retrieve.idorlevelIt is used to explicitly indicate which user group you want to query.
Why should we pay attention to 'correct context'?
The core of the problem lies in, when we call in the templateuserGroupDetailwithout explicitly specifyingidorlevel
Therefore, to ensure data accuracy, we always recommend callinguserGroupDetailWhen tagging, specify the target user group as clearly as possibleidorlevel.
Scene one: Display details of a specific user group
假设你正在创建一个VIP等级介绍页面,需要展示不同VIP等级的详细信息。In this case, the user group ID or level is known and fixed, and we can specify it directly in the tag.
For example, to display the details of the VIP user group with ID 3:
{# 在一个VIP等级介绍页面,明确指定要展示的用户组ID #}
{% userGroupDetail vipGroup with id="3" %}
<div class="vip-level-card">
<h3>{{ vipGroup.Title }}</h3>
<p>{{ vipGroup.Description }}</p>
<p>专属价格: {{ vipGroup.Price }}</p>
{# 假设用户组设置中有"专属客服"字段 #}
{% if vipGroup.Setting.专属客服 %}
<p>享受服务: 专属客服</p>
{% endif %}
</div>
{% enduserGroupDetail %}
Or, if you prefer to use the level of the user group (level) to differentiate:
{# 展示等级为5的用户组详情 #}
{% userGroupDetail platinumVip with level="5" %}
<div class="vip-level-card platinum">
<h4>{{ platinumVip.Title }} - 顶级尊享</h4>
<p>{{ platinumVip.Description }}</p>
<p>年费: {{ platinumVip.Price }}</p>
</div>
{% enduserGroupDetail %}
Thus, it will accurately retrieve the specified user group information regardless of the state of the page.userGroupDetailIt will accurately retrieve the specified user group information for you.
Scenario two: Retrieve the user group information of the currently logged-in user.
More often, we may need to display the user group information of the currently logged-in user in the user center page or profile card. Anqi CMS providesuserDetailLabel to retrieve detailed data of the current user, which includes the user'sGroupId. We can first get thisGroupId, and then pass it as a parameter touserGroupDetail.
{# 假设你已经通过后端逻辑将当前登录用户的ID传递给模板,例如变量名为 `loggedInUserId` #}
{% userDetail currentUser with id=loggedInUserId %}
{% if currentUser %}
{# 拿到当前用户的 GroupId,并传递给 userGroupDetail #}
{% userGroupDetail userMembership with id=currentUser.GroupId %}
<div class="user-profile-card">
<p>您好,{{ currentUser.UserName }}!</p>
<p>您的会员等级: <strong>{{ userMembership.Title }}</strong> (等级: {{ userMembership.Level }})</p>
<p>享受特权: {{ userMembership.Description }}</p>
</div>
{% enduserGroupDetail %}
{% else %}
<p>您尚未登录,请先<a href="/login">登录</a>。</p>
{% endif %}
{% enduserDetail %}
Through this chained call, we first confirmed the current logged-in user, then based on their actualGroupIdGo to query the details of the corresponding user group to ensure that the data context is closely related to the currently logged-in user.
Scenario three: Perform conditional judgment and content display based on user group information.
userGroupDetailThe data obtained from the label is not only used for display, but it is also often used for conditional judgment in business logic.For example, decide whether to display certain exclusive content or features based on the user's membership level.
"twig
{# 假设你已经通过后端逻辑将当前登录用户的ID传递给模板,例如变量名为loggedInUserId` #}
{% userDetail currentUser with id=loggedInUserId %}
{% if currentUser %}
{% userGroupDetail userMembership with id=currentUser.GroupId %}
{% if userMembership.Level >= 3 %} {# 假设等级3及以上可以访问高级教程 #}
<div class="premium-tutorial-access">
<h4>恭喜!您可以访问高级教程。</h4>
<p><a href="/tutorials/premium">立即前往学习</a></p>
</div>
{% else %}
<div class="standard-tutorial-info">
<p>您的当前等级为: {{ userMembership.Title }} (等级: {{ userMembership.Level }})</p>
<p>升级到VIP3,即可解锁更多高级教程!</p>
<p><a