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

UnderstandinguserGroupDetailtags

The template engine of AnQi CMS supports Django template syntax, it provides a series of powerful tags, among whichuserGroupDetail

userGroupDetailThe basic usage of the tag 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 subsequent template code to access the various fields of the user group. If you do not set a variable name, the tag 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 example,Title(User group name) or,Description(User group introduction).
  • idThe parameter is the unique identifier ID for 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 tagnameparameter settingsTitleand provide the correspondingid.

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

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

If you want to store the obtained name in a variable for subsequent reuse or other logical judgments, you can do it like this:

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

By assigning the name togroupNameVariable, you can use it more flexibly in templates, such as for titles or in conditional statements.

Get user group introduction (Description)

Similarly, to get the introduction information of a user group, you only need tonameparameter settingsDescription.

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

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

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

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

In this way, you can easily display the introduction of the user group at any position on the page.

Comprehensive example and practical application scenarios

In the operation of actual websites, 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 also combine conditional judgment to display the corresponding user group name and introduction when a user visits a certain page, according to the user group ID they belong to:

{# 假设当前用户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,currentUserGroupIdyou need to go throughuserDetailtags to obtain the current user's GroupIdFields, this allows you to dynamically display information based on the actual identity of the user.

Besides the name and description,userGroupDetailtags also support getting other useful fields, such asLevel(Group level)、Price(Purchase Price),FavorablePrice(Discount Price) andSetting(User group settings, usually in key-value pair format). In a multi-site management environment, you can alsositeIdThe parameter specifies the retrieval of user group information under a specific site, which provides great flexibility for multi-site operations.

Summary

In the templates of Anqi CMS,userGroupDetailLabel, you can easily get the name and introduction of the user group based on the user group ID.Master the usage of this tag, and you will have more freedom and efficiency when performing personalized content display on websites and developing member functions.Remember, flexibly using label parameters and combining them with actual needs can help build rich and user-friendly dynamic content.


Common Questions (FAQ)

问1:If the specified user group ID does not exist, what will the template display?

答:If throughidthe parameters specify a user group that 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 变量名 %}This conditional judgment statement is used to check if the value exists to avoid blank or error display on the page and to provide a friendly prompt.

问2:除了用户组ID,我还能用什么来获取用户组信息?

答:除了用户组ID (id),你还可以使用level参数来获取用户组信息。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.

问3:How to get the name and description of the user group to which the current logged-in user belongs?

答:To achieve this functionality, you need to take two steps. First, useuserDetailLabel (assuming this label can retrieve the detailed information of the current logged-in user) to retrieve the detailed information of the current logged-in userGroupId. Then, the obtainedGroupIdasuserGroupDetailTagsidParameters are passed in to obtain the name and introduction of the current user's user 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 examples in thecurrent_logged_in_user.Id需要根据你实际获取当前登录用户ID的方法进行调整。)