In AnQi CMS, realizing the acquisition and display of detailed information of website users or user groups is a key link in building personalized member systems and enriching user experience.Whether it is to display the user's VIP level, account balance, or the name of the user group they belong to, AnQiCMS provides intuitive and powerful template tags to help us easily achieve these functions.
Deep understanding of AnQiCMS user and user group system
AnQiCMS is built-in with a complete user management and user group (VIP system) function.It allows website administrators to create different user groups, set different permission levels for these groups, and even support VIP paid content.This means that each user not only has their own basic information but will also be assigned to a specific user group, which also has its own detailed attributes.
To display this information on the website front end, AnQiCMS does not mix user or user group data directly into the conventional article or product models, but calls them independently through specially designed template tags.This separation of design philosophy ensures the clarity of content management and user management, and also provides great flexibility for front-end developers.
Get user details:userDetailThe clever use of tags
When we need to display specific data for a user, such as their account ID, username, balance, VIP expiration time, and so on, userDetailThe tag is our helpful assistant. This tag can retrieve and present the user's personal profile based on the unique identifier (usually the user ID).
Its basic usage is very concise:
{% userDetail 变量名称 with name="字段名称" id="用户ID" %}
Among them:
变量名称: You can specify a temporary variable name for the user information you obtain, which is convenient for referencing in subsequent code.name="字段名称": Specify the type of user information you want to retrieve, for exampleId/UserName/Balance/ExpireTimeetc.id="用户ID": Specify the ID of the user to query. In practice, this ID is usually dynamically obtained, such as from a user login session.In the absence of a specific user ID, or on the user center page, if the context provides information about the currently logged-in user,idSometimes parameters can be omitted, and the system will automatically recognize them.
For example, to display the current user's balance and VIP expiration time, we can operate as follows:
{% userDetail currentUser with id="123" %} {# 假设用户ID为123,实际应用中会动态获取 #}
{% if currentUser %}
<p>您的账户余额:¥{{ currentUser.Balance|floatformat:2 }}</p>
{% if currentUser.ExpireTime %}
{% set vipExpiryDate = stampToDate(currentUser.ExpireTime, "2006年01月02日 15:04") %}
<p>您的VIP服务将于:{{ vipExpiryDate }} 到期。</p>
{% else %}
<p>您是普通用户,暂无VIP有效期。</p>
{% endif %}
{% else %}
<p>请登录以查看您的账户信息。</p>
{% endif %}
{% enduserDetail %}
Please note,BalanceFields are usually usedfloatformat:2This filter is used to format, ensuring that the balance is displayed with two decimal places.ExpireTimefield is a timestamp and needs to be handled withstampToDateLabel it to format it into a human-readable date and time format. Here2006年01月02日 15:04It is a special magic string defined in the Go language for date and time formatting, representing year, month, day, hour, and minute.
Reveal user group information:userGroupDetailThe clever use of tags
The user's VIP level, which is usually not stored directly in the user's personal information, but defined by the user group they belong to. To display specific user group names such as 'VIP Gold Member' or 'Silver User', we need to useuserGroupDetailThe tag is used to retrieve detailed attributes of a user group, such as its name, description, and level.
Its basic usage is also very direct:
{% userGroupDetail groupInfo with id="用户组ID" %}
Or, if you know the user group level (level), you can also use it like this:
{% userGroupDetail groupInfo with level="用户组等级" %}
Among them:
groupInfo: It is the temporary variable name specified for user group information.id="用户组ID"orlevel="用户组等级": Specifies the user group to be queried, either one of the two.
连接userDetailanduserGroupDetailThe key lies in:userDetailThe tag can return the user group ID of the user (GroupId) We can first get thisGroupId Then pass it as a parameter touserGroupDetailLabels, thus you can query the detailed information of the user group, including its name.
Let's look at a complete integration example:
{% userDetail currentUser with id="123" %} {# 假设用户ID为123 #}
{% if currentUser %}
<h2>欢迎回来,{{ currentUser.UserName }}!</h2>
{# 获取用户组信息 #}
{% userGroupDetail currentGroup with id=currentUser.GroupId %}
{% if currentGroup %}
<p>您当前的会员等级是:<strong style="color: gold;">{{ currentGroup.Title }}</strong></p>
{% else %}
<p>您当前是普通用户。</p>
{% endif %}
{% enduserGroupDetail %}
<p>您的账户余额:¥{{ currentUser.Balance|floatformat:2 }}</p>
{# 再次检查VIP有效期,通常VIP等级与有效期密切相关 #}
{% if currentUser.ExpireTime %}
{% set vipExpiryDate = stampToDate(currentUser.ExpireTime, "2006年01月02日 15:04") %}
<p>VIP有效期至:{{ vipExpiryDate }}</p>
{% endif %}
<p><a href="/user/profile">管理我的账户</a></p>
{% else %}
<p>您尚未登录。请<a href="/login">登录</a>以查看您的专属信息。</p>
{% endif %}
{% enduserDetail %}
This code first tries to retrieve the user information for user ID 123.If a user is found, it will display the username and further query the details of the user group the user belongs to, thereby displaying the name of the VIP level.It will then display the user's balance and check the VIP validity period, formatting the display if it exists.This way, a user can clearly see their membership status and financial overview.
Useful tips and precautions
- Data source and dynamic IDIn a real website, the user's ID is usually not hardcoded as '123'.It will dynamically obtain from user login sessions, URL parameters, or other page contexts.The AnQiCMS template engine will usually automatically inject the user object or its ID into the template context when handling the current logged-in user, you can find out how to get it by referring to the specific development document or backend code.
- Error handling and conditional judgment: In use
userDetailoruserGroupDetailBe sure to use tags{% if 变量名 %}Such conditional judgment. This can effectively avoid errors or blank pages on the page when the user is not logged in or the relevant information does not exist. - Time formatting: Remember Go language
stampToDateThe time formatting rules of the label (for example2006-01-02 15:04:05It is the base time), adjust the display format flexibly according to your needs. - Multi-site environmentIf you are running the AnQiCMS multi-site version and need to call user or user group information across sites, these tags also support
siteIdparameters to specify from which