On a website built with AnQiCMS, the user grouping function 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 foundation.How to flexibly display these user group information in the front-end template of the website, which is a focus for many operators.
AnQiCMS uses a template engine syntax similar to Django, which makes template writing intuitive and efficient. The system providesuserGroupDetailThe tag, its main function is to obtain detailed data of a single user group.
UnderstandinguserGroupDetailTag
userGroupDetailThe tag is the key to obtaining user group information in the template. It can be based on the user group'sIDorLevelTo retrieve detailed data for a specific user group.
Basic usage of tags
{% userGroupDetail 变量名称 with name="字段名称" id="1" %}
Or use:levelparameters:
{% userGroupDetail 变量名称 with name="字段名称" level="1" %}
Here, 变量名称Is the name you have customized for the user group information you have obtained,name="字段名称"Then specifies which specific field of the user group you want to retrieve.idorlevelIt is used to precisely specify which user group.
userGroupDetailThe fields that can be obtained include:
Id: The unique identifier ID of the user group.Title: The display name of the user group, such as 'Regular Member', 'Gold VIP'.Description: Detailed introduction of the user group.Level: The level of the user group.Price: The purchase price of the user group.FavorablePrice: The discount price for the user group.Setting: Detailed configuration information for the user group, which is a key-value (map) structure containing 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 the AnQiCMS template language does not provide a directuserGroupListtag to retrieve the list of all user groups at once, but weuserGroupDetailcan combine tags and templates to achieve thisforThe loop feature is used to traverse and display user group information. This usually requires us to know or be able to obtain a list of all user group IDs or Level lists in advance.
Scenario 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 at these levels), and their ID or Level is fixed, you can use a semi-manual method to simulate the cyclic display.
Assuming 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 it can quickly meet the needs when the number of groups is limited and does not change frequently.
Scene two: Implementing dynamic looping by combining backend data (recommended and more flexible)
A more flexible and recommended approach is to provide an array variable containing all user group IDs or Levels to the template via the AnQiCMS backend (or through custom development), then we can make use of the template'sforLoop for dynamic display.
For example, assuming your backend configuration or custom interface can pass an array namedallUserGroupLevelswhich contains all the Level values of the 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 perform a loop in a hypotheticalallUserGroupLevelsarray.forLoop, and use tags to get the user group details of the current Level in each iteration.userGroupDetail.SettingThe field is a key-value pair and can be accessed directlyuserGroup.Setting.键名To access the custom properties configured for the user group on the backend
How to obtainallUserGroupLevelsIs this kind of data?
This usually requires the website administrator to predefine a list containing all user group Levels or IDs in a configuration item in the AnQiCMS backend (such as custom parameters in "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 backend feature expansion may be required to assist in implementation.
Considerations in practical applications.
- Page loading efficiencyIf the number of user groups is very large and each
userGroupDetailAll tags trigger a database query, which may affect 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 managementEnsure the uniqueness and stability of the ID or Level when managing user groups in the background, to avoid随意更改 and affect the frontend display.
- template designYou can flexibly design the display styles of different user groups based on the user group data you obtain, such as adding unique badges, colors, or feature descriptions for the VIP user group.
By using the above method, even without a direct "User Group List" tag, we can effectively loop and display user group information in the AnQiCMS template, meeting various operational needs.
Frequently Asked Questions (FAQ)
Q1: How can I retrieve a specific setting (Setting) item of a user group?A1: The user group'sSettingThe field is a key-value (map) structure that contains all the custom attributes configured by the backend administrator for the user group.You can directly access the specific items through dot syntax.For example, if there is a custom field named "SpecialFeature" in the background user group settings, you can access it in the template like this:{{ userGroup.Setting.SpecialFeature }}. It is recommended to use it before using it.{% if userGroup.Setting.SpecialFeature %}Make a judgment, ensure that this field exists to avoid template errors.
Q2:userGroupDetaillabel'sidandlevelWhat are the differences between the parameters, and when should which one be used?A2:idThe parameter is used to retrieve information through the unique identifier (ID) of the user group, this ID is automatically generated by the system.levelParameters are retrieved through the level of the user group, 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). In most cases, if you define a clear level concept for the user group in the background and this level is a key factor in your template logic (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 useidIt would be more suitable. In practice, you can choose to use one based on the user group data type you have.
Q3: Can I let users select a user group directly on the website frontend and register, and then display different user group information based on the selection?A3:userGroupDetailLabels are mainly used to display static information of user groups on the front end.If you want to implement the function of selecting a user group when registering a user, or upgrading a user 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 its provided user management API to achieve.The front-end template can be used to display the user group options available for selection, but to submit the user's choice and update their user group information, backend logic processing is required.