How to implement conditional logic in AnQiCMS to control the display of content?

Calendar 👁️ 60

In website operation, displaying different content based on different conditions is the key to improving user experience and achieving refined operation.AnQiCMS (AnQiCMS) provides powerful and flexible template tags and logical judgment functions, allowing us to easily implement conditional display of content, thereby meeting diverse business needs.

This article will delve into how to make full use of the template engine features of AnQiCMS, and control the display of website content accurately through conditional logic judgments.

Flexible and versatile conditional judgment basis:ifTag

AnQiCMS template engine supports syntax similar to Django'sif/elif/elseandendifLabel, this is the core of realizing conditional judgment. Through these labels, we can decide whether to display a piece of content based on the truth or falsity of various expressions.

The basic judgment structure is very intuitive:

{% if 条件 %}
    <!-- 满足条件时显示的内容 -->
{% endif %}

When the condition involves multiple possibilities, we can introduceelif(else if) andelseto handle:

{% if 条件1 %}
    <!-- 满足条件1时显示的内容 -->
{% elif 条件2 %}
    <!-- 满足条件2时显示的内容 -->
{% else %}
    <!-- 以上条件都不满足时显示的内容 -->
{% endif %}

When constructing conditions, we can use common comparison operators such as==equal,!=Not equal to,>Greater than,<Less than,>=Greater than or equal to,<=less than or equal to), as well as logical operators such asandand,oror,notnon) to combine more complex judgments.

Master content attributes to achieve fine-grained control

AnQiCMS provides a rich set of built-in variables and tags, carrying various attributes of page, document, category, and other content.By using these properties, we can construct various meaningful conditional judgments.

1. Display specific content based on recommended content attributes.

When editing documents in the background, we can set "recommended properties" for the document, such as headlines, recommendations, slides, etc. These properties can be obtained througharchiveDetailorarchiveListtags to get the document'sFlagfield.

For example, if you want to display all the latest documents with the 'Slide[f]' attribute on the homepage:

{% archiveList archives with type="list" flag="f" limit="5" order="id desc" %}
    {% for item in archives %}
        <a href="{{ item.Link }}">{{ item.Title }}</a>
    {% endfor %}
{% endarchiveList %}

If you want to determine whether the current document is recommended on the document detail page,ifCheck statement:

{% if archive.Flag contains "c" %} {# 假设“c”代表推荐属性 #}
    <span class="recommend-tag">推荐</span>
{% endif %}

2. Judge the classification characteristics, build intelligent navigation

When handling the category list, you may need to decide whether to display the submenu or directly link to the category based on whether the category has subcategories.categoryListthe tags returned byitemThe object includes:HasChildrenProperty.

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul>
        {% for item in categories %}
            <li>
                <a href="{{ item.Link }}">{{ item.Title }}</a>
                {% if item.HasChildren %}
                    <!-- 如果有子分类,这里可以再次循环显示子分类,或者显示下拉菜单 -->
                    {% categoryList subCategories with parentId=item.Id %}
                        <ul>
                            {% for subItem in subCategories %}
                                <li><a href="{{ subItem.Link }}">{{ subItem.Title }}</a></li>
                            {% endfor %}
                        </ul>
                    {% endcategoryList %}
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

3. Using custom fields to create personalized content modules

AnQiCMS's 'Content Model' feature allows us to add custom fields to documents or categories.These custom fields are the key to displaying content individually.For example, a product model may have a name calledPricecustom field.

{# 在产品详情页,如果产品价格大于0,则显示价格信息 #}
{% archiveDetail productPrice with name="Price" %}
{% if productPrice > 0 %}
    <p>价格:¥ {{ productPrice }}</p>
{% else %}
    <p>价格面议</p>
{% endif %}

You can also determine whether a custom field exists or its value is empty to decide whether to render the relevant HTML block.

4. Determine if the image exists and optimize the page structure.

In a list or detail page, if the document does not have a thumbnail or cover image, you may not want to display an empty image placeholder. Document detail labelarchiveDetailOr list itemitemofThumborLogofield can be directly used to judge:

{% if item.Thumb %}
    <img src="{{ item.Thumb }}" alt="{{ item.Title }}">
{% else %}
    <img src="/static/images/default-thumb.png" alt="无图"> {# 显示一个默认占位图 #}
{% endif %}

Loop condition judgment: enhance list display

InforCombining in the loopifTags can provide more detailed control over list items.

1. Prompt when the list is empty

forLoop-specificemptyThe tag is very useful, when traversing the empty object,emptythe content inside the tag will be rendered, which is very suitable for displaying prompts such as 'No content'.

{% archiveList archives with type="list" categoryId="10" limit="10" %}
    <ul>
        {% for item in archives %}
            <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
        {% empty %}
            <li>该分类下暂无文章。</li>
        {% endfor %}
    </ul>
{% endarchiveList %}

2. Highlight the current navigation item or a specific element in the list

In the navigation menu or pagination component,itemusually includes aIsCurrentThe attribute can be used to determine whether the current item is the page being visited by the user, thereby addingactivethe class for highlighting.

{% navList navs %}
    <ul>
        {% for item in navs %}
            <li class="{% if item.IsCurrent %}active{% endif %}">
                <a href="{{ item.Link }}">{{ item.Title }}</a>
            </li>
        {% endfor %}
    </ul>
{% endnavList %}

Similarly, in the pagination tagpaginationofpages.Pagesloop, it can also be used.item.IsCurrentHighlight the current page number.

Use the system and contact information settings to implement site-level conditional display.

BysystemandcontactLabel, we can obtain the global configuration information of the website and make conditional judgments accordingly.

1. Website shutdown status reminder

During website maintenance, you can set the website status to closed.At this time, it is possible to display a shutdown prompt by judging the system variables.“`twig {% system siteCloseTips with name=“SiteCloseTips” %} {% system siteStatus with name=“SiteStatus” %} {# Assume SiteStatus 0: Disabled 1: In Use #} {% if siteStatus ==

Related articles

How to set and display the global information of AnQiCMS, such as Logo, filing number, and copyright?

AnQiCMS provides an intuitive and powerful way to manage and display the global information of the website, such as the website logo, filing number, and copyright statement.This information is not only an important part of the website's brand image, but also concerns the legal and compliant operation of the website.The detailed introduction on how to set and utilize these global information will follow. ### Set global information in the background All the global information of the website is concentrated in the "Global Function Settings" of AnQiCMS backend.

2025-11-08

How to display pagination links in AnQiCMS and control the number of page numbers displayed?

In website operation, effectively managing and displaying a large amount of content is the key to improving user experience.When the content list is too long, a reasonable pagination mechanism can not only make it easier for users to browse, but also optimize the page loading speed, and indirectly improve the search engine friendliness.In AnQiCMS, implementing pagination and flexibly controlling the display quantity of page numbers is a relatively direct and powerful process.--- ## Display pagination links elegantly and flexibly control the number of pages in AnQiCMS In your AnQiCMS website, when the number of articles, products, or other list content increases

2025-11-08

How to retrieve and display custom form fields in AnQiCMS comment form?

When using AnQiCMS to manage website content, the online message function is undoubtedly an important means of interacting with visitors, collecting feedback, or gathering potential customer information.In addition to the basic name, contact information, and message content, we often need to collect more specific and personalized information, such as 'Your project budget', 'The type of service you are interested in', or 'The expected contact time'.AnQi CMS provides a flexible custom message field function, allowing us to easily meet these needs.How can I retrieve and elegantly display these custom form fields on the website front page?

2025-11-08

How to display user comment lists and implement pagination in AnQiCMS?

In website content operation, user comments are an important part of enhancing interactivity and activity, and reasonable pagination can ensure that the comment list is both complete and loads smoothly.AnQiCMS (AnQiCMS) provides powerful and flexible template tags, allowing you to easily display and manage user comments on the front-end page, and implement pagination functionality for the comment list.

2025-11-08

How does the AnQiCMS template loop through arrays or objects to display list data?

In the Anqi CMS template system, the core of looping through arrays or objects and displaying list data lies in understanding its Django-like template syntax, especially the use of the `for` loop tag.This mechanism makes the presentation of dynamic content intuitive and efficient, whether it is for article lists, category navigation, or product displays, it can adapt flexibly. ### Core Mechanism: The Use of `for` Loops Data traversal in Anqi CMS templates mainly relies on `{% for ...in ...

2025-11-08

How to format a timestamp in AnQiCMS template and display it in a specified date format?

In AnQiCMS template, processing timestamps and displaying them in a specific date format is a common requirement in content operation.No matter whether you need to display the publication time of the article or the update date of the product, understanding how to flexibly format time will greatly enhance the expressiveness of the website content.AnQiCMS provides simple and powerful template tags, making this process easy and convenient.

2025-11-08

How to define and use variables in AnQiCMS to display data flexibly in templates?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system, whose powerful template function is the key to building a personalized website.In templates, defining and using variables flexibly allows us to easily handle and display various types of data, whether it's article titles, categorization information, or custom field content. All of this can be dynamically called through variables, making the website full of vitality.We will delve deeper into how to define, obtain, and use variables in AnQiCMS templates, helping everyone build and maintain websites more efficiently.

2025-11-08

How to use the template inheritance and reference feature of AnQiCMS to build reusable display modules?

When using AnQiCMS to build a website, we often encounter such situations: each page of the website has common header navigation, footer information, or some modules (such as sidebars, article recommendation lists) need to be displayed in multiple places, but the content or style may be slightly different.If you copy and paste this code every time, not only is it inefficient, but also once it needs to be modified, you have to adjust it one by one on all related pages, which is very easy to make mistakes and difficult to maintain.

2025-11-08