Display user group names and descriptions flexibly in AnQiCMS templates

AnQi CMS as an efficient content management system, its "User Group Management and VIP System" function provides great convenience for website operators, allowing them to easily implement user classification, content permission control, and value-added membership services.But in actual operation, how to naturally present these carefully designed user group information, such as their names and detailed introductions, on the website front-end template, so that users can clearly understand their identity privileges or exclusive rights to various services, is a demand that template developers and content operators often encounter.

幸运的是,AnQiCMS provides a powerful and intuitive template tag system, we can take advantage of itsuserGroupDetail tags to easily achieve this goal.

UnderstandinguserGroupDetailTag

userGroupDetailIs the template tag specially designed for AnQiCMS to obtain user group details.Its core function is to retrieve and output detailed data of a specified user group based on specified conditions, including its name, introduction, level, etc.

Its basic usage is as follows:

{% userGroupDetail 变量名称 with name="字段名称" id="用户组ID" %}

Or, if you prefer to get it based on the user group level:

{% userGroupDetail 变量名称 with name="字段名称" level="用户组等级" %}

Here变量名称Optional, if set, you can store the user group information obtained in a variable for repeated use in the subsequent part of the template.name="字段名称"Specify which specific attribute of the user group you want to retrieve, such as name or introduction. When specifying which user group information to retrieve, you can use the user groupid(ID) orlevel(Level) Choose one of these parameters to locate, either one can be used, but not both at the same time.

How to display the user group name (Title) and description (Description)

In AnQiCMS, the name of the user group corresponds toTitlefield, and its detailed introduction corresponds toDescriptionfield.

For example, if you want to display the ID directly in the template as1to write the name of the user group

<p>用户组名称:{% userGroupDetail with name="Title" id="1" %}</p>

Similarly, to display the ID of1Group Introduction:

<p>用户组介绍:{% userGroupDetail with name="Description" id="1" %}</p>

In order to handle and display this information more flexibly, we usually assign the obtained user group details to a variable and then access different properties through this variable. The advantage of this is that you only need to do it onceuserGroupDetailTags, you can get all available fields of the user group, avoiding repeated queries.

{# 假设我们要获取ID为1的用户组的名称和介绍 #}
{% userGroupDetail groupInfo with id="1" %}
{% if groupInfo %}
    <p>用户组名称:<strong>{{ groupInfo.Title }}</strong></p>
    <p>用户组介绍:{{ groupInfo.Description }}</p>
{% else %}
    <p>未找到ID为1的用户组信息。</p>
{% endif %}

If you want to get information through the level of the user group, for example, to get the level of2The details of the user group (usually representing VIP level), just need toidparameter withleveland it is done:

{# 获取等级为2的用户组的名称和介绍 #}
{% userGroupDetail vipGroupInfo with level="2" %}
{% if vipGroupInfo %}
    <p>VIP用户组名称:<strong>{{ vipGroupInfo.Title }}</strong></p>
    <p>VIP用户组介绍:{{ vipGroupInfo.Description }}</p>
{% else %}
    <p>未找到等级为2的用户组信息。</p>
{% endif %}

Application scenarios in practice

  1. Display the user's group in the user center profile page:After the user logs in, we usually display the name and introduction of the user group to which the user belongs in the personal center. This requires first obtaining the user group ID of the currently logged-in user (usually through globaluseror objectuserDetailLabel retrieval), then use the ID to query the user group details.

    {# 假设当前登录用户的用户组ID可通过 {{ user.GroupId }} 获得 #}
    {% if user.GroupId %}
        {% userGroupDetail currentUserGroup with id=user.GroupId %}
        {% if currentUserGroup %}
            <div class="user-group-info">
                <h3>您当前的用户组:{{ currentUserGroup.Title }}</h3>
                <p>{{ currentUserGroup.Description }}</p>
                {# 还可以显示VIP过期时间等,如果userDetail中有相关字段 #}
            </div>
        {% endif %}
    {% else %}
        <p>您目前是普通访客,登录可查看更多权益。</p>
    {% endif %}
    
  2. Display the privileges of a specific user group on the content or service detail page:If your website offers paid content or VIP exclusive services, you can directly display the names and introductions of specific user groups on the corresponding detail page, guiding users to upgrade.

    "twig {# Assume some content