Flexible display of user group names and introductions in AnQiCMS templates
AnQi CMS, as an efficient content management system, provides great convenience for website operators with its 'User Group Management and VIP System' feature, allowing for easy implementation of user classification, content permission control, and value-added member services.But in actual operation, how to naturally present these carefully set user group information, such as their names and detailed introductions, on the front-end template of the website, so that users can clearly understand their identity privileges or exclusive rights to various services, is a common requirement for template developers and content operators.
It is fortunate that AnQiCMS provides a powerful and intuitive template tag system, which we can take advantage of to achieve this.userGroupDetailTags can easily achieve this goal.
UnderstandinguserGroupDetailtags
userGroupDetailIs a template tag specially designed by AnQiCMS for obtaining user group details.The core function is to retrieve and output detailed data of a specified user group based on specified conditions, including its name, introduction, level, etc.
The 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变量名称It is optional, if set, you can store the obtained user group information in a variable for repeated use in the subsequent parts of the template.name="字段名称"Then specify which specific attribute of the user group you want to retrieve, such as the name or introduction. When specifying which user group information to retrieve, you can pass theid(ID) orlevelUse either of these two parameters to locate, choose one of them, and do not use both at the same time.
How to display the user group title (Title) and description (Description)
In AnQiCMS, the user group name corresponds toTitleField, while its detailed introduction corresponds toDescriptionfield.
For example, if you want to display the ID directly in the template1of the user group, you can write it like this:
<p>用户组名称:{% userGroupDetail with name="Title" id="1" %}</p>
Similarly, to display the ID of1Introduction of the user group:
<p>用户组介绍:{% userGroupDetail with name="Description" id="1" %}</p>
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 benefit of this approach is that you only need to execute it onceuserGroupDetailTags can be used to retrieve all available fields of a user group, avoiding duplicate 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 retrieve information through the level of user groups, for example, to get the level of2The user group (usually representing VIP level) details, just need to replaceidthe parameter withlevelas follows:
{# 获取等级为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 %}
Actual application scenarios
Display the user's group in the user center profile page:When a user logs in, we usually display the name and introduction of the user group they belong to in the personal center. This requires first obtaining the user group ID of the currently logged-in user (usually through the global
useran object oruserDetailTag 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 %}Display specific user group privileges on the content or service details 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 pages to guide users to upgrade.
en: `twig {# Assume some content)