How to get and display detailed information of user groups in AnQiCMS template?

Calendar 👁️ 72

In AnQiCMS, user group management is one of the core functions for building differentiated services and content monetization systems.It is crucial to display detailed information about user groups accurately in the front-end template, whether it is to provide exclusive content for VIP users or to set access restrictions for users of different permission levels.This not only improves the user experience, but also effectively guides users to understand and upgrade their own permissions.

How can we flexibly obtain and display the detailed information of these user groups in the AnQiCMS template? AnQiCMS provides special template tagsuserGroupDetailHelp us achieve this goal.

UnderstandinguserGroupDetailTag

userGroupDetailThe tag is mainly used to obtain detailed data for a single user group. Its basic usage is very intuitive, allowing us to accurately query through the user group ID or its level.

Basic usage:

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

Or

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

Here变量名称Any variable name you define can be used to refer to the user group information obtained.nameThe parameter specifies the specific field you want to retrieve.idandlevelare two key parameters, but remember,they are mutually exclusive, you can only choose one to specify the user group to query. If provided at the same time, the system will prioritize usingid.

The detailed fields that can be obtained include:

  • IdThe unique identifier ID of the user group.
  • TitleThe name of the user group, such as 'Regular Member', 'Gold VIP', etc.
  • DescriptionThe detailed introduction of the user group.
  • LevelGroup level, usually a number used for permission sorting.
  • PricePurchase price of the user group, if it is a paid group.
  • FavorablePriceDiscount price of the user group, if available.
  • SettingThis is a very flexible field, which stores the custom settings of user groups in the form of key-value pairs, such as "discount ratio", "exclusive service link", and so on.

Perform the actual operation: Retrieve and display user group information

Let's look at a few examples to see how to use templatesuserGroupDetail.

1. Display the name and introduction of a specific user group

Assuming we want to display the name and description of the user group with ID 1:

{# 通过用户组ID获取用户组详情 #}
{% userGroupDetail commonGroup with id="1" %}
  <p>用户组名称:{{ commonGroup.Title }}</p>
  <p>用户组介绍:{{ commonGroup.Description }}</p>
{% enduserGroupDetail %}

{# 或者通过用户组等级获取,假设等级1对应的也是ID为1的用户组 #}
{% userGroupDetail basicLevelGroup with level="1" %}
  <p>基本等级用户组名称:{{ basicLevelGroup.Title }}</p>
  <p>基本等级用户组等级:{{ basicLevelGroup.Level }}</p>
{% enduserGroupDetail %}

In this example, we first definedcommonGroupandbasicLevelGrouptwo variables to hold the user group information, and then accessed.using the symbolTitleandDescriptionfield.

2. Display user group pricing information

If our website has VIP paid members, we may need to display prices for different VIP levels:

{# 显示黄金VIP用户组的价格信息,假设ID为2 #}
{% userGroupDetail goldVIPGroup with id="2" %}
  <h3>{{ goldVIPGroup.Title }}</h3>
  <p>原价:{{ goldVIPGroup.Price }}元</p>
  {% if goldVIPGroup.FavorablePrice > 0 %}
    <p>优惠价:{{ goldVIPGroup.FavorablePrice }}元</p>
  {% endif %}
{% enduserGroupDetail %}

Here we also added a simpleifThe judgment is only displayed when the discount price exists (greater than 0).

3. Flexible handlingSettingCustom settings in the field

SettingThe field is in key-value pair form, which allows us to add various custom attributes for user groups in the background.For example, we can set a "discount ratio" of 0.8 (i.e., an 80% discount) for the "Gold VIP" user group in the background.

{% userGroupDetail goldVIPGroup with id="2" %}
  <h3>{{ goldVIPGroup.Title }}</h3>
  <p>介绍:{{ goldVIPGroup.Description }}</p>
  {# 获取自定义设置中的“折扣比例” #}
  {% if goldVIPGroup.Setting.discountRate %}
    <p>享有商品 {{ goldVIPGroup.Setting.discountRate * 100 }}% 的折扣!</p>
  {% endif %}
  {# 也可以遍历所有的设置项 #}
  {% if goldVIPGroup.Setting %}
    <h4>更多专属特权:</h4>
    <ul>
      {% for key, value in goldVIPGroup.Setting %}
        <li>{{ key }}:{{ value }}</li>
      {% endfor %}
    </ul>
  {% endif %}
{% enduserGroupDetail %}

Please note,goldVIPGroup.Setting.discountRateThis syntax can be accessed directlySettingThe key in the object. If you need to display all custom settings, you canfor key, value in goldVIPGroup.Settingfor traversal.

4. Get the user group information of the currently logged-in user

In practical applications, we often need to display the user group information of the currently logged-in user. This requires combininguserDetailtags to first obtain theGroupId, and then useuserGroupDetailto obtain the user group details.

{# 假设我们已经获取了当前登录用户的ID,例如通过一个全局变量 currentUser.Id #}
{# 或者直接通过userDetail标签获取当前用户的GroupId #}
{% userDetail currentUser with id="当前用户的ID" %} {# 或者如果当前页面是用户中心,可能可以直接获取当前用户ID #}
  {% if currentUser.GroupId %}
    {# 获取该用户所属的用户组详情 #}
    {% userGroupDetail userAssignedGroup with id=currentUser.GroupId %}
      <div class="user-group-info">
        <p>您当前的用户组是:<strong>{{ userAssignedGroup.Title }}</strong> (等级: {{ userAssignedGroup.Level }})</p>
        <p>用户组介绍:{{ userAssignedGroup.Description }}</p>
        {% if userAssignedGroup.Setting.vipBenefits %}
          <p>您享有的VIP特权:{{ userAssignedGroup.Setting.vipBenefits }}</p>
        {% endif %}
      </div>
    {% enduserGroupDetail %}
  {% else %}
    <p>您当前未加入任何用户组。</p>
  {% endif %}
{% enduserDetail %}

Here, we first go throughuserDetailtag to getcurrentUserA variable that contains the user'sGroupId. Then, we will use thisGroupIdpass touserGroupDetaillabel to successfully retrieve and display the detailed information of the user's group.

Some points to note

  • Parameter selection: In useuserGroupDetailAt the time, make sureidandlevelChoose only one of the two parameters to provide. If you have both, it is usually usedidwill be more accurate
  • Handling empty valuesBefore attempting to retrieve user group information, it is best to check if the user group ID or level exists, or when using.Titleconsider the possibility that these fields may be empty and addifJudge or use the default value filter to avoid abnormal display on the page. For example{{ userAssignedGroup.Title|default:"无用户组名称" }}.
  • SettingField dynamics:SettingThe content of the field depends entirely on the backend configuration, so when accessing it in the template, it is necessary to clearly understand which key-value pairs the backend has set in order to accurately pass throughuserGroupDetail.Setting.你的键名Get it.

ByuserGroupDetailLabel, AnQiCMS makes the display of user group information simple and powerful, helping us to better build personalized website experiences and business models.


Frequently Asked Questions (FAQ)

  1. If the user is currently not assigned any user group, a call is made in the templateuserGroupDetailWhat will be displayed? How should we handle this situation?Answer: IfuserGroupDetaillabel'sidorlevelThe value is empty or invalid (for example, the user is not associated with a user group, or the user group ID does not exist), thenuserGroupDetailThe variable defined by the tag (as in the example above)userAssignedGroupwill not be filled with data, or its internal fields (such asTitlewill be empty. To avoid page display errors, it is recommended to use the template.ifCheck if a variable exists or if its key field is empty. For example, after callinguserGroupDetailyou can judge like this:{% if userAssignedGroup.Title %}Ensure that information is displayed only when the user group title exists, or usedefaultthe filter to provide default text.

  2. SettingHow to flexibly retrieve and display custom settings such as 'VIP exclusive discount' in the template?Answer:SettingThe field is an object or a map, you can access specific keys directly through dot notation (.). For example, if the backend is in a user group'sSettingSet in ChinesediscountRateThis key, you can pass through{{ userGroup.Setting.discountRate }}Get the value.

Related articles

How to parse and output HTML code in AnQiCMS template instead of escaping it?

When using AnQi CMS for website content creation and template design, we often encounter a problem related to HTML code display: Why is the HTML code I enter in the background editor displayed as plain text on the front page instead of being parsed by the browser as actual HTML elements?This is actually the content management system that defaults to enabling HTML content escaping for website security.This article will delve into how to effectively control the output of HTML code in the AnQiCMS template, ensuring that it is parsed correctly and not displayed as escaped.

2025-11-07

How to get the thumbnail of AnQiCMS image resources and display it?

AnQiCMS focuses on visual effects and page loading efficiency in content display, therefore, how to efficiently obtain and display thumbnail images of image resources is a common problem we encounter in daily operations.It is fortunate that AnQiCMS provides a very convenient and flexible way to handle image thumbnails, allowing us to easily meet various display needs. --- ### How to manage and generate image thumbnails in AnQiCMS?

2025-11-07

How to format floating-point numbers and retain a certain number of decimal places in AnQiCMS templates?

In website content operation, we often need to display floating-point numbers such as prices, percentages, ratings, and so on.The precision and display method of these numbers directly affect the user experience and the professionalism of information.AnQi CMS knows this need and provides a powerful and flexible floating-point number formatting function in the template engine, allowing you to easily control the display accuracy of floating-point numbers.Next, we will explore two main methods of formatting floating-point numbers and retaining specified decimal places in AnQiCMS templates: the `floatformat` filter and the `stringformat` filter

2025-11-07

How to automatically wrap long text in AnQiCMS templates for optimized reading experience?

In website content operation, optimizing the reading experience is a key factor in attracting and retaining users.Especially for long text content, if there is a lack of proper formatting and automatic line breaking, users may face issues such as horizontal scrolling and text stacking, which can severely affect reading comfort and even lead to user loss.As an AnQiCMS user, we can take advantage of its powerful template engine and built-in features to easily implement automatic line breaks in long text, thereby greatly enhancing the reading experience of the website content.## The Importance of Understanding Long Text Line Breaks Imagine browsing an article on your phone

2025-11-07

How to display the language switch options and hreflang tags on a multilingual site in AnQiCMS?

Building multilingual websites in AnQiCMS and providing convenient language switch options as well as correct hreflang tags is a key step in expanding the international market and improving user experience.AnQiCMS with its flexible architecture provides a clear path to achieve this goal, allowing your website content to accurately reach global users while ensuring search engine friendliness. ### Overview of AnQiCMS Multilingual Implementation AnQiCMS integrates multilingual support with multi-site management functions in its design.This means, each different language version

2025-11-07

How to manage website navigation links and display them on the front page in AnQiCMS?

When building and managing a website, a clear and intuitive navigation system is the foundation of user experience and search engine optimization.AnQiCMS provides a flexible and powerful navigation management mechanism, allowing you to easily organize the website structure and elegantly present it to users. ### One, Back-end Navigation Management: Build the Skeleton of Your Website The navigation management feature of AnQiCMS is located in the "Back-end Settings" menu. Click on "Navigation Settings" to enter.Here, you can build a clear navigation system for the website like stacking blocks.

2025-11-07

How to set contact information in AnQiCMS and display it on the front page (such as phone number, address, WeChat)?

In today's digital age, a website's contact information is not only for displaying information but also a bridge for communication between the enterprise and the customer.Clear and easily accessible contact information can effectively enhance customer trust and promote business conversion.AnQiCMS (AnQi Content Management System) knows this well and provides a simple and efficient way to set up and display various contact methods, whether it's phone, address, or WeChat. Let's take a step-by-step look at how to configure and display this key information in AnQiCMS.### First step: Set your contact information in the background First

2025-11-07

How to loop and display the titles (ContentTitles) of AnQi CMS documentation in the front-end template?

Dynamically display document content title: The Loop and Application of ContentTitles in AnQiCMS front-end template When operating website content with AnQiCMS, we often need to provide users with a better reading experience and navigation convenience. One very practical feature is to automatically generate the article table of contents (Table of Contents).AnQiCMS considers this very carefully, it can automatically extract the titles from the document content and provide them to the front-end template in the form of `ContentTitles`. Today

2025-11-07