In website content management, clearly identifying the publishing status of content is a crucial link, which can help operators and visitors quickly understand the timeliness of information.AnQiCMS provides a flexible template system, allowing us to conveniently meet this requirement.Today, we will explore how to cleverly use the built-in templates of AnQiCMSyesnoFilter to elegantly present the publication or draft status of content.
Understand the content publication status in AnQiCMS.
In the AnQiCMS system, each piece of content you create (whether it's an article, product, or single page) usually associates with a status field.This field controls whether the content is visible on the front end in the background.Generally, when the content is set to 'Published', its status value will betrueWhen the content is in a 'draft' or 'pending review' state, the status value may befalseornil(null value). Understanding this is the basis foryesnousing the filter.)
yesnoThe principle of the filter.
yesnoThe filter is a practical tool provided by the AnQiCMS template system, which can return different strings based on the boolean value of variables (true/falseornil/unknown) and return different strings. Its basic syntax is{{ 变量 | yesno }}.
By default,yesnoThe filter will return the following three states:
- If the variable is
true, then it will return"yes". - If the variable is
false, then it will return"no". - If the variable is
nilor return an unrecognized boolean value."maybe".
However, merely displaying 'yes' or 'no' clearly does not meet our need to show the publishing status.yesnoThe real strength of the filter lies in its ability to allow you to customize the output strings corresponding to these three states. You can pass parameters like this:{{ 变量 | yesno:"真值,假值,未知值" }}.
Use cleverlyyesnoThe filter displays the content release status
Now, we willyesnoThe filter is applied to the publishing status of the content. Suppose we are iterating over a content list, or on the content detail page, we want to display the publishing status of the content.The status field of the content can usually be represented in the AnQiCMS templatearchive.Statusoritem.Status(depending on the variable name defined inarchiveListetc. tags).
Basic usage and default output:If we use it directly:{{ archive.Status|yesno }}You might get a "yes", "no", or "maybe" as such a result. Although this expresses the state, it is not intuitive.
Custom output, clearly present the status:To better user experience, we can customize the output text. For example, we can changetrueto “Published”,falseto “Draft”, andnilOr an unknown status corresponds to “Pending Review” or “Unknown Status”.
{# 假设 archive.Status 为 true 代表已发布,false 代表草稿 #}
{# 在内容详情页或循环中的每一项内容(item)都可以这样使用 #}
<p>内容标题:{{ archive.Title }}</p>
<p>发布状态:
<span class="status-indicator">
{{ archive.Status|yesno:"已发布,草稿,待审核" }}
</span>
</p>
{# 在一个内容列表的循环中 #}
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<div>
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>
当前状态:
<span class="content-status content-status-{{ item.Status|yesno:"published,draft,pending" }}">
{{ item.Status|yesno:"已发布,草稿,待审核" }}
</span>
</p>
<p>{{ item.Description }}</p>
</div>
{% empty %}
<p>暂无内容。</p>
{% endfor %}
{% endarchiveList %}
In the above code example, we not only utilizeyesnoThe filter directly outputs a friendly Chinese status description, and cleverly uses its return values ("published", "draft", "pending") as part of the CSS class name.This allows you to add different colors or visual effects to content in different states through CSS styles, further enhancing the intuitiveness of the interface.
yesnothe filter meetsifLabel selection
You might ask, since{% if archive.Status %}已发布{% else %}草稿{% endif %}Why choose if it can achieve a similar effect?yesnoWhat about the filter?
Both have their own focus:
ifTags:Mainly used to control the display of different HTML structures in templates or to execute complex conditional logic.For example, if it is published, show a button, and if it is a draft, show a different button.yesnoFilter:It is more suitable to convert a boolean value (or a value that can be converted to a boolean) into text output. When you only need to display different text based on the state, without changing the HTML structure or performing complex logic,yesnoThe filter makes the template code more concise and readable, especially in scenarios where you need to quickly display the state in a single row, such as table columns, card views, etc.
Summary
yesnoThe filter is a small but powerful tool in the AnQiCMS template system.By flexibly customizing its output value, you can present the publishing status of the background content intuitively and friendlily on the website front end. Whether it is used for list overview or content detail page, it can effectively improve the usability and user experience of the website.Master this filter, it will help you operate content and template development more efficiently.
Frequently Asked Questions (FAQ)
Q1:yesnoCan the filter handle non-boolean values? For example, what if my status field is a number (0/1) or a string ("true"/"false")?A1: Yes,yesnoThe filter has certain intelligent conversion capabilities. It will try to convert non-boolean values (such as numbers 0/1, strings "true"/"false") to boolean values for processing.Generally, non-zero numbers or the string "true" are consideredtrueWhereas the number 0 or the string "false" will be consideredfalseOr other non-empty values that cannot be recognized.nilThe value will trigger a return of 'unknown status'. So, even if your status field stores numbers or specific strings,yesnoit usually works fine.
Q2: If I only need 'yes' or 'no' two states, and don't want to handle the 'unknown' state,yesnoCan the filter still be used?A2: Of course. If you only care abouttrueandfalseTwo cases, and you do not want to process or displaynilOr unknown status, you can only provide two custom values. For example:{{ archive.Status|yesno:"上线中,下线" }}In this case, ifarchive.StatusWithnilOr cannot be converted to a boolean value,yesnoIt will return the second value, namely