In website content operation, content review 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 being displayed to the public.AnQiCMS provides a flexible template mechanism that allows 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"), thereby 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 utilizeitemthe object ofStatusField judgment. When you loop through or retrieve some content in a template (such as comments, articles, etc.), thisitemobject usually contains aStatusField, used to indicate the review status of the current content. Usually,Statusthe field will use numbers to represent different statuses, such as1may represent “Passed review”,0May represent "Under review" or "Pending approval".

To determineitem.Statusvalue to conditionally display content, we will mainly use{% if %}[en]This tag allows you to make conditional judgments on variables and display different template content based on the judgment results.

[en]How to base onitem.Status[en]to implement conditional display

Let's take a common scenario - a comment list, for example. After a user submits a comment, it may need to be reviewed by an administrator before it is displayed. In the AnQiCMS template, you can do it throughcommentListLabel retrieves comment data, then iterate over these comments and judge according toStatusthe value.

Suppose we have already{% commentList comments with archiveId=archive.Id type="list" limit="6" %}such labels to get the comment list,commentsThe variable contains multiple comment data, each comment corresponds to aitemobject.

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

{% 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 judge whether the comment has been reviewed and approved.Statusequals1Then display the full username and comment content; ifStatusNot equal to1(for example, is)0Then a "Under review" prompt is displayed, and only part of the content or key information may be shown.|truncatechars:6and|truncatechars:9This filter is used to truncate strings to prevent too much of the unreviewed content from being leaked. At the same time,|safeThe filter is also indispensable here, as it allows the HTML content stored in the background (such as comments generated by a rich text editor) to be safely rendered on the page instead of displaying it as plain text.

Expand Application Scenarios and Precautions

Although the above examples are given with comments as an example, this kind of according toitem.StatusThe method for conditional display applies to any content type that includes a status field, such as articles, products, etc. As long as you obtain the data item (itemIn it there is a field representing the status, and once you are clear about the different values of this field, you can use{% if %}/{% elif %}and{% else %}tags to build flexible display logic.

Please note the following points when in use:

  1. Field name and value:ConfirmStatusThe exact field name (e.g.,)Status/AuditStatusetc.) and its corresponding numerical meaning (e.g.,)0Pending review,1Passed the review.2Review rejected).This information can usually be found in the content model settings or related documents of AnQiCMS.
  2. Data type matches:When performing conditional judgments, ensure that the comparison operator==on both sides have consistent data types. For example, ifitem.Statusis an integer, then you should use== 1Instead== "1"The template engine of AnQiCMS usually handles it intelligently, but maintaining consistent types is a good programming habit.
  3. Cache issue: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 the flexible template tags and conditional judgment mechanism of AnQiCMS, you can easily display differentiated information on the front-end page according to the content status, greatly enhancing the fineness of website content management and user experience.


Common Questions (FAQ)

Q1:item.StatusIs the field only used for comments? Are there similar fields in the article or product detail page?A1: Although it is mentioned in the AnQiCMS documentcommentListThe label is explicitly mentioneditem.Statusfields, but in practical applications, AnQiCMS as an enterprise-level content management system usually also provides similar status management fields for content models such as articles, products, etc. These fields may be namedStatus/AuditStatusThe names or similar terms are all for the purpose of indicating the different review statuses of the content.You can confirm this by checking the data structure of the corresponding content model or observing if there are related status options when editing content in the background.item.StatusThe method applies similarly.

Q2: How should the template code be written if it needs to judge multiple review statuses (such as: pending review, review passed, review rejected)?A2: When there are multiple review statuses, you can use{% elif %}(an abbreviation for else if) labels to extend your conditional logic. For example, suppose0pending review,1Reviewed and approved.2Review rejected:

{% 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 %}

Thus, you can display different styles or messages for each status.

Q3: Why is the page still showing 'Under Review' when my content status is clearly 'Approved'?A3: This could be due to several reasons:

  1. The background has not been saved or updated:Make sure you have clicked save after modifying the content status in the AnQiCMS backend, and the data has been successfully updated to the database.
  2. Template Caching:AnQiCMS system uses cache 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" feature and execute it, then refresh the front-end page.
  3. Field name or value does not match:Please carefully check your template code:item.Statuswhether the spelling is correct, as well as the status value you use for comparison (e.g.)== 1Whether it is consistent with the actual status value defined in the background. For example, if the background defines the status 'Approved' astrueor“approved”, and you judge it in the template as== 1, it will lead to a mismatch.