In the website built with AnQiCMS, the user grouping feature provides us with powerful content management and user permission division capabilities. Whether it is to implement membership content, display different information based on user level, or plan VIP services, user grouping is an indispensable basic feature.How to flexibly display these user group information on the front-end template of the website is a focus for many operators.

AnQiCMS uses a template engine syntax similar to Django, which makes template writing both intuitive and efficient. When it comes to user group information, the system providesuserGroupDetailLabel, its main function is to obtain detailed data of a single user group.

UnderstandinguserGroupDetailtags

userGroupDetailLabel is the key to obtaining user group information in the template. It can be used to obtain the user groupIDorLevelGet detailed data for a specific user group.

Basic usage of the tag

{% userGroupDetail 变量名称 with name="字段名称" id="1" %}

or usinglevelParameters:

{% userGroupDetail 变量名称 with name="字段名称" level="1" %}

Here,变量名称Is the custom name you have set for the user group information obtained.name="字段名称"Then specifies which specific field of the user group you want to obtain.idorlevelIt is used to precisely specify which user group.

userGroupDetailThe fields that can be retrieved include:

  • Id: The unique identifier ID of the user group.
  • Title: User group display name, for example “General Member”, “Gold VIP”.
  • Description: Detailed introduction of the user group.
  • Level: Level of the user group.
  • Price: Purchase price of the user group.
  • FavorablePrice: User group's discount price.
  • Setting: User group's detailed configuration information, which is a key-value pair (map) structure and contains all the custom attributes set by the backend for this user group.

For example, if we need to display the name and introduction of the user group with ID 1 on the page, we can use it like this:

{# 获取ID为1的用户组名称 #}
<div>用户组名称:{% userGroupDetail with name="Title" id="1" %}</div>

{# 获取ID为1的用户组介绍,并存储到变量中以便后续使用 #}
{% userGroupDetail groupDesc with name="Description" id="1" %}
<div>用户组介绍:{{ groupDesc }}</div>

Strategy for cyclic display of user group information

Although AnQiCMS template language does not provide a direct oneuserGroupListsuch tag to get the list of all user groups at one time, but we can combineuserGroupDetailtags and templatesforThe loop feature is used to iterate and display user group information. This usually requires us to pre-knowledge or be able to obtain the ID or Level list of all user groups.

Scene one: The number of user groups is relatively small and fixed

If the number of user groups on your website is not many (for example: ordinary members, silver members, gold members, diamond members these levels), and their IDs or Levels are fixed, you can adopt a "semi-manual" way to simulate the circular display.

Assume your user group IDs are 1, 2, 3, 4:

<div class="user-group-list">
    {# 显示普通会员信息 #}
    <div class="group-item">
        {% userGroupDetail group1 with id="1" %}
        <h3>{{ group1.Title }}</h3>
        <p>{{ group1.Description }}</p>
        <p>价格:{{ group1.Price }}</p>
        <a href="/register?group={{ group1.Id }}">立即加入</a>
    </div>

    {# 显示白银会员信息 #}
    <div class="group-item">
        {% userGroupDetail group2 with id="2" %}
        <h3>{{ group2.Title }}</h3>
        <p>{{ group2.Description }}</p>
        <p>价格:{{ group2.Price }}</p>
        <a href="/register?group={{ group2.Id }}">立即加入</a>
    </div>

    {# 依此类推... #}
</div>

This method is not strictly a 'loop', but when the number of groups is limited and does not change frequently, it can quickly meet the needs.

场景二:结合后端数据实现动态循环(推荐且更灵活)

The more flexible and recommended approach is, if your AnQiCMS backend (or through custom development) can provide an array variable containing all user group IDs or Levels to the template, then we can utilize the template'sforPerforming dynamic display in a loop.

For example, assuming your backend configuration or custom interface can pass a parameter namedallUserGroupLevelsan array that includes the Level values of all user groups (such as[1, 2, 3, 4, 5]):

<div class="user-group-overview">
    <h2>会员等级一览</h2>
    {% for level in allUserGroupLevels %}
        {# 遍历每个 Level,并获取对应的用户组详情 #}
        {% userGroupDetail userGroup with level=level %}
        <div class="user-group-card">
            <h3>{{ userGroup.Title }} (等级: {{ userGroup.Level }})</h3>
            <p>{{ userGroup.Description }}</p>
            <p>原价: {{ userGroup.Price }} / 优惠价: {{ userGroup.FavorablePrice }}</p>
            {# 如果用户组有自定义设置,也可以在这里显示 #}
            {% if userGroup.Setting.SpecialFeature %}
                <p>专属特权: {{ userGroup.Setting.SpecialFeature }}</p>
            {% endif %}
            <a href="/upgrade?level={{ userGroup.Level }}">了解详情</a>
        </div>
    {% endfor %}
</div>

This example shows how to loop over an assumedallUserGroupLevelsarrayforloop, and useuserGroupDetailtags to get the user group details of the current Level.SettingThe field is a key-value pair, which can be accessed directly byuserGroup.Setting.键名to access the custom attributes configured for the user group by the backend.

How to getallUserGroupLevelssuch data?

This usually requires the website administrator to predefine a list containing all user group Levels or IDs in some configuration item of the AnQiCMS backend (such as the custom parameters of 'Global Settings', or through customized 'Content Models' to manage user group levels), and then ensure that this list variable can be passed to the front-end template.If the default function cannot directly provide such a list, some lightweight backend development or background feature expansion may be required to assist in implementation.

Considerations in practical application

  • Page loading efficiency: If the number of user groups is very large, and eachuserGroupDetailLabels are triggered once for each database query, which may affect the page loading speed.But usually, the number of user groups will not be too many, AnQiCMS also has an internal caching mechanism to optimize performance.
  • data managementEnglish: When managing user groups in the background, it is imperative to maintain the uniqueness and stability of the ID or Level, and avoid changing them arbitrarily to prevent affecting the frontend display.
  • Template DesignYou can flexibly design the display styles for different user groups based on the user group data you obtain, such as adding unique badges, colors, or feature descriptions for VIP user groups.

Through the above method, even without a direct "user group list" label, we can effectively loop and display user group information in the AnQiCMS template, meeting various operational needs.


Common Questions and Answers (FAQ)

Q1: How can I display a specific setting (Setting) item of the user group?A1: User group:SettingThe field is a key-value pair (map) structure that contains all the custom attributes configured by the backend administrator for the user group.You can directly access the specific items using dot notation.{{ userGroup.Setting.SpecialFeature }}It is recommended to use it first,{% if userGroup.Setting.SpecialFeature %}Perform a judgment to ensure that this field exists to avoid template errors.

Q2:userGroupDetailTagsidandlevelWhat is the difference between the parameters and when to use which?A2:idThe parameter is used to retrieve information through the unique identifier (ID) of the user group, which is automatically generated by the system.levelParameters are obtained through the user group's level, which is usually manually set by the administrator in the background to represent the hierarchical relationship of the user group (such as Level 1, Level 2, Level 3). Generally, if you define a clear level concept for user groups in the background and this level is a key factor in your template's logical judgment (such as sorting by level), then uselevelIt will be more intuitive. If your backend can provide a list of user group IDs, or if you need to refer to a specific user group regardless of its level, then useidWould be more suitable. In practical applications, you can choose one according to the user group data type you have.

Q3: Can I allow users to select a user group and register directly on the website frontend, and then display different user group information based on their selection?A3:userGroupDetailTags are mainly used to display the static information of user groups on the front end.If you want to implement the function of selecting user groups when users register or when users upgrade to a certain user group, this belongs to user interaction and data writing operations, which usually requires combining the AnQiCMS user registration/login module and the provided user management API to achieve.The front-end template can be used to display the options for the user groups that can be selected, but to submit the user's choice and update their user group information, backend logic processing is required.