In the template development practice of AnQi CMS, flexibly handling Boolean (Boolean) type data is part of daily work. When faced with a status value representing "true" or "false", we usually use two main template syntaxes to deal with it: one is conditional judgment.ifThe statement, second is the formatter of outputyesnoFilter.Although they are all related to boolean values, their original design intention, mechanism of action, and applicable scenarios have obvious differences.Understanding these differences can help us build website content more accurately and efficiently.

ifThe statement: the controller of content flow

ifThe role of the statement in AnQiCMS template is similar to the conditional branch in our daily programming. It decides whether to render a specific content block based on the truth or falsity of a certain conditionWhether to render a specific content blockor rendered under different conditionscompletely different content structure. Its core is 'control flow', that is, guiding the rendering path of the template according to the state of boolean values.

For example, when displaying a product list, we may need to display different operation buttons based on the stock status of the product: if the product is in stock, display 'Buy Now'; if the stock is zero, display 'Sold Out'. At this time,ifThe statement is the ideal choice:

{% if product.is_in_stock %}
    <button class="buy-button">立即购买</button>
{% else %}
    <span class="sold-out-tag">已售罄</span>
{% endif %}

Here,product.is_in_stockThis boolean value directly determines which of two completely different HTML elements (a button or a text label) will be displayed on the page.ifThe sentence can be combinedelif(else if) andelseTo handle more complex multiple conditional branches, its strength lies in the ability to build highly dynamic page structures around boolean values.

yesnoFilter: An Elegant Translator of Boolean Values

Compared toifmacro-control of content structure,yesnoFilter is a more refined tool, it focuses onTranslate the boolean state directly into readable text output。Its function is not to determine what content block is displayed, but to present the boolean status in a concise textual form within the given content.

yesnoFilter applied after the variable, it will check the boolean state of the variable:

  • If the variable is 'true', the default output is 'yes'.
  • If the variable is "false", the default output is "no".
  • If the variable is “empty” (nil/undefined), then the default output is “maybe”.

It's even better that this output is fully customizable. We can specify any text for these three states:

{{ user.is_active|yesno:"启用,禁用,未知" }}

Suppose we are displaying a user profile and need to show the user's active status. Ifuser.is_activeresponse fortrue, the above code will output “Enabled”; if it isfalse, then it will output “Disabled”; ifuser.is_activeThis field does not exist or is empty, and "unknown" will be output.

This way makes the text output of boolean values very concise, avoiding writing a long one.if-elseStructure is just to display a few words.

Core difference and selection strategy

Now, let's sort out the main differences and application scenarios of the two:

  1. Different purposes:

    • ifSentences are meant toControl the rendering process and content structure of the templateIt determinesWhat to display.
    • yesnoFilters are forConverting boolean states to specific textIt determinesHow to express in text formBoolean state.
  2. Different granularity of action:

    • ifSentences usually act on:Larger content blocks or multiple independent elements.
    • yesnoFilterer acts onoutput of a single variable. It is usually embedded as part of the text in the page.
  3. When to chooseifStatement:

    • when you need to base something on a boolean valueShow or hide the entire code blockFor example, a form, a warning message, a functional module.
    • when you need to base something on a boolean valueRender two or more completely different page layouts or components.
    • When a boolean value determines whether the interactive elements (such as buttons, links) appear on the pageand their properties..
  4. When to chooseyesnoFilter:

    • when you just want to place a sentence or labelSimply display a text description of a boolean state.For example: “Is public: Yes”, “Approval status: Passed”, “Available: No”.
    • When you want to useA unified and concise wayConvert multiple boolean fields to readable text without having to write repeated for each field.if-elseLogical time.
    • When you need to handleBoolean values that may be empty.,and wish to provide a “unknown” or “N/A” default text representation for it.

In short, when a boolean value needs to “command” the page,structure or functionuseifStatement; when a boolean value only needs to “report” itsstatus textwhenyesnoThe filter would be a more elegant, more efficient choice. These two tools serve different roles in the AnQiCMS template system, together forming a flexible strategy for Boolean value processing.

Common Questions (FAQ)

  1. Q:yesnoFilter can act likeifDoes the statement execute complex conditional judgments?A: No.yesnoThe filter is intended to simply map boolean values to three text states. It does notifsuch conditional combinations as statementsand/or)or the ability to judge multiple different variables. If complex logical judgments are needed to determine what content block to display, still need to useifstatement.

  2. Q: If I only want to display “Yes” or “No”, not caring about the “Unknown” status,yesnoFilter byifwhat are the advantages of the statement?A: Even if we only deal with the two cases of “Yes” or “No”,yesnoThe filter is usually also more concise. For example,{{ item.status|yesno:"是,否" }}vs.{% if item.status %}是{% else %}否{% endif %}It has fewer code lines and is more readable, especially in scenarios where similar boolean status text needs to be output multiple times.

  3. Q:yesnoFilter “unknown” status (maybe) specifically refers to what?A: In the AnQiCMS template context, the “unknown” status usually refers to the value of the variable beingnil(Empty value), undefined, or certain ones deemed as 'empty' by the template engine rather than explicittrueorfalseThe value. For example, if a boolean field is allowed to be null in the database or missing during data transmission, it may be represented as an "unknown" state when passed to the template.yesnoThe filter will output the third predefined text.