How to retrieve and display a user's account balance and cumulative income in the Anqi CMS template?
For operators, flexibly displaying user-specific information on the website, such as their account balance or cumulative earnings, is a key element in improving user experience and managing the membership system.AnQi CMS as a powerful and practical content management system, provides a very convenient way to obtain these user data.Today, let's talk about how to use your template touserDetailTags can easily retrieve and display a user'sBalance(account balance) andTotalReward(cumulative income).
Get to know in-depthuserDetailTag
userDetailIt is a very practical tag in the Anqi CMS template engine, specifically used to obtain detailed information about a specified user.It's like a data interface that allows you to directly retrieve various properties of the user in the front-end template based on the user ID.Whether it is the basic information of the user, or the financial data we are concerned about today,userDetailCan easily help you achieve that.
While usinguserDetailThe most core parameter when labeling isid. ThisidRepresents the user identity you want to retrieve. Typically, this will be the ID of the currently logged-in user, or the ID of a specific user you want to display in a particular scenario.
Its basic usage form is as follows:
{% userDetail 变量名称 with name="字段名称" id="用户ID" %}
Here, "variable name" is the temporary name you specify for the data you obtain, for easy reference in templates; "field name" is the specific user attribute you want to obtain, such asBalanceorTotalReward;“User ID” literally means the unique identifier of the user to be queried.
Get the user's account balance (Balance)
The account balance usually refers to the available funds of the user on the platform, which can be used for consumption, purchasing services, or cash withdrawal, etc.It is an essential function to display the user's account balance on membership, points system, or e-commerce websites.
ByuserDetailLabel, we can get the user's account balance like this:
{# 假设我们已经通过某种方式获取到了当前登录用户的ID,并存储在名为 `currentUser.Id` 的变量中 #}
<p>
<strong>当前账户余额:</strong>
{% userDetail userBalance with name="Balance" id=currentUser.Id %}
{{ userBalance }} 元
</p>
In this code, we useuserDetailLabel, specifyname="Balance"to retrieve the user's balance and assign the result touserBalancethis temporary variable. Subsequently, through{{ userBalance }}The balance amount can be displayed on the page. If the user has no balance or the amount is zero, it usually displays '0' or '0.00'.
Get the cumulative income of the user (TotalReward)
The cumulative income is more focused on the total income accumulated by users on the platform through certain behaviors (such as content creation rewards, promotion commissions, invitation rebates, etc.).This is very helpful in motivating users and showcasing the value of the platform.
The method to get the cumulative income of the user is similar to getting the balance:
{# 假设我们已经通过某种方式获取到了当前登录用户的ID,并存储在名为 `currentUser.Id` 的变量中 #}
<p>
<strong>累计收益:</strong>
{% userDetail totalReward with name="TotalReward" id=currentUser.Id %}
{{ totalReward }} 元
</p>
Similarly, we specifyname="TotalReward"to get the cumulative income and store it intotalRewardThe variable is then displayed. This value is usually a continuously increasing cumulative value that can bring users a sense of achievement.
Combine with the actual application scenario
In practice, you may wish to display these financial information simultaneously on a user center page or personal dashboard.Combine them, and users can easily understand their financial situation.
For example, you might write template code like this in a user's personal center page overview module:
<div class="user-financial-overview">
<h2>我的财务概览</h2>
{# 假设 currentUser.Id 已经由后端传递到模板上下文中,代表当前登录用户的ID #}
{% userDetail userBalance with name="Balance" id=currentUser.Id %}
{% userDetail totalReward with name="TotalReward" id=currentUser.Id %}
<p>
<strong>当前可用余额:</strong>
{# 如果希望余额为0时显示特定文本,可以结合if标签判断 #}
{% if userBalance > 0 %}
<span class="highlight-balance">{{ userBalance }}</span> 元
{% else %}
<span>0.00</span> 元
{% endif %}
</p>
<p>
<strong>历史累计收益:</strong>
{# 同样,如果累计收益为0时显示特定文本 #}
{% if totalReward > 0 %}
<span class="highlight-reward">{{ totalReward }}</span> 元
{% else %}
<span>0.00</span> 元
{% endif %}
</p>
<div class="actions">
<a href="/user/recharge">充值</a>
<a href="/user/withdraw">提现</a>
<a href="/user/rewards">收益明细</a>
</div>
</div>
In this way, not only do you display the data, but you can also present it in different styles based on specific data values (such as whether it is greater than 0), even guide the user to the next step, making the entire financial module more vivid and practical.
Summary
userDetailThe label provides great flexibility for Anqi CMS users, allowing them to easily display dynamic user-related data on the front-end template. By proficiently usingBalanceandTotalRewardThese fields will enable your website to better serve various business scenarios such as member management, content monetization, e-commerce operations, and provide users with more personalized and transparent experiences.
Frequently Asked Questions (FAQ)
1.userDetailin the labelidHow should the parameter be obtained?
idThe parameter usually represents the ID of the currently logged-in user. In the regular development process of Anqi CMS, the backend controller will pass the current logged-in user's ID through the template context (Context) to the front-end template, and you can directly refer to this variable in the template (as shown in the above example)currentUser.Id). If in a specific scenario it is necessary to display other users' IDs, then the user's ID should be obtained through URL parameters, database queries, and passed to the template.
2. If the user does not haveBalanceorTotalRewarddata, what will the template display?ForBalanceandTotalRewardfor this type of numeric field, if the user does not have relevant records or the data is zero,userDetailthe label will usually return directly0In the template, you can combine as shown in the sample code.{% if ... %}The tag is used for judgment. When the value is 0, it displays "0.00" or other more friendly prompts, such as "No balance" or "No earnings yet."}
3. In addition to account balance and cumulative earnings,userDetailWhat information can tags obtain about users?
userDetailTags have very rich functions, in addition toBalanceandTotalRewardIt can also obtain many other important information about the user, such as:
UserNameUsernameRealNameReal NameEmailEmailPhonePhone NumberAvatarURL: User avatar linkGroupId: User group IDIsRetailer: Is a distributorInviteCode: Invitation codeLastLogin: Last login time (timestamp, needs to be paired with...stampToDateformatted)ExpireTime: VIP expiration time (timestamp, needs to be coordinated with)stampToDateformatting) You can flexibly call these fields according to your actual needs to build a more perfect user page.