In AnQiCMS, if you want to display users' exclusive invitation codes on the website page, this usually involves the application of membership systems or referral mechanisms. AnQiCMS provides flexible template tags that can help us easily obtain and display various user information, including invitation codes (InviteCode)

To implement this feature, we mainly use AnQiCMS'suserDetailThe label is specifically designed to retrieve detailed data of a specific user from the background database and display it in the front-end template.

UnderstandinguserDetailTag

userDetailThe core function of the tag is to retrieve all available details based on the user's ID. Its basic structure is as follows:

{% userDetail 变量名称 with name="字段名称" id="用户ID" %}

There are several key points to understand:

  • 变量名称This is an optional parameter. If you need to store the data obtained in a temporary variable for subsequent complex processing, you can specify a variable name (for example,currentUser/inviteCodeDataIf not needed, you can simply omit it.
  • name="字段名称"This is used to specify which user information you want to retrieve. For invitation codes, you need to字段名称is set toInviteCode.
  • id="用户ID"This parameter isrequired.It tells AnQiCMS which user's details you want to query. This用户IDcan be a specific number (such asid="123"), can also be a dynamic variable, such as the current logged-in user's ID.

userDetailThe fields that can be obtained by the label are very rich, including not only invitation codes, but also usernames (UserName), real name (RealName)、user avatar(AvatarURL)et al.,InviteCodeThat is the target field we need to obtain this time.

Actual operation: Obtain and display the invitation code

Suppose we want to display a user's own invitation code on a user's personal center page, or on an administrator page to display a specific user's invitation code.

Scenario one: Display the invitation code for the currently logged-in user

In AnQiCMS, when you design pages such as user personal centers, the system usually provides some contextual information for the currently logged-in user. AlthoughuserDetailthe tag documentation explicitly statesidis required but can be omitted on certain default user-related pages (such as the user profile page)idPass the current user ID through parameters or in other ways, it may automatically obtain the information of the currently logged-in user.

The most reliable approach is to ensure that you can obtain the current logged-in user'sID. If your template context already includes the current user's ID (such as incurrent_user.Idsuch a variable in)

{% set userId = 123 %} {# 假设这是当前登录用户的ID,实际应用中会从会话或上下文中获取 #}
<div>
    您的专属邀请码是:
    <strong>{% userDetail with name="InviteCode" id=userId %}</strong>
</div>

if the system supports skipping directly in the user context pageidand defaults to the current user, it can be written more concisely as:

<div>
    您的专属邀请码是:
    <strong>{% userDetail with name="InviteCode" %}</strong>
</div>

Please test which method is more suitable for obtaining the current logged-in user ID based on your actual AnQiCMS version and template context.

Scenario two: Display the invitation code for the specified user (via user ID)

If you know a specific user's ID (for example, when displaying a user list, you need to display each user's invitation code), then you can directly pass in the ID in theidparameters.

For example, to get the user ID of1the invitation code:

<div>
    用户ID为 1 的邀请码是:
    <strong>{% userDetail with name="InviteCode" id="1" %}</strong>
</div>

If you are iterating over a user list and each user object has aIdattribute, you can do this:

{% for user in userList %} {# 假设这里有一个包含用户信息的 userList 循环 #}
    <div class="user-card">
        <h3>{{ user.UserName }} 的邀请信息</h3>
        <p>用户ID:{{ user.Id }}</p>
        <p>专属邀请码:
            <strong>{% userDetail userInviteCode with name="InviteCode" id=user.Id %}</strong>
        </p>
    </div>
{% endfor %}

In this example, we first go throughid=user.IdSpecified the user ID to be queried, then the invitation code obtained is assigned touserInviteCodea variable, and finally displayed on the page.

Advanced usage and注意事项

  1. Assigned to a variable and then processed: In order to maintain the neatness of the template and facilitate subsequent processing, we usually willuserDetailAssign the obtained value to a variable. This way, you can add additional HTML wrapping, styling, or even simple logical judgments to the invitation code.

    {% userDetail userInviteCode with name="InviteCode" id="当前用户ID或指定用户ID" %}
    {% if userInviteCode %} {# 判断邀请码是否存在或不为空 #}
        <p class="invite-code-display">您的邀请码是:<code>{{ userInviteCode }}</code></p>
    {% else %}
        <p class="no-invite-code">暂无专属邀请码。</p>
    {% endif %}
    
  2. Dynamically retrieve the user IDIn practical applications,用户IDIt is usually dynamic. For example, it can be obtained from URL parameters or extracted from the session information of the logged-in user.This logic is usually handled at the controller or routing level, and then the user ID is passed to the template.

  3. Data security and visibility: Invitation codes belong to users' personal information or business sensitive information, and must pay attention to permission control when displayed.Ensure that only the user themselves or administrators with the corresponding permissions can view the invitation code to avoid unnecessary privacy leaks.

By using the above method, you can flexibly obtain and display the exclusive invitation code for users in the AnQiCMS template, providing necessary information display for your website membership system or referral feature.


Frequently Asked Questions (FAQ)

  1. What will the page display if the user does not have an invitation code? Answer:If it passesuserDetailTag to get the `Invite