How to elegantly handle the case where the document content is empty in AnQi CMS?
During the operation of the website, we often publish various types of documents that carry important information. However, for various reasons, such as the content being in draft stage, omissions during data import, or simply some documents only having titles without detailed content, we may encounter documents that are...ContentField is empty.
If the template does not perform any processing when rendering these document detail pages, users may see a blank page area, which not only affects user experience but may also lead search engines to mistakenly believe that the page lacks substantial content, thereby affecting the SEO performance of the website.As content operators, of course, we hope that every page of the website can provide valuable displays, even if the content is incomplete, there can also be friendly prompts.
The Anqi CMS provides a flexible template tag system that allows us to easily deal with such situations. Today, let's discuss how to make use ofarchiveDetailLabel and condition judgment, cleverly handling documentsContentIssue of empty fields and displaying alternative content.
KnowarchiveDetailTags andContentField
In the template design of AnQi CMS,archiveDetailTags are a powerful tool for obtaining detailed information about a single document. They can retrieve various attributes of a document, such as its title, description, content, and images, based on the document's ID or URL alias.
Among them,ContentThe field is the carrier of our document's main content. Usually, we refer to it on the document detail page like this:
{% archiveDetail mainContent with name="Content" %}
{{ mainContent|safe }}
Here,{% archiveDetail mainContent with name="Content" %}will assign the current document'sContentcontent tomainContentVariables. Since document content often contains HTML tags, in order to ensure these HTML tags are parsed correctly by the browser and not displayed as plain text, we also need to use|safeFilter it to mark it as safe content, avoiding automatic escaping.
Cunning approach: use conditional judgment check.ContentIf it is empty.
Now, the question is: ifmainContentThis variable gets an empty content, what should we do? The AnQi CMS template engine provides powerful conditional judgment features, among whichifThe label is the key to solving this problem.
We can use{% if 变量名 %}such a structure to determine whether a variable contains valid content. WhenmainContentThe variable is not empty (i.e., it contains non-whitespace characters or HTML content) when,ifthe condition will be evaluated as true; conversely, ifmainContentit is an empty string or undefined,ifthe condition will be evaluated as false.
Based on this logic, we can organize the template code in such a way that a substitute display is shown when the content is empty:
{% archiveDetail mainContent with name="Content" %}
{% if mainContent %}
<div class="document-content">
{{ mainContent|safe }}
</div>
{% else %}
<div class="no-content-fallback">
<p>抱歉,该文档目前没有提供详细内容。</p>
<p>您可以查看其他相关文章,或稍后再次访问。</p>
<p><a href="/">返回首页</a> | <a href="/contact.html">联系我们</a></p>
</div>
{% endif %}
In this piece of code, we first attempt to retrieve the document'sContent.mainContentIf the variable has a value, we will display the document content normally. IfmainContentthere is no value,elseThe part of the code will be executed at this time, and we will display a friendly prompt message to guide the user back to the homepage or contact us.This way, regardless of whether the document content is rich or lacking, the user can have a complete page experience.
Further optimization: multi-level content fallback strategy
Sometimes, we may not only want toContentWhen empty, display a generic prompt and it is hoped that other fields of the document can be used as more personalized alternative content. For example, ifContentis empty, can we try to display the document'sDescription[en] Where is the (summary) field?
This can also be achieved through nestedifjudgment:
{% archiveDetail mainContent with name="Content" %}
{% if mainContent %}
<div class="document-content">
{{ mainContent|safe }}
</div>
{% else %}
{% archiveDetail docDescription with name="Description" %} {# 尝试获取文档简介 #}
{% if docDescription %}
<div class="no-content-fallback">
<h3>内容简介</h3>
<p>{{ docDescription }}</p>
<p>很抱歉,当前文档尚未提供详细正文,以上是文档简介。</p>
<p><a href="/">查看更多文章</a></p>
</div>
{% else %}
<div class="no-content-fallback">
<p>抱歉,该文档暂无详细内容和简介。</p>
<p>您可以浏览其他分类或使用搜索功能寻找感兴趣的内容。</p>
</div>
{% endif %}
{% endif %}
This more完善的 code demonstrates a multi-level fallback logic:
- Firstly, try to display the document's
Content. - If
ContentIf empty, try to get the document'sDescription. - If
DescriptionIf it exists, display it as a substitute and include a hint. - If
ContentandDescriptionIf all are empty, display the final general hint.
In this way, we ensure that the page maintains a certain amount of information and friendly interaction even in the most sparse content situations.
Summary
In the AnQi CMS, utilizingarchiveDetailTag fetches document content and combinesifTag makes conditional judgment, to process the documentContentA valid strategy for empty fields.This method not only improves user experience and avoids the appearance of blank pages, but also helps maintain the professional image of the website and the health of SEO.By flexibly utilizing the template functions provided by AnQi CMS, we can build more robust and user-friendly websites.
Common Questions (FAQ)
1. This judgment logic is only applicable toContentthe field?
No.{% if 变量名 %}This conditional judgment logic can be used to check whether any variable contains valid content (non-empty string, non-zero number, non-empty object, etc.). Therefore, you can apply it toarchiveDetailTag retrieval of other fields, such asTitle/Description, even custom fields, to achieve more refined display control.
2. If I want to display a default image instead of a text prompt when the content is empty, how can I do that?
You can use a similar conditional judgment logic as above.elseIn the branch, you can insert a<img>tag and set itssrcattribute to point to the default image address you have set. For example:
{% if mainContent %}
{{ mainContent|safe }}
{% else %}
<div class="no-content-image-fallback">
<img src="/static/images/default-no-content.jpg" alt="内容缺失,请稍后查看" />
<p>文档内容正在更新中,敬请期待。</p>
</div>
{% endif %}
How to avoid creating a completely blank document while editing documents in the background?
The backend content editor of AnQi CMS usually provides rich text editing features and supports setting certain fields as required. To avoid creating documents with completely blank content, you can set the content model settings to includeContentField configuration is required.In addition, when releasing documents, it is a good practice to cultivate the habit of checking the completeness of the content and make reasonable use of the document summary (Description) field to ensure that there is also backup information available for display even if the main content is missing.