How to determine if the boolean value of a custom field in the AnQiCMS template is true and dynamically add a CSS class?

Calendar 👁️ 67

In AnQiCMS template development, we often need to adjust the display style of the page based on the specific attributes of the content.This includes adding CSS classes dynamically based on boolean values of custom fields (i.e., true or false status), thus achieving a more flexible and expressive interface.

AnQiCMS with its flexible content model design, allows users to create various custom fields for different content types (such as articles, products).These custom fields greatly enrich the expression of content, enabling templates to be highly customized for different business needs.

Understand the custom boolean field in AnQiCMS

Although AnQiCMS does not provide a field type named 'Boolean', we can simulate boolean logic through other field types. This is usually done in the following way:

  1. Single choice field: Create a radio (radio) or dropdown (select) field with options set to "Yes/No", "True/False", or "1/0".
  2. Text fieldEnter 'true', 'false', '1', or '0' directly.
  3. Numeric fieldStore '1' for true, '0' for false.

Understanding how your custom field stores boolean values is key to determining its true or false nature.

Get the value of the custom field.

In the AnQiCMS template, we mainly usearchiveDetailorarchiveParamstags to obtain custom field values for articles, products, and other content.

Assuming you have created a custom field in the content model, such as namedis_featuredIt represents whether it is recommended, it is set to a single selection type, the options are yes and no. In the article detail page template, you can get the value of this field in this way:

{# 获取当前文档的自定义字段 'is_featured' 的值 #}
{% archiveDetail is_featured_value with name="is_featured" %}

here,is_featured_valueThe variable will store a custom fieldis_featuredThe value (for example, “yes” or “no”).*

Determine the boolean value and dynamically add a CSS class

After obtaining the value of the custom field, the next step is to judge its truth or falsity and dynamically add CSS classes to the HTML element. The AnQiCMS template engine supports powerful conditional judgments ({% if %}Tags and filters make this task very intuitive.

Considering that custom fields may be stored as strings ("yes","no","true","false") or numbers (1,0), we can cleverly utilizeyesnoA filter for general judgment.yesnoThe filter can map a value to one of the three states: "yes", "no", or

Let's go aboveis_featured_valueFor example, if it stores “yes” or “no”:“

<div class="article-item
    {%- if is_featured_value|yesno:"是,否," == "是" -%}
        featured-article
    {%- endif -%}
">
    <!-- 这里是文章内容 -->
    <h2 class="article-title">
        {{- archive.Title -}}
    </h2>
    <p class="article-description">
        {{- archive.Description -}}
    </p>
    {# 其他文章详情内容 #}
</div>

In this example:

  • is_featured_value|yesno:"是,否,"This part is the core. It tells the AnQiCMS template engine ifis_featured_valueIf the value equals 'yes', return the string 'yes'; if it equals 'no', return 'no'; if it is neither of these (such as a field not filled in or empty), return an empty string.
  • == "是": We checkyesnoWhether the result returned by the filter is "yes", thus determining the boolean state to be true.
  • featured-article: If the condition is true, this CSS class will be added todivthe element.

If you store custom fields as numbers (1 represents true, 0 represents false), you can useintegera filter to make the judgment:

{# 假设 'is_promoted' 字段存储为 1 或 0 #}
{% archiveDetail is_promoted_value with name="is_promoted" %}

<div class="product-card
    {%- if is_promoted_value|integer == 1 -%}
        promoted-product
    {%- endif -%}
">
    <!-- 这里是产品卡片内容 -->
    <h3 class="product-name">
        {{- archive.Title -}}
    </h3>
    <span class="product-price">
        ¥{{- archive.Price -}}
    </span>
    {# 其他产品详情内容 #}
</div>

here,is_promoted_value|integer == 1Willis_promoted_valueconvert it to an integer and then check if it is equal to 1.

Application scenarios in practice

Dynamically adding CSS classes has a wide range of applications in website operations:

  1. Highlighting important contentHighlight articles where the 'Recommended' field is true or use a different background color.
  2. Special layout processing:To provide exclusive layout styles for product cards marked as “VIP Content”, attract user clicks.
  3. Visual feedback: Add a gray overlay or 'SOLD OUT' label dynamically based on the content status (such as the 'sold out' field being true).
  4. A/B test: Test the visual effects of page elements through different CSS classes to optimize the user experience.

In this way, you can make your AnQiCMS website template more intelligent, automatically adapting and displaying a **user experience.


Frequently Asked Questions (FAQ)

1. If my custom field is of 'number' type, storing 1 or 0 to represent boolean values, how should I judge?

You can use it directly.integerThe filter converts the field value to an integer for judgment. For example, if the custom field name isis_active, you can write it like this:{% if archive.is_active|integer == 1 %}.

2. My custom field is of 'single choice' type, with options 'True' and 'False'. How do I determine which is true?

Similar to the examples of "yes/no" in the article, you can useyesnoa filter and customize the mapping values. For example:{% if custom_field_value|yesno:"True,False," == "True" %}.

3. If the custom field may be empty (not set), how will the template handle it, will it cause an error?

AnQiCMS template engine handles null values (nil or empty strings) very robustly. It can be used directly in{% if %}tags, such as{% if custom_field_value %}

Related articles

How to combine `if` and `yesno` filters in AnQiCMS templates to display different prompts based on VIP status and expiration time?

In website operations, providing differentiated services and content prompts to different user groups is a key link in improving user experience and achieving business goals.Especially on websites with a VIP membership system, displaying personalized information based on the user's VIP status and even the expiration time of the membership can greatly enhance the user's sense of belonging and willingness to interact.AnQiCMS (AnQiCMS) relies on its flexible template engine and rich tags, filters, making everything simple and efficient.Today, let's discuss how to use the AnQiCMS template

2025-11-09

How to ensure that the AnQiCMS template does not cause page rendering errors when the variable is `nil`?

When building a website with AnQiCMS, we often encounter the situation where template variables may be empty (`nil`).For example, a document may not have a thumbnail set or a custom field may not be filled in.If the template code does not properly handle these potential null values, errors may occur during page rendering, affecting user experience and even causing some website functions to be unavailable.Fortunately, the AnQiCMS template engine provides various mechanisms to help us elegantly handle the cases where variables are `nil`, ensuring the stability and smoothness of the page

2025-11-09

In AnQiCMS template, which filters or tags behave in three states of 'or and not' logic for `nil` values?

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 contain `nil` (empty) values, it is particularly important to elegantly display content, provide default values, or execute specific logic in the template.AnQiCMS provides various filters and tags that can help us display three states of existence, non-existence, or undefined for `nil` values, thereby enhancing the flexibility and user experience of templates.### One, directly judge whether the data exists: `if`

2025-11-09

What does the `stringformat` filter return when it fails to handle formatting in the AnQiCMS template?

AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and flexible template engine, bringing great convenience to content management.In daily content operation, we often need to present data in a specific format on the website front-end, which cannot do without various practical template filters.The `stringformat` filter is a powerful tool for data formatting, allowing us to format numbers, strings, and even more complex data types in a precise way, similar to the `fmt.Sprintf` function in the Go language.Understand

2025-11-09

In AnQiCMS template, how to add a recommended icon to the article title based on the boolean state of the `item.Flag` attribute?

When managing website content in AnQiCMS, we often hope to highlight important articles through some intuitive visual elements, such as adding a prominent small icon to recommended content.This not only attracts the attention of visitors, but also helps them quickly find high-quality information on the website.It is fortunate that AnQiCMS has built-in `item.Flag` properties and a flexible template engine, making it very easy to implement this requirement.This article will discuss in detail how to use the `item.Flag` attribute status in the AnQiCMS template

2025-11-09

How to calculate the number of times a specific keyword appears in a string or array using the `count` filter in AnQiCMS templates?

During the template development process of AnQiCMS, we often need to perform dynamic data analysis or display of page content, such as counting the frequency of a keyword or checking the number of specific elements in a list.At this time, the `count` filter has become a very practical tool.It can help you quickly and efficiently calculate the number of times a specific keyword appears in a string or array, providing the possibility of flexible template presentation.--- ### `count` filter: Accurately count your content In short, `count`

2025-11-09

How can you get the document details of Anqi CMS by document ID?

When using AnQi CMS to manage website content, we often need to retrieve detailed information about specific documents from the backend, whether it is to display content on custom pages or integrate data into other systems (such as mini-programs, mobile applications, etc.).AnQi CMS provides a powerful and flexible API interface, one of the core functions of which is to obtain specific document details through the document ID.Today, let's dive deep into how to use the document ID to easily obtain the details of Anqi CMS documents, making your content operations more efficient and automated.### Core Steps

2025-11-09

How to get the details of a document through the document URL alias (`filename`)?

In AnQi CMS, the document URL alias (`filename`) is a very practical feature, which not only makes your website URL more friendly and improves search engine optimization (SEO) effect, but also provides great convenience in content acquisition.When you need to obtain detailed information about a specific document through programming, AnQiCMS provides a simple and intuitive API interface.### Core Feature Revelation: Get Details Using Document URL Alias To get all details of the document through its URL alias

2025-11-09