In the security CMS, displaying the names and introduction information of different user groups is an important part of website personalized operation and content distribution.No matter if you want to display exclusive identification for VIP members or provide customized content for users with different permissions, Anqi CMS's flexible template tag system can help you easily achieve this.

Understanding the core concept of user group information

To display the name and introduction of the user group, it is necessary to clarify two key points: whether the user is logged in and which user group the user belongs to.English CMS provides powerful template tags for you to dynamically retrieve and display these data on the front-end page.

The core idea is: first obtain the user group ID of the current user through user-related tags, then use the user group detail tags, according to this ID or the level of the user group, to obtain the corresponding user group name, introduction, and other custom properties.

Get user group details template tag

Anqí CMS provides a specialuserGroupDetailLabel, used to retrieve detailed information about user groups. This label is very flexible and can accurately obtain the required data based on the user group ID or level.

userGroupDetailLabel supports specifying which user group information to retrieve via the following parameters:

  • id: The unique identifier ID of the user group.
  • level:The level of the user group. Usually, you can choose one of them as a query condition.

And the information it can return includes:

  • Id:The ID of the user group.
  • Title:The name of the user group.
  • Description:User group introduction.
  • Level:User group level.
  • Price:User group purchase price (if the VIP system is set).
  • FavorablePrice:User group discount price.
  • SettingAn auto key-value pair (map) is used to store any custom attributes you set for user groups in the background.

With this understood, we can start practicing in the template.

Practice Exercise: Integrate Display User Group Information

Next, we will demonstrate how to display the name and introduction of user groups in templates through several common scenarios.

Scene one: Display the current logged-in user's group name and introduction

If you want to display the user group name and its introduction in the personal center or other pages after the user logs in.This requires us to first obtain the current logged-in user's group ID, and then query the details of that group.

{# 假设当前登录用户的信息已在模板上下文中,并可以通过 {{ user.Id }} 获取到用户ID #}
{% if user.Id %}
    {# 1. 获取当前登录用户的用户组ID #}
    {% userDetail currentUserGroupId with name="GroupId" id=user.Id %}

    {# 2. 根据获取到的用户组ID,查询用户组的详情 #}
    {% userGroupDetail userGroup with id=currentUserGroupId %}
    
    {% if userGroup.Title %}
        <div class="user-group-info">
            <p>您当前的用户组是:<strong>{{ userGroup.Title }}</strong></p>
            {% if userGroup.Description %}
                <p>用户组介绍:{{ userGroup.Description }}</p>
            {% endif %}
        </div>
    {% else %}
        <p>未能获取到用户组信息。</p>
    {% endif %}
{% else %}
    <p>请登录以查看您的用户组信息。</p>
{% endif %}

In this code, we firstuserDetailTag obtained the current logged-in user'sGroupIdand assigned it tocurrentUserGroupIdvariable. Subsequently, we useuserGroupDetaillabel, passing thiscurrentUserGroupId,obtained the complete user group object.userGroup,finally through.{{ userGroup.Title }}and{{ userGroup.Description }}Displayed the name and introduction of the user group.

Scenario two: Display the information of a specific user group based on the specified ID or level.

Sometimes, you may need to display an overview of all user groups on the page, or an introduction to a specific VIP level, without first obtaining information about the currently logged-in user. In this case, you can directly throughidorlevelParameters are used to specify the user group to be queried.

Query by user group ID:

{# 假设要查询ID为1的用户组(例如:普通会员) #}
{% userGroupDetail normalMemberGroup with id=1 %}
    <div class="member-tier-card">
        <h3>{{ normalMemberGroup.Title }}</h3>
        {% if normalMemberGroup.Description %}
            <p>{{ normalMemberGroup.Description }}</p>
        {% endif %}
        {% if normalMemberGroup.Price %}
            <p>价格:{{ normalMemberGroup.Price }} 元</p>
        {% endif %}
    </div>

{# 假设要查询ID为2的用户组(例如:VIP会员) #}
{% userGroupDetail vipMemberGroup with id=2 %}
    <div class="member-tier-card vip">
        <h3>{{ vipMemberGroup.Title }}</h3>
        {% if vipMemberGroup.Description %}
            <p>{{ vipMemberGroup.Description }}</p>
        {% endif %}
        {% if vipMemberGroup.FavorablePrice %}
            <p>优惠价:{{ vipMemberGroup.FavorablePrice }} 元</p>
        {% endif %}
    </div>

Query by user group level:

{# 假设要查询等级为1的用户组 #}
{% userGroupDetail levelOneGroup with level=1 %}
    <p>等级为1的用户组名称:{{ levelOneGroup.Title }}</p>

{# 假设要查询等级为5的最高级用户组 #}
{% userGroupDetail highestLevelGroup with level=5 %}
    <p>最高等级({{ highestLevelGroup.Level }})用户组:{{ highestLevelGroup.Title }}</p>

These examples demonstrate how to directly locate and extract information for specific user groups, allowing you to flexibly display this content in different areas of the website.

Further: Use custom user group settings (Setting)

The user group management in Anqi CMS is not limited to preset fields, you can also add custom attributes for user groups in the background. These custom attributes will be stored inSetting字段中,并以键值对的形式供模板调用。例如,您可以为不同用户组设置不同的“专属折扣”或“客服热线”。

Assume you have set a custom attribute namedDiscountRate(Discount Rate) for a user group in the background. You can access it in the template like this:

{% userGroupDetail userGroup with id=currentUserGroupId %}
    {% if userGroup.Setting.DiscountRate %}
        <p>您的用户组专属折扣:{{ userGroup.Setting.DiscountRate }}%</p>
    {% endif %}

Through this method, you can define as many custom pieces of information for user groups as you like, and flexibly display them on the front end to achieve highly customized member services.

Summary

The user group management function of Anqi CMS is powerful and flexible, which can be easily displayed on the front end of the website with intuitive template tags, allowing you to show different user group names, introductions, and custom information.From identifying the user's group to obtaining detailed group data, and then utilizing custom settings, the entire process is smooth and efficient, providing solid support for your website content operation and user experience optimization.


Common Questions (FAQ)

1. Why can't I get the current logged-in user's group name according to the example code?Answer: Please make sure the user is logged in on the website. Safe CMS'suserDetailThe label needs to rely on whether the user ID exists in the session when fetching the current logged-in user information. If the user is not logged in, or the login status cannot be correctly passed to the template context (for example, on some non-user center pages), you may not be able to retrieve ituser.IdThus, the subsequent user group information cannot be obtained. Please check your login status and template