In AnQiCMS template development, efficiently and robustly handling data is the key to building dynamic websites. Especially when the data obtained from the backend may existnilWhen the value is empty, how to elegantly display content, provide default values, or execute specific logic becomes particularly important. AnQiCMS provides a variety of filters and tags that can help us targetnilValue shows three logical states of “exists”, “does not exist”, or “undefined”, thus enhancing the flexibility of the template and the user experience.

I. Directly determine whether the data exists:ifLogical judgment label andfor...emptyLoop structure

The most basic and most commonly used method is to use conditional judgment labelsifto check if data or variables exist. AnQiCMS's template engine willnilEnglish, empty string, zero or empty collections (such as empty arrays, empty slices) are considered in boolean contextsfalse. This means that when a variable or object isnilwhen{% if 变量名 %}it will executeelseBranch, indicating a state of "non-existent" or "no content."

For example, when trying to display the details of a document, but the document may not exist:

{% archiveDetail myArchive %} {# 尝试获取当前文档详情,如果不存在,myArchive将为nil #}
{% if myArchive %}
    <h1>{{ myArchive.Title }}</h1>
    <p>{{ myArchive.Description }}</p>
{% else %}
    <p>抱歉,您查找的文档不存在。</p>
{% endif %}

This pattern clearly demonstrates the binary logic of 'existence' and 'non-existence'.

Similarly, for tags that may return an empty set (such as document lists, friend link lists), AnQiCMS providesfor...emptyLoop structure, it is used specifically to handle the third state of 'list is empty':

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <li><a href="{{item.Link}}">{{item.Title}}</a></li>
    {% empty %}
        <li>当前没有任何内容可以显示。</li>
    {% endfor %}
{% endarchiveList %}

Here, ifarchivesExecute if the list contains data,forLoop body; ifarchivesIt is empty, then executeemptyThe block directly provides "with content" and "without content" two clear visual feedbacks, which is very suitable for handling list data that may be empty.{% linkList %}/{% tagList %}labels in conjunction withiforfor...emptyis also often used in such scenes.

二、Precise Handling of Null Values Filter:defaultWithdefault_if_none

In addition to condition judgment, AnQiCMS also provides filters tonilor set default alternative content for null values.

defaultFilters can be used for variables (including)nilAn alternative value is provided when the value is empty, null, or zero. It offers an 'or' alternative for any 'empty' state:

{{ document.Title|default:"无标题" }}

Ifdocument.Titleis an empty string ornilIt will display "untitled".

whiledefault_if_noneThe filter is more precise, it is specifically designed fornil(Typically refers to a pointer type or an uninitialized variable) provides a default value. If the variable value is an empty string or zero, but notnilIt will not apply the default value. This helps distinguish between the two cases of 'having a value but it is empty' and 'no value at all', reflecting a more refined 'or' state handling:

{# 假设后端user对象可能为nil,或者其UserName字段可能为空字符串 #}
{{ user.UserName|default_if_none:"访客" }} {# 如果user或UserName为nil,显示“访客” #}
{{ user.UserName|default:"匿名用户" }}   {# 如果user或UserName为nil或空字符串,显示“匿名用户” #}

Passdefault_if_noneYou can provide explicit default representation for those truly 'not set' data, distinguishing them from data that 'are set but empty.'

Three, clear three-state logic:yesnoFilter

yesnoThe filter is a powerful tool that directly implements the "OR and NOT" three-state logic in AnQiCMS templates, especially for handling boolean values or nullable data. Its default behavior is:

  • If the value istrue,output "yes".
  • If the value isfalse,output "no".
  • If the value isnil(or considered asnil),output "maybe".

This three-state logic is very useful when it is necessary to clearly distinguish between "yes

{{ user.IsVip|yesno }} {# 可能会输出 yes, no, 或 maybe #}

The more flexible is,yesnoThe filter allows us to customize the output text of these three states, making its expression fully fit the actual business needs:

{{ user.IsVip|yesno:"已启用,已禁用,未设置" }}

Now, ifuser.IsVipresponse fortrueDisplaying "Enabled" for,false,displaying "Disabled"; asnil,displaying 'Not set'. This perfectly demonstrates the three states of 'OR and NOT': namely, 'Condition is true', 'Condition is false', and 'Condition is not evaluated (due to missing data)'.

Summary

In AnQiCMS template design, understanding and applying these features can greatly enhance the robustness and user experience of the template. Whether throughnilthe handling mechanism of values, it can greatly enhance the robustness and user experience of the template. Whether throughifandfor...emptyImplement binary judgment and list empty status handling, bydefaultanddefault_if_noneProvide detailed default values, or withyesnoFilter implementation with clear three-state logic, these tools can all help us build more flexible, reliable, and user-friendly content display pages.


Common Questions (FAQ)

1. Why does AnQiCMS provide multiple ways to check the "empty" status of data? What are the differences?

AnQiCMS provides various methods to adapt to different "empty" concepts.ifTags andfor...emptyBlocks are mainly used for logical judgment and loop control, they willnilAuto is also considered a boolean, as well as empty strings, zero values, and empty collections.false.defaultThe filter is the same, providing default values for all these 'empty' states.default_if_noneandyesnoThe 'maybe' state is more precise, mainly distinguishing betweennil(which means the variable has not been assigned or points to null) and other 'null values' (such as empty strings)""or numeric0The difference. Understanding these differences can help you choose the most suitable tool and write more accurate template logic.

2. When usingyesnoCan the output be customized for filters, excluding 'yes', 'no', 'maybe'?

Of course you can.yesnoThe filter allows you to provide a comma-separated string to fully customize the display text for these three states. For example,{{ variable|yesno:"真棒,遗憾,待定" }}they will be displayed separately.variableresponse fortrue/falseandnilAt this time, output "Great", "Regret" and "Pending". This flexibility makesyesnovery useful when specific business terms are needed.

3. If a variable may benilAnd I try to directly access its properties, will there be a template rendering error?

Yes, if a variable isnilAnd if you try to directly access its properties (for example{{ myVar.Property }}),AnQiCMS的模板引擎通常会抛出错误,导致页面渲染中断或显示异常。为了