In website content management, clearly identifying the publication status of content is a crucial link, as it helps operators and visitors quickly understand the timeliness of the information.AnQiCMS provides a flexible template system, allowing us to conveniently meet this requirement.yesnoFilter, to elegantly present the published or draft status of content.

Understand the content publishing status in AnQiCMS

In the AnQiCMS system, each piece of content you create (whether it's an article, product, or a single page) is usually associated with a status field.This field controls whether the content is visible on the front end in the background.true,when the content is in the "draft" or "pending review" status, its status value may befalseornilUnderstanding this is essential for utilizingyesnothe basis for the filter.

yesnoThe working principle of the filter

yesnoFilter is a practical tool provided by AnQiCMS template system, it can return different strings according to the boolean value of the variables.true/falseornil/unknownIts basic syntax is{{ 变量 | yesno }}.

By default,yesnoThe filter will return the following three states:

  • If the variable istruethen return"yes".
  • If the variable isfalsethen return"no".
  • If the variable isnilor a boolean value that cannot be recognized, then return"maybe".

However, simply displaying “yes” or “no” clearly does not meet our needs to show the publication status.yesnoThe true power 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 publication status of the content.Assuming we are iterating over a content list, or on the content detail page, we want to display the publishing status of the content.archive.Statusoritem.Status(Depends on the variable name defined in)archiveListetc. tag).

Basic usage and default output:If we use directly{{ archive.Status|yesno }}This may result in "yes

Custom output, clearly display status:To provide a better user experience, we can customize the output text. For example, we can settrueto 'Published',falseto 'Draft',nil或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.Here, you can add different colors or visual effects to content in different states through CSS styles, further enhancing the intuitiveness of the interface.

yesnoFilter is related toifthe selection of tags

You might ask, since{% if archive.Status %}已发布{% else %}草稿{% endif %}Why choose when you can also achieve a similar effect?yesnoWhat about filters?

Both of these have their own focus:

  • ifTags:主要用于控制模板中不同 HTML 结构的显示或执行复杂的条件逻辑。For example, if it has been published, show a button; if it is a draft, show another different button.
  • yesnoFilter:More suitable for converting 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 status without changing the HTML structure or executing complex logic,yesnoThe filter makes template code more concise and readable, especially in scenarios where it is necessary to quickly display the status 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.Through flexible customization of its output value, you can present the publishing status of background content intuitively and friendliness on the website front end. Whether it is used for list overview or content detail page, it can effectively enhance the usability and user experience of the website.Mastering this filter will help you operate content and templates more efficiently.


Common 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,yesnoFilter 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.true,and the number 0 or the string 'false' will be consideredfalse. Other unrecognized non-empty values ornilThe value will trigger a return of 'unknown status'. So, even if your status field stores numbers or specific strings,yesnoit usually works normally.

Q2: If I only need 'yes' or 'no' states, and do not want to handle the 'unknown' state,yesnoCan the filter still be used?A2: Of course. If you only care abouttrueandfalseTwo cases, and do not want to handle or display.nilOr unknown status, you can only provide two custom values. For example:{{ archive.Status|yesno:"上线中,下线" }}In this case, ifarchive.Statusresponse fornilor cannot be converted to a boolean value,yesnoThe return will be the second value, that is,