How to use the `yesno` filter in AnQiCMS templates to indicate whether content has been published or is in draft status?

Calendar 👁️ 71

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 istrue, then it will return"yes".
  • If the variable isfalse, then it will return"no".
  • If the variable isnilor 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

Related articles

How to use the AnQiCMS template `yesno` filter to elegantly display the user's membership status (such as: member, non-member, pending review)?

In website operations, clearly displaying a user's membership status is a key element 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 states 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, among which the `yesno` filter is a very practical tool that can help us handle conditional judgments in a concise and elegant way, making the template code cleaner and easier to read.### Get to know AnQiCMS's

2025-11-09

What are the main differences and application scenarios between the `yesno` filter of AnQiCMS and the `if` statement when handling boolean values?

In the practice of template development for Anqi CMS, flexible handling of Boolean data is a part of daily work.When facing a state value representing "true" or "false", we usually use two main template syntaxes to deal with it: one is the conditional judgment `if` statement, and the other is the `yesno` formatting filter.Although they are all related to boolean values, their original intention, mechanism of action, and applicable scenarios are obviously different.Understanding these differences can help us build website content more accurately and efficiently.### `if` statement

2025-11-09

How to customize the display text of the 'yes', 'no', and 'unknown' three states in the AnQiCMS template `yesno` filter?

When using AnQiCMS to build a website, templates often need to handle some status values representing "yes" or "no".For example, is an article published, a feature enabled, or a certain setting item is true.The system provides a very practical `yesno` filter that can help us elegantly convert these boolean values or three-valued logic (true, false, null) into friendly text display.This `yesno` filter was initially designed to display `true` values as 'yes' and `false` as 'no'

2025-11-09

How does the `yesno` filter in the AnQiCMS template determine if a variable exists or is empty (nil)?

In AnQiCMS template development, we often encounter situations where we need to determine whether a variable is true, false, or simply does not exist or is null.To handle these scenarios concisely and efficiently, AnQiCMS provides a very practical built-in filter——`yesno`.It can elegantly recognize the three core states of variables and output preset or custom text based on these states, greatly simplifying the conditional logic in the template.### `yesno` filter working principle: three-state judgment `yesno`

2025-11-09

What state does the `yesno` filter return by default when handling null values in AnQiCMS templates?

In Anqi CMS template development, flexibly handling various data states is the key to building dynamic pages.We often encounter situations where we need to display different content based on the true or false state of a variable.This is when the `yesno` filter becomes a very practical tool.It can help us elegantly convert boolean logic into more readable text output.### Deep understanding of `yesno` filter The `yesno` filter is a small but powerful template tool, its core function is to judge the status of a variable

2025-11-09

How to set the internationalized text for three states in the `yesno` filter of AnQiCMS template for a multilingual website?

How to ensure that the dynamic text in the template of a multilingual website can be displayed correctly according to the visitor's language settings is a challenge often faced by website operators.AnQiCMS (AnQiCMS) relies on its flexible template engine and strong multilingual support to provide an elegant solution.Today, let's delve into how the `yesno` filter of the AnQiCMS template can be combined with the internationalization mechanism in a multilingual website to accurately present three status texts.### Get to know `yesno`

2025-11-09

In AnQiCMS template, can the `yesno` filter determine the true or false state of a numeric or string variable?

In AnQiCMS template development, in order to better control the display logic of content, we often need to judge the 'true or false' status of a variable.This is a very practical tool, the `yesno` filter.It can help us output different texts in a concise way based on the status of variables, and it can even handle numeric and string type variables.The `yesno` filter is designed to provide a straightforward way to handle ternary states: yes, no, and uncertain (or no value).The basic working principle is to parse the input variable into a boolean value (true or false)

2025-11-09

How does the `yesno` filter combine with the data list in AnQiCMS templates to display the specific status of each piece of data?

During the process of building a website with AnQiCMS, we often need to display various data on the front-end page, which often has different states.For example, is an article published or a draft, a product listed or not, or is a user active or not.How to clearly and intuitively present these states to the user while keeping the template code concise and readable is a common concern for content operations and template developers.Today, let's delve into a very practical tool in AnQiCMS——the `yesno` filter

2025-11-09