How to display different content in the AnQiCMS template based on the value of the `item.Status` field (such as approved, in review)?

Calendar 👁️ 86

In website content operation, the content review status is a very important link.Whether it is comments submitted by users, forum posts, or articles and product information published by website administrators, they often need to be reviewed before they can be displayed to the public.AnQiCMS provides a flexible template mechanism, allowing us to easily display different content on the front-end page based on the review status of the content (such as "approved" or "under review"), thus providing users with clearer and more accurate feedback.

The template syntax of AnQiCMS is similar to the Django template engine, which makes conditional judgments intuitive and easy to understand. The core idea is to utilizeitemWithin the objectStatusField judgment. When you loop through or retrieve content in a template (such as comments, articles, etc.), thisitemobject usually contains aStatusThe field is used to indicate the review status of the current content. Normally,Statusthe field uses numbers to represent different statuses, such as,1which may represent 'reviewed passed,' and0It may represent “Under review” or “Pending review”.

Based onitem.Statusthe value to conditionally display content, we mainly use{% if %}Label. This tag allows you to make conditional judgments about variables and display different template content based on the judgment.

How to useitem.Statusto achieve conditional display

Let's take a common scenario - a comment list as an example. After users submit comments, they may need to be reviewed by administrators before they can be displayed. In the AnQiCMS template, you cancommentListLabel to get comment data, then iterate over these comments and judge according to theirStatusvalues.

Assuming we have already passed{% commentList comments with archiveId=archive.Id type="list" limit="6" %}Such a label gets the comment list,commentsThe variable contains multiple comment data, each comment corresponds to oneitemObject.

Now, we can check each comment in the loopitem.StatusField:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
      <div>
        <span>
          {% if item.Status == 1 %} {# 如果Status是1,表示审核通过 #}
          {{item.UserName}}
          {% else %} {# 否则,表示审核中 #}
          审核中:{{item.UserName|truncatechars:6}}
          {% endif %}
        </span>
        {# 如果存在父级评论,也进行状态判断 #}
        {% if item.Parent %}
        <span>回复</span>
        <span>
          {% if item.Parent.Status == 1 %}
          {{item.Parent.UserName}}
          {% else %}
          审核中:{{item.Parent.UserName|truncatechars:6}}
          {% endif %}
        </span>
        {% endif %}
        <span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
      </div>
      <div>
        {% if item.Parent %}
        <blockquote>
          {% if item.Parent.Status == 1 %}
          {{item.Parent.Content|truncatechars:100}}
          {% else %}
          该内容正在审核中:{{item.Parent.Content|truncatechars:9}}
          {% endif %}
        </blockquote>
        {% endif %}
        {% if item.Status == 1 %}
        {{item.Content}} {# 审核通过的评论内容直接显示 #}
        {% else %}
        该内容正在审核中:{{item.Content|truncatechars:9}} {# 审核中的评论内容进行提示 #}
        {% endif %}
      </div>
      <div class="comment-control" data-id="{{item.Id}}" data-user="{{item.UserName}}">
        <a class="item" data-id="praise">赞(<span class="vote-count">{{item.VoteCount}}</span>)</a>
        <a class="item" data-id=reply>回复</a>
      </div>
    </div>
    {% empty %}
    <div>暂无评论</div>
    {% endfor %}
{% endcommentList %}

In the above code snippet, we use{% if item.Status == 1 %}To determine whether the comment has been reviewed. IfStatusequals1then display the full username and comment content; ifStatusnot equal to1(for example, is0If this is the case, a "Under review" prompt will be displayed, and some content may be displayed partially or hidden.|truncatechars:6and|truncatechars:9This filter is used to truncate strings to prevent too much unreviewed content from leaking.|safeThe filter is also essential here, as it allows the HTML content stored in the background (such as rich text editor-generated comments) to be safely rendered on the page instead of displaying it as plain text.

Expand Application Scenarios and Precautions

Although the above examples are based on comments, this is based on the followingitem.StatusThe method of conditional display also applies to any content type that contains a status field, such as articles, products, etc. As long as you obtain the data item (itemThere is a field representing the status, and you are aware of the meanings of different values of this field, you can use{% if %}/{% elif %}and{% else %}tags to construct flexible display logic.

Please note the following points when in use:

  1. Field name and value:ConfirmStatusThe exact name of the field (for example),Status/AuditStatusand its corresponding numerical meaning (for example),0Pending review,1The review has passed,2The review has been rejected).This information is usually found in the AnQiCMS content model settings or related documents.
  2. Data type match: When making conditional judgments, ensure that the comparison operator==is consistent on both sides. For example, ifitem.Statusis an integer, then you should use== 1instead of== "1". AnQiCMS's template engine usually handles it intelligently, but maintaining type consistency is a good programming habit.
  3. Cache problem:After modifying the template, if the page content does not update immediately, please try to clear the cache of the AnQiCMS backend to ensure that the latest template file is loaded.

By using AnQiCMS's flexible template tags and conditional judgment mechanism, you can easily display differentiated information on the front-end page according to the content status, greatly enhancing the fineness and user experience of website content management.


Frequently Asked Questions (FAQ)

Q1:item.StatusDoes the field only used for comments? Is there a similar field in the article or product detail page?A1: Although in the AnQiCMS documentcommentListThe tag explicitly mentioneditem.Statusfield, but in practice, AnQiCMS as an enterprise-level content management system, usually also provides similar status management fields for content models such as articles, products, and so on. These fields may be namedStatus/AuditStatusOr similar names, their functions are all to indicate different review statuses of content.You can confirm this by checking the data structure of the corresponding content model or observing whether there are related status options when editing content in the background.If it exists, judgment in the templateitem.StatusThe method applies as well.

Q2: If you need to judge multiple review statuses (such as: pending review, review passed, review rejected), how should the template code be written?A2: When there are multiple review statuses, you can use{% elif %}(the abbreviation of else if) tag to extend your conditional judgment logic. For example, assuming0pending review,1Passed for review,2Rejected for review:

{% if item.Status == 1 %}
    <span style="color: green;">审核通过</span>
{% elif item.Status == 0 %}
    <span style="color: orange;">待审核</span>
{% elif item.Status == 2 %}
    <span style="color: red;">审核拒绝</span>
{% else %}
    <span>状态未知</span>
{% endif %}

This way, you can display different styles or hints for each status.

Q3: My content status is clearly 'Approved', why is it still showing 'Under Review' on the page?A3: This could be due to several reasons:

  1. The background has not been saved or updated:Make sure you have clicked save in the AnQiCMS background after modifying the content status, and the data has been successfully updated to the database.
  2. Template cache:The AnQiCMS system will use caching to improve performance.Even if the data has been updated, the old page content may still be cached.Please log in to the AnQiCMS backend, find the "Update Cache" or "Clear Cache" function and execute it, then refresh the front-end page.
  3. Field name or value does not match:Please check your template codeitem.Statuswhether the spelling is correct, as well as the state values you use for comparison (for example,== 1Is consistent with the actual status value defined in the background. For example, if the status defined as 'Audit Passed' in the background istrueor“approved”, and you judge in the template is== 1, it will lead to a mismatch.

Related articles

How to judge if there is a thumbnail in the `archiveList` tag in AnQiCMS template through `if` to selectively display images?

In AnQiCMS template development, displaying list content is a common requirement, and how to elegantly handle the images in these lists, especially thumbnails, is directly related to the visual effects and user experience of the website.The `archiveList` tag is one of the core content call tags of AnQiCMS, which helps us flexibly obtain various document lists.However, in practice, we often encounter situations where certain documents have not set thumbnails, which may lead to broken images or layout confusion on the page. At this time

2025-11-09

In AnQiCMS template, how to judge if the list is empty and display a prompt of 'No content'?

When using AnQiCMS for website template development, you often encounter situations where you need to display list data, such as article lists, product lists, or image galleries.When we retrieve data through template tags (such as `archiveList` or `categoryList`) and use a `for` loop to iterate over the list, if the list is empty, it is usually necessary to provide the user with a friendly prompt instead of displaying a blank space.How can you elegantly judge whether a list is empty in a `for` loop and display a 'No content' prompt?

2025-11-09

In AnQiCMS template, how to decide whether to display the default image or placeholder based on the existence of a variable?

In the template development of AnQiCMS, we often encounter situations where the content may not contain images.For example, a news article may not have an illustration, or a certain product in a product list may not have uploaded the main image temporarily.In this case, if the template directly calls the image address, it may display a broken image icon or leave an abrupt blank, which undoubtedly affects the overall aesthetics and user experience of the website.To solve this problem, AnQiCMS provides flexible template tags and filters, allowing us to determine whether the image variable exists

2025-11-09

How to elegantly handle null or undefined variables returned by the database in AnQiCMS templates to avoid page errors?

During the development of website templates, we often encounter a headache-inducing problem: when the data obtained from the database is empty (null) or a variable is not defined under certain circumstances, the template rendering will cause an error, resulting in the page not displaying normally and significantly reducing the user experience.AnQiCMS (AnQiCMS) is based on its powerful Go language backend and flexible Django-style template engine, providing us with various elegant solutions for handling such situations, allowing the template to run smoothly even when the data is incomplete.### One, make good use of conditional judgments: `{% if

2025-11-09

In AnQiCMS template, how to judge and display the 'In stock' or 'Out of stock' status based on the product inventory (`Stock`) quantity?

It is crucial to clearly communicate the inventory status of products to users in website operations, especially for sites involving product display.This not only optimizes the user experience, reduces invalid consultations, but also effectively guides the user's purchase decision.AnQiCMS as a flexible and efficient content management system, implements the display of "in stock" or "out of stock" status according to the product inventory quantity in the template, which is very intuitive. AnQiCMS template system adopts a syntax similar to Django, which allows us to control the display of page content through concise tags and variables.When processing product information

2025-11-09

In AnQiCMS template, how to judge whether a string can be successfully converted to a numeric type and perform conditional processing?

In AnQi CMS template creation, we often encounter situations where we need to process strings entered by users or retrieved from the database.One common requirement is to determine whether a string can be successfully converted to a numeric type and to perform different conditional processing based on the result.This is crucial for data display, calculation, and even simple form validation.The AnQi CMS template engine (based on Go language's Pongo2) provides a rich set of filters (filters) and logical tags, allowing us to flexibly meet this requirement. Below

2025-11-09

What is the default return value when the `integer` and `float` filters fail to convert in the AnQiCMS template?

When building a website on Anqi CMS, we often need to flexibly handle and display data in the template.These, `integer` and `float` filters are very commonly used tools when converting values to integers or floating-point numbers.However, have you ever thought about how the system will handle when these filters receive a value that cannot be recognized as a number?In other words, what default values will these filters return if the conversion operation fails?Understanding this is crucial for us to write robust and predictable template logic.###

2025-11-09

How to determine whether to truncate text and add an ellipsis (...) in AnQiCMS templates based on content length?

In AnQiCMS website content operation, how to elegantly handle long text content to make it both beautiful and complete on the page is the key to improving user experience.Especially on the list page, card display, or introduction area, overly long text often leads to layout confusion and affects the overall visual effect.AnQiCMS' powerful template engine provides various flexible ways to solve this problem, the most commonly used being the function of text truncation and adding ellipses.The AnQiCMS template system borrows the syntax of the Django template engine

2025-11-09