In AnQiCMS template, dynamically obtaining and displaying the unique identifier of the user group is a crucial step in achieving personalized content display on the website and user permission management.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 obtain and present user group information.

Understanding user group identification in AnQi CMS

In AnQiCMS, user groups are the foundation for managing user permissions and content access.Each user is assigned to a specific user group after registration or 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 visitor, allowing us to provide customized page layouts, display exclusive content, even control the visibility of certain features, thereby greatly enhancing the user experience and the operation efficiency of the website.

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

Step 1: Get the current user's group ID

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

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

Here is a simple code snippet demonstrating how to obtain the user group ID of the current user and store it in a variable:

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

In this code block,currentUserGroupIdIt will carry the user group ID of the currently logged-in user. If the user has not logged in, or does not have a clear user group,currentUserGroupIdThis will usually be empty or 0, which needs to be taken into account in subsequent logic judgments. Obtaining this ID actually gets the 'unique identifier' of the user group.

The second step: Get user group details through user group ID (optional)

Although the unique identifier of the user group has already been obtained in the previous step, i.e.GroupId),But in practice, we may prefer to display the name of the user group (such as "General Member", "VIP Member") or judge according to the level of the user group. At this time, we can use another template tag of AnQiCMS——userGroupDetail.

userGroupDetailThe label can query and return the user group details according to the provided user group ID or level. We will obtain the information obtained in the first step.currentUserGroupIdasuserGroupDetaillabel'sidParameters can be used to obtain all available attributes of the user group.

Here is an example of obtaining user group details 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 block,currentGroupDetailWill become an object that includes the user groupId(Unique identifier),Title(Name),Level(Level) and other fields. You can directly access these fields in the template as needed to display information.

Practical scenarios and code examples

Combining the above two steps, we can achieve various personalized displays based on user groups. For example, displaying 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 %}

In this way, you can usecurrentGroupDetail.LevelorcurrentGroupDetail.Idthe value to flexibly insert conditional judgments in{% if %}tags), thus achieving refined content management and user experience.

Summary

In AnQiCMS, dynamically obtaining and displaying the unique identifier of user groups is the foundation for realizing website personalization and refined operation. ThroughuserDetailLabel to retrieve the current user's group ID and combineuserGroupDetailLabel to retrieve detailed information about the user group, we can easily display the user group name, level, and other information in the template, and make conditional judgments based on this to provide customized content and services to different users.Mastering the use of these tags will bring greater flexibility and creativity to your website operation.


Frequently Asked Questions (FAQ)

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