In AnQiCMS template, dynamically obtaining and displaying the unique identifier of the user group is a key step to realizing the personalized content display and user permission management of the website.AnQiCMS provides flexible and powerful template tags to help us easily achieve this goal.Next, we will discuss how to use these tags to accurately retrieve and present user group information.

Understanding the user group identifier in AnQi CMS

In AnQiCMS, user groups are the foundation for managing user permissions and content access.Each user will be assigned to a specific user group after registration or allocation by an administrator.These user groups usually have their own unique identifiers (Id), names (Title), and levels (Level) and other attributes.In the template, it can dynamically identify the user group of the current visited user, allowing us to provide customized page layout, display exclusive content, and even control the visibility of certain functions according to the user's identity, thus greatly enhancing user experience and website operational efficiency.

The 'unique identifier' of a user group usually refers to its internal numeric ID, which is unique throughout the entire website.Through this ID, we can further query other detailed information of this user group, such as the name of the user group, level, or specific settings.

第一步:获取当前用户的用户组ID

To dynamically obtain the unique identifier of the user group, we first need to know which user group the current visitor belongs to. AnQiCMS provides a very practical template tag—userDetail.

userDetailThe label is mainly used to obtain the detailed information of the currently logged-in user. When we do not specify a specific user ID, it will automatically query the user in the current session. By specifyingname="GroupId"Parameters, we can easily obtain the user group ID of the current user.

Here is a simple code snippet that shows how to get the current user's group ID and store it in a variable:

{% userDetail currentUserGroupId with name="GroupId" %}

In this code,currentUserGroupIdWill carry the current logged-in user's user group ID. If the user has not logged in, or there is no clear user group,currentUserGroupIdThis will usually be an empty value or 0, which needs to be noted in the subsequent logic judgment. Obtaining this ID actually means obtaining the 'unique identifier' of the user group.

第二步:Through the user group ID, obtain detailed user group information (optional)

Although the unique identifier of the user group has been obtained in the previous step (i.e.GroupId),但在实际应用中,我们可能更希望展示用户组的名称(例如“普通会员”、“VIP会员”)或者根据用户组的等级进行判断。这时,我们可以利用AnQiCMS的另一个模板标签——userGroupDetail.

userGroupDetailTags can query and return detailed information of the user group based on the provided user group ID or level. We will use the information obtained in the first step.currentUserGroupIdasuserGroupDetailTagsidParameters can be used to retrieve all available attributes of the user group.

The following is an example of retrieving detailed information about a user group and displaying its name and level:

{% userDetail currentUserGroupId with name="GroupId" %}
{% if currentUserGroupId %} {# 确保用户已登录且有用户组 #}
    {% userGroupDetail currentGroupDetail with id=currentUserGroupId %}
    <p>您当前的用户组ID是:{{ currentGroupDetail.Id }}</p>
    <p>您的用户组名称是:{{ currentGroupDetail.Title }}</p>
    <p>您的用户组等级是:{{ currentGroupDetail.Level }}</p>
{% else %}
    <p>您尚未登录,无法显示用户组信息。</p>
{% endif %}

In this code,currentGroupDetailWill become an object that includes information about the user group,Id(unique identifier),Title(Name),Level(level) and other fields. You can access these fields directly in the template to display information.

Useful scenarios and code examples

Combine the above two steps, and we can achieve various personalized displays based on user groups. For example, display different greetings or specific content based on the level of the user group:

{# 获取当前用户的用户组ID #}
{% userDetail currentUserGroupId with name="GroupId" %}

{# 判断用户是否登录以及是否有用户组 #}
{% if currentUserGroupId %}
    {# 通过用户组ID获取用户组的详细信息 #}
    {% userGroupDetail currentGroupDetail with id=currentUserGroupId %}

    <div class="user-welcome">
        <h3>您好,尊贵的{{ currentGroupDetail.Title }} 用户!</h3>
        <p>您的用户组唯一标识是:{{ currentGroupDetail.Id }}</p>

        {% if currentGroupDetail.Level >= 5 %} {# 假设等级5及以上是VIP用户 #}
            <div class="vip-content">
                <p>作为我们的高级VIP用户,您可以享受专属的折扣优惠和优先客服支持!</p>
                <a href="/vip-benefits" class="btn btn-primary">查看VIP特权</a>
            </div>
        {% elif currentGroupDetail.Level >= 2 %} {# 普通付费用户 #}
            <div class="paid-user-content">
                <p>感谢您成为我们的{{ currentGroupDetail.Title }}用户。您已解锁部分高级功能。</p>
                <a href="/upgrade" class="btn btn-secondary">升级到更高等级</a>
            </div>
        {% else %} {# 免费用户 #}
            <div class="free-user-content">
                <p>您是我们的{{ currentGroupDetail.Title }}用户。立即升级到VIP,体验更多专属功能吧!</p>
                <a href="/upgrade" class="btn btn-info">了解如何升级</a>
            </div>
        {% endif %}
    </div>
{% else %}
    <div class="guest-welcome">
        <h3>欢迎访问我们的网站!</h3>
        <p>登录后您可以体验更多个性化服务。</p>
        <p><a href="/login">立即登录</a> 或 <a href="/register">免费注册</a></p>
    </div>
{% endif %}

By this method, you can flexibly insert conditional judgments in the templatecurrentGroupDetail.LevelorcurrentGroupDetail.Idbased on the value{% if %}to achieve refined content management and user experience.

Summary

In AnQiCMS, dynamically retrieving and displaying the unique identifiers of user groups is the foundation for website personalization and refined operation.userDetailLabel gets the user group ID of the current user and combines it withuserGroupDetailLabel to obtain the detailed information of the user group, we can easily display the user group name, level and other information in the template, and make conditional judgments accordingly, providing customized content and services to different users.Mastering the use of these tags will bring greater flexibility and creativity to the operation of your website.


Common Questions (FAQ)

  1. Q: If the user is not logged in,userDetailwhat will the tag return?A: When the user is not logged in,userDetailLabel attempts to retrieveGroupIdThis field requires user identity and usually does not return a valid value (such as an empty string or