In website operation, clearly displaying the user's membership status is a key factor in improving user experience and guiding user behavior.We often encounter situations where we need to display different texts or styles based on the different statuses of users (such as whether they are members, their membership level, whether their account is active, etc.)AnQiCMS provides rich and powerful template tags and filters, whereyesnoA filter is a very practical tool that can help us handle conditional judgments in a simple and elegant way, making the template code more tidy and readable.
Know AnQiCMS'syesnoFilter
AnQiCMS's template engine supports Django template syntax, which makes content operation very flexible.yesnoThe filter is one of the convenient tools for us to handle boolean logic. Its core idea is: to output the preset text based on the 'truth value' of a variable.
By default,yesnoThe filter will determine if a variable is true (true) or false (false) or empty (nilorunknown) and output 'yes', 'no', or 'maybe' accordingly.
Give an example, suppose we have a boolean variableisActive:
{# 假设 isActive 为 true #}
<p>状态:{{ isActive|yesno }}</p> {# 输出:状态:yes #}
{# 假设 isActive 为 false #}
<p>状态:{{ isActive|yesno }}</p> {# 输出:状态:no #}
{# 假设 isActive 为 nil 或未定义 #}
<p>状态:{{ nonExistentVar|yesno }}</p> {# 输出:状态:maybe #}
This filter is powerful because it allows us to customize the output text for these three states, not just the default 'yes', 'no', 'maybe'. We can customize these three values by adding comma-separated strings at the end of the filter:"真值对应的文本,假值对应的文本,空值对应的文本".
Smart useyesnoDisplay user membership status
Now, let's apply this filter to a real-world scenario: how to elegantly display a user's membership status, such as 'Member', 'Non-member', and 'Pending review'.
We can map the user status as follows:
- Member: Corresponds to logical
true. - Non-member: Corresponds to logical
false. - Pending revieworUnknown Status: Corresponds to logical
nilorunknown.
Assuming in our AnQiCMS backend, user data (such as throughuserDetailTags retrieved from comments.useran object) includes a field such asuser.IsMemberApprovedThis field may be a boolean to indicate whether the user has passed the membership review, or it may not exist at all (as `nil`) when the user has not submitted the review.