In AnQi CMS, implementing the acquisition and display of detailed information for website users or user groups is a key step in building a personalized member system and enriching the user experience.AnQiCMS provides intuitive and powerful template tags to help us easily implement these functions, whether it is displaying the user's VIP level, account balance, or the name of the user group they belong to.

Deeply Understand the User and User Group System of AnQiCMS

AnQiCMS comes with comprehensive user management and user group (VIP system) features.It allows website administrators to create different user groups and set different permission levels for these groups, even supporting VIP paid content.This means that each user not only has their own basic information but also belongs to a specific user group, which also has its own detailed attributes.

To display this information on the website frontend, AnQiCMS does not mix user or user group data directly into the regular article or product models, but calls them independently through specially designed template tags.This separation design concept 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 user data, such as their account ID, username, balance, VIP expiration time, and so on,userDetailTags are our reliable assistants. This tag can retrieve and present a user's personal profile based on their 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 obtained, 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 be queried.In practical applications, this ID is typically dynamically retrieved, such as from a user login session.idThe parameter can sometimes be omitted, and the system will automatically recognize it.

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 usually need to be usedfloatformat:2Such a filter to format, ensure that the balance is displayed with two decimal places.ExpireTimefield is a timestamp, which needs to be helped withstampToDateLabel and format it into a human-readable date and time format. Here2006年01月02日 15:04Is a special magic string defined in Go language for date and time format, representing year, month, day, hour, and minute.

Reveal user group information:userGroupDetailThe magic 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 useuserGroupDetailThis tag is specifically 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:

  • groupInfois the temporary variable name specified for user group information.
  • id="用户组ID"orlevel="用户组等级"specifies the user group to be queried, either one of the two.

ConnectuserDetailanduserGroupDetailThe essence lies in:)userDetailThe tag can return the user group ID of the user (GroupId) and we can first get thisGroupId, then pass it as a parameter touserGroupDetailLabels, thus querying 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 the user with ID 123.If a user is found, it will display the username and further query the details of the user group to which the user belongs, thereby displaying the name of the VIP level.Then, it will display the user's balance and check the VIP validity period, if it exists, it will be displayed in a formatted manner.Thus, a user can clearly see their membership status and financial overview at a glance.

Practical tips and注意事项

  1. Data source and dynamic IDIn a real website, the user's ID usually would not be hardcoded as '123', like in the example.It dynamically retrieves from the user login session, URL parameters, or other page contexts.The AnQiCMS template engine typically automatically injects the user object or its ID into the template context when handling the currently logged-in user. You can check the specific development documentation or backend code to understand how to retrieve it.
  2. Error Handling and Conditional Judgment: When usinguserDetailoruserGroupDetailWhen using tags, be sure to use{% if 变量名 %}such conditional judgments. 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.
  3. Time FormattingAlways keep in mind Go languagestampToDateThe time formatting rules of tags (e.g.2006-01-02 15:04:05It is the base time), adjust the display format flexibly according to your needs.
  4. Multi-site environmentIf you are running the multi-site version of AnQiCMS and need to call user or user group information across sites, these tags also supportsiteIdparameters to specify which