In AnQiCMS, if you want to display users' exclusive invitation codes on website pages, 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 will mainly use AnQiCMS.userDetailThis tag is specifically designed to retrieve detailed data of a specific user from the background database and display it in the frontend template.
UnderstanduserDetailTag
userDetailThe core function of the label is to retrieve all available details of the user based on their 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/inviteCodeData)。If not needed, it can be omitted.name="字段名称":This is used to specify which user information you want to retrieve. For the invitation code, you need to set.字段名称toInviteCode.id="用户ID":This parameter isrequiredIt tells AnQiCMS which user's details you want to query.用户IDIt can be a specific number (for example,id="123"),can also be a dynamic variable, such as the current logged-in user's ID.
userDetailThe fields that tags can get are very rich, including not only invitation codes, but also usernames (UserName), real name (RealName)、user avatar(AvatarURL)etc.,“InviteCodewhich is the target field we need to retrieve this time.
Actual operation: obtain and display the invitation code
Suppose we want to display a user's invitation code on their personal center page or on an admin page for a specific user.
Scene one: Display the invitation code of the currently logged-in user
In AnQiCMS, when you design pages such as the user personal center, the system usually provides some context information for the currently logged-in user. AlthoughuserDetailit is explicitly stated in the tag document.idis required, but can be omitted on certain default user-related pages (such as the user profile page)idThe parameter or obtaining the current user ID in other ways and passing it in may automatically retrieve the information of the currently logged-in user.
The safest 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 (for example,current_user.IdSuch a variable (in parentheses), then you can use it like this:
{% set userId = 123 %} {# 假设这是当前登录用户的ID,实际应用中会从会话或上下文中获取 #}
<div>
您的专属邀请码是:
<strong>{% userDetail with name="InviteCode" id=userId %}</strong>
</div>
If the system supports omitting in the user context page directlyidAnd default to the current user, then 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.
Scene two: Display the invitation code of a specified user (via user ID)
If you know the specific user ID (for example, when displaying a user list, you need to show each user's invitation code), then you can pass the ID directly 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 it like 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 accessid=user.Idspecified the user ID to be queried, then assign the obtained invitation code touserInviteCodethe variable, and finally display it on the page.
Advanced usage and注意事项
Assign to a variable and process: For the cleanliness of the template and the convenience of subsequent processing, we usually will
userDetailAssign the obtained value to a variable. This way, you can perform additional HTML wrapping, styling, and even simple logical judgments on 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 %}Dynamically retrieve the user IDIn actual 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.Data security and visibilityThe invitation code is personal or business-sensitive information of the user. It must be displayed with proper permission control.Ensure that only the user themselves or administrators with the corresponding permissions can view the invitation code, to avoid unnecessary privacy leaks.
You can flexibly obtain and display the exclusive invitation code of users in AnQiCMS templates using the above method, providing necessary information display for your website's membership system or referral function.
Common Questions (FAQ)
- What will be displayed on the page if the user does not have an invitation code?
Answer:If through
userDetailLabel to get the `Invite