Operating a website on AnQi CMS, flexibility and accuracy are crucial.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 is the foundation for personalized experiences and permission control.Today, let's delve into how to utilizeuserGroupDetailLabel, ensure that we can always get the correct group data.

userGroupDetailThe core role of tags

userGroupDetailThe tag is a very practical tool in Anqi CMS template, which allows us to obtain detailed information about specific user groups, such as the name, description, level, purchase price, and custom settings of the user group.This information is crucial for building member systems, 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 group data you get.nameParameters are used to specify the specific fields you want to retrieve, whereasidorlevelis used to clearly indicate which user group you want to query as a key parameter.

Why is it necessary to pay attention to the 'correct context'?

The core of the problem lies in callinguserGroupDetailWhen labeling, if not explicitly specifiedidorlevelThe system may attempt to infer user group data from the current page context or certain default settings.But this inference is not always consistent with the "correct context of the current page" we expect.For example, if the page does not have explicit user group information, the tag may return a default user group or unrelated user group data, which may lead to display errors and even affect the functional logic of the website.

Therefore, to ensure data accuracy, we always recommend callinguserGroupDetailWhen labeling, specify the target user group as clearly as possibleidorlevel.

Scenario one: Display the details of a specific user group

Assuming you are creating an introduction page for VIP levels, you need to display detailed information about different VIP levels.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 a 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 %}

This, no matter what state the page is in,userGroupDetailwill accurately obtain the user group information you specified.

Scenario two: Obtain 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 on the user center page or the 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 %}

By this chained call, we first confirmed the current logged-in user, then based on their realGroupIdGo to query the corresponding user group details to ensure that the data context is closely related to the current logged-in user.

Scenario three: Make conditional judgments and content display based on user group information.

userGroupDetailThe data obtained from the label is not only used for display, but it is often used for conditional judgments 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