In AnQi CMS template development, we often need to display different content based on specific conditions, or extract specific information from the system.User group management is a core function of AnQi CMS, which allows us to classify and control website users with precision.When you need to obtain the name and introduction based on the user group ID, Anqi CMS provides a concise and efficient template tag to achieve this goal.

UnderstandinguserGroupDetailTag

The AnQi CMS template engine supports Django template syntax, it provides a series of powerful tags, among whichuserGroupDetailTags are used to obtain detailed information about a specific user group.Whether you want to display the characteristics of different user groups on a membership system introduction page or show the detailed information of the user group the user belongs to in their personal center, this label can help you easily achieve it.

userGroupDetailThe basic usage of the label is as follows:{% userGroupDetail 变量名称 with name="字段名称" id="用户组ID" %}.

Here:

  • 变量名称Is an optional parameter. If you set the variable name (such asgroupInfo),then you can use this variable in the subsequent template code to access various fields of the user group. If you do not set the variable name, the label will directly output the value of the specified field.
  • nameThe parameter is used to specify the specific fields of the user group you want to retrieve, for exampleTitle(Group name) orDescription(Group description).
  • idThe parameter is the unique identifier ID of the user group, you need to provide this ID to accurately specify which user group's information you want to retrieve.

Get the name of the user group (Title)

To get the name of a specified user group, you canuserGroupDetailput in the tag.namethe parameter toTitleand provide the correspondingid.

For example, if we want to get the name of the user group with ID1, we can write it directly like this:

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

If you wish to store the obtained name in a variable for subsequent reuse or other logical judgments, you can do so:

{% userGroupDetail groupName with name="Title" id="1" %}
<div>当前用户组的名称是:{{ groupName }}</div>

By assigning the name togroupNameVariable, you can use it more flexibly in the template, for example as a title or in conditional statements.

Get user group introduction (Description)

Similarly, to get the introduction information of a user group, justnamethe parameter toDescription.

Suppose we still need to get the introduction of a user group with ID1, you can directly write it in the template like this:

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

If you need to store the introduction in a variable, the method is similar to getting the name:

{% userGroupDetail groupDescription with name="Description" id="1" %}
<div>当前用户组的详细介绍:{{ groupDescription }}</div>

In this way, you can conveniently display the introduction content of the user group at any location on the page.

Comprehensive example and practical application scenario

In the operation of a website, you may encounter various scenarios where you need to display user group information. For example, you have a VIP member page that needs to list the names and detailed privileges of each level:

<h3>VIP会员等级介绍</h3>
<div class="vip-level-card">
    {# 假设ID为1是普通会员 #}
    {% userGroupDetail group1Title with name="Title" id="1" %}
    {% userGroupDetail group1Desc with name="Description" id="1" %}
    <h4>{{ group1Title }}</h4>
    <p>{{ group1Desc }}</p>
</div>

<div class="vip-level-card">
    {# 假设ID为2是白银会员 #}
    {% userGroupDetail group2Title with name="Title" id="2" %}
    {% userGroupDetail group2Desc with name="Description" id="2" %}
    <h4>{{ group2Title }}</h4>
    <p>{{ group2Desc }}</p>
</div>

{# 更多用户组... #}

In addition, you can combine conditional judgments to display the corresponding user group name and introduction when a user visits a certain page according to the user group ID:

{# 假设当前用户ID为10,并已获取其用户组ID为currentUserIdGroup #}
{% userDetail currentUserGroupId with name="GroupId" id="10" %} {# 假设存在userDetail标签获取当前用户组ID #}

{% if currentUserGroupId %}
    {% userGroupDetail userGroupTitle with name="Title" id=currentUserGroupId %}
    {% userGroupDetail userGroupDesc with name="Description" id=currentUserGroupId %}
    <p>您好,您当前是:<span style="font-weight: bold;">{{ userGroupTitle }}</span>。</p>
    <p>用户组特权简介:{{ userGroupDesc }}</p>
{% else %}
    <p>您当前未加入任何用户组。</p>
{% endif %}

In the above example,currentUserGroupIdIt needs to go throughuserDetailtags to obtain the current user'sGroupIdField, this allows you to dynamically display information based on the actual identity of the user.

In addition to the name and introduction,userGroupDetailthe tag also supports retrieving other useful fields such asLevel(Group level),Price(Purchase price),FavorablePrice(Discount price) andSetting(User group settings, usually in key-value pair form). In a multi-site management environment, you can also usesiteIdThe parameter specifies obtaining user group information under a specific site, which provides great flexibility for multi-site operations.

Summary

Through the Anqi CMS template,userGroupDetailLabel, you can easily get the name and introduction of the user group according to the user group ID.Mastering the usage of this tag will allow you to have greater freedom and efficiency when it comes to personalized content display on websites and member function development.Remember to flexibly use label parameters and combine them with actual needs to build rich and user-friendly dynamic content.


Frequently Asked Questions (FAQ)

What will the template display if the specified user group ID does not exist?

If it passesidThe parameter-specified user group does not exist in the system,userGroupDetailThe tag will return an empty value (nil)。Therefore, after obtaining this information in the template, we usually cooperate with{% if 变量名 %}Such conditional judgment statements are used to check for the existence of values to avoid blank or erroneous display on the page and to provide friendly prompts.

Question 2: Besides the user group ID, what else can I use to get user group information?

Answer: Besides the user group ID (id), you can also uselevelparameters to get user group information.levelThe parameter represents the level of the user group. For example, if you want to get the group name of the level1, you can write it like this:{% userGroupDetail groupName with name="Title" level="1" %}. This is very useful when the ID is unknown but the level is known.

How to get the name and introduction of the current logged-in user's group?

To achieve this function, you need to take two steps first, usinguserDetailThe tag (assuming this tag can retrieve detailed information about the currently logged in user) to retrieve the details of the currently logged in userGroupId. Then, the retrievedGroupIdasuserGroupDetaillabel'sidPass the parameter to obtain the name and introduction of the current user's group. For example:

{% userDetail currentUser with name="GroupId" id=current_logged_in_user.Id %} {# 假设current_logged_in_user是全局或通过其他方式获取到的当前用户对象 #}
{% if currentUser %}
    {% userGroupDetail groupTitle with name="Title" id=currentUser %}
    {% userGroupDetail groupDesc with name="Description" id=currentUser %}
    <p>您的会员等级:{{ groupTitle }}</p>
    <p>特权说明:{{ groupDesc }}</p>
{% endif %}

(Please note that the example in thecurrent_logged_in_user.IdAdjust according to the method you actually use to obtain the current logged-in user ID.