In AnQiCMS, when managing users and content, we often encounter the need to display different information based on the user's identity (such as VIP level).The powerful template engine and user group management features of AnQiCMS make it very intuitive and efficient to achieve this goal.

Today, let's discuss how to retrieve and flexibly display detailed information of specific user groups in the AnQiCMS template, such as the name of the VIP level.

Learn about the user group and VIP system of AnQiCMS

An QiCMS is built with a comprehensive user group management and VIP system, which allows us to divide users into different permission levels and even support paid content.In the background, you can define multiple user groups, such as " ordinary usersThis information can be easily displayed on the front-end page through template tags.

In order to display the user group details in the template, our core approach is to take two steps: first, obtain the current user's ID and the user group ID associated with it; second, based on this user group ID, further obtain the detailed information of the user group (such as the VIP level name).

The first step: Get the current user ID and the user group ID it belongs to

In the AnQiCMS template, we can useuserDetailLabel to retrieve the details of the current user (or specified user).This tag requires a user ID as a parameter.In most cases, you will operate on the user's personal center page or any page that needs to display user information.currentUserId.

userDetailTags provide many user-related fields, includingGroupIdThe field is the key to obtaining the user group details next.

Here is a simple example showing how to obtain the current user's ID and further extract itsGroupId:

{# 假设 currentUserId 变量已由后端控制器传递至模板 #}
{% userDetail currentUserDetail with id=currentUserId %}
{% if currentUserDetail %}
    {# 成功获取到用户详情,现在提取用户组ID #}
    {% set userGroupId = currentUserDetail.GroupId %}
    
    {# 可以在此处打印一些调试信息,以便确认是否正确获取 #}
    {# <p>用户名:{{ currentUserDetail.UserName }}</p> #}
    {# <p>用户组ID:{{ userGroupId }}</p> #}
{% else %}
    {# 用户未登录或未找到该用户ID的处理 #}
    {# <p>用户未登录或用户ID无效。</p> #}
{% endif %}

In the code above, we first try to go throughuserDetailtag to obtaincurrentUserIdthe corresponding user details, and assign it tocurrentUserDetailthe variable. If it is successfully retrieved, we takecurrentUserDetailoutGroupIdand store it inuserGroupIdIn the variable, preparing for the next step.

Second step: Get user group details based on user group ID

Once we have the user'sGroupId, we can useuserGroupDetailLabel to get detailed information about the user group. This label also acceptsid(User Group ID) orlevel(User Group Level) as a parameter. Since we have already obtainedGroupIddirectlyidParameters are the most convenient way.

userGroupDetailThe fields provided by the label include.Title(Group name, usually the VIP level name),Description(Group introduction),Level(Group level value) and so on.

Following the above example, let's get the detailed information of the user group:

{% if userGroupId %} {# 确保 userGroupId 存在 #}
    {% userGroupDetail userGroup with id=userGroupId %}
    {% if userGroup %}
        {# 成功获取到用户组详情,现在可以展示信息了 #}
        {# <p>用户组名称:{{ userGroup.Title }}</p> #}
        {# <p>用户组等级:{{ userGroup.Level }}</p> #}
        {# <p>用户组描述:{{ userGroup.Description }}</p> #}
    {% else %}
        {# 未找到对应的用户组(可能已删除或ID无效) #}
        {# <p>未找到对应的用户组信息。</p> #}
    {% endif %}
{% endif %}

Here, we useuserGroupIdto query the details of the user group and store the results inuserGroupthe variable. Then, we can accessuserGroup.TitleTo get the name of the VIP level,userGroup.LevelOr get the level value,userGroup.DescriptionTo display more information.

Integration and practical application

Combine the above two steps, and we can fully display the user's VIP level in the template.We can also add some simple logical judgments, according to the user group level to assign different styles or display different prompt information.

{% set currentUserId = 1 %} {# 示例:假设当前用户ID为1,请替换为实际从后端获取的变量 #}

<div class="user-info-panel">
    {% userDetail currentUserDetail with id=currentUserId %}
    {% if currentUserDetail %}
        {% set userGroupId = currentUserDetail.GroupId %}
        
        <p>欢迎回来,<strong>{{ currentUserDetail.UserName }}</strong>!</p>
        
        {% userGroupDetail userGroup with id=userGroupId %}
        {% if userGroup %}
            <span class="user-level vip-level-{{ userGroup.Level }}">
                您当前的身份是:<strong class="vip-text-{{ userGroup.Level }}">{{ userGroup.Title }}</strong>
                {% if userGroup.Description %}<small>({{ userGroup.Description }})</small>{% endif %}
            </span>
        {% else %}
            <span class="user-level">您当前的身份是:普通用户(用户组信息缺失)</span>
        {% endif %}

        {# 还可以根据VIP等级显示不同的内容或功能入口 #}
        {% if userGroup.Level >= 2 %} {# 假设Level 2及以上是VIP #}
            <p>作为尊贵的{{ userGroup.Title }},您可以访问专属内容和特权!</p>
            <a href="/vip-exclusive-content">前往VIP专属页面</a>
        {% else %}
            <p>升级成为VIP,解锁更多精彩!</p>
            <a href="/upgrade-vip">立即升级</a>
        {% endif %}

    {% else %}
        <p>您好!请<a href="/login">登录</a>或<a href="/register">注册</a>,享受更多服务。</p>
    {% endif %}
</div>

In this comprehensive example, we not only obtain and display the user's VIP level name and description, but also judge different display copy and functional links according to the level. Throughvip-level-{{ userGroup.Level }}Such CSS class names, you can also provide unique visual styles for VIP users of different levels.

Points to note

  • Obtaining the user ID: As shown in the above example,currentUserIdEnsure that you can pass the information from the backend controller correctly to the template in practical applications. In the AnQiCMS page context, it usually contains information about the currently logged-in user.
  • Back-end configurationAll user groups (including VIP levels) must be configured in advance in the "User Group Management and VIP System" of the AnQiCMS backend to ensure correct template calls. * **