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:
- Field name and value:Confirm
StatusThe 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. - 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. - 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:
- 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.
- 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.
- Field name or value does not match:Please check your template code
item.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.