During the process of building a website with AnQiCMS, we often need to display various data on the front-end page, and these data often have different states.For example, an article is published or a draft, a product is on sale or off the shelves, or whether a user is active.How to present these states clearly and intuitively to users while maintaining the simplicity and readability of the template code is a common issue for content operation and template developers.Today, let's delve into a very practical tool in AnQiCMS——yesnoFilter, it can help us solve this problem elegantly.

yesnoFilter: Make the state expression more intuitive.

yesnoThe filter is a built-in tool provided by the AnQiCMS template engine, which core function is to output different text based on the value of a variable (usually a boolean value or data that can be interpreted as a boolean). It can transformtrue/falseandnilEmpty value mapping these three states to the specified descriptive text, making the status information of the data clear at a glance.

By default,yesnoThe filter will also treattrueValue is converted to “yes”,falseValue is converted to “no”, andnilIf empty, it is converted to 'maybe'. This default behavior is already sufficient in many scenarios, but its true strength lies in its high customizability. We can define any text we want for these three states according to our actual needs.

Why chooseyesnoFilter?

UseyesnoFilter instead of traditionalif-elseLogic to display simple boolean states, with several significant advantages:

FirstlySimplicity and readability of codeImagine if you had to write a judgment for every state{% if condition %}显示文字{% else %}显示另外的文字{% endif %}The code would become lengthy.{{ item.IsActive|yesno:"活跃,冻结,未知" }}This style can be done in one line of code, and the semantics are also clearer, expressing the corresponding relationship between variables and display text at a glance.

Next isCentralized management of output text. ThroughyesnoFilter, we can define the text corresponding to the state in the filter parameters, rather than scattered in multipleifInside the branch. When it is necessary to adjust the display text, it only needs to be modified in one place, which greatly reduces the maintenance cost, especially in a multilingual environment, this advantage is even more obvious.

Finally, it provides a kind ofelegant way to handle missing data (nil). In actual development, data fields occasionally appear to be empty or unset.yesnoThe default "maybe" output or our custom "status unknown" text, etc., can provide a friendly prompt when the data is incomplete, rather than allowing the page to display blank or error messages.

yesnoThe combination of filter and data list practice

Now, let's look at a specific example to demonstrateyesnoHow does the filter combine with AnQiCMS's data list to display the specific status of each data item.

Assuming our article (or any document model) has a custom field namedIsRecommended(whether recommended),