How to determine if the `Content` field of the document is empty using the `archiveDetail` tag and display alternative content?

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, due to various reasons, such as content still in draft stage, omissions during data import, or simply some documents that are only titled without detailed text, we may encounter documents that theContentThe field 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 the user experience but may also cause 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 display, even if the content is incomplete, it can also have a friendly prompt.

AnQi CMS provides a flexible template tag system, allowing us to easily deal with such situations. Today, let's discuss how to make use ofarchiveDetailLabel and condition judgment, skillfully handle documentsContentHandle the issue of empty fields and display alternative content.

Get to knowarchiveDetailwith the tag andContentfield

In the template design of AnQi CMS,archiveDetailTags are a tool used to obtain detailed information about a single document. It can fetch various properties such as the title, description, content, images, etc. based on the document's ID or URL alias.

Among them,ContentThe field is the carrier of the main content of our document. Usually, we call it like this on the document detail page:

{% archiveDetail mainContent with name="Content" %}
{{ mainContent|safe }}

here,{% archiveDetail mainContent with name="Content" %}Will assign the content of the current document'sContentTo assign the content ofmainContentVariables. Since the document content often contains HTML tags, we also need to use them to ensure that these HTML tags are parsed correctly by the browser and not displayed as plain text.|safeThe filter is used to mark it as safe content, to avoid being automatically escaped.

Clever response: use conditional judgment checkContentIf it is empty

Now, the question is: ifmainContentThis variable is empty, what should we do? The Anqi CMS template engine provides powerful conditional judgment features, whereifThe label is the key to solving this problem.

We can use{% if 变量名 %}Such a structure is used to determine whether a variable contains valid content. WhenmainContentA variable is not empty (i.e., it contains non-whitespace characters or HTML content) when,ifthe condition is evaluated as true; conversely, ifmainContentit is an empty string or undefined,ifthe condition is evaluated as false.

Based on this logic, we can organize the template code in such a way that a substitute display is implemented 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 section of the code, we first attempt to obtain the document'sContentIfmainContentIf the variable has a value, we will normally display the document content. IfmainContentno value,elseThe code will be executed, at this time we will display a friendly prompt and guide the user back to the homepage or contact us.In this way, whether the document content is rich or lacking, users can get a complete page experience.

Further optimization: Multi-level content fallback strategy

Sometimes, we may not only want toContentIf the content is empty, a general prompt is displayed, and it is hoped that other fields in the document can be used as more personalized alternatives. For example, ifContentis empty, can we try to display the document'sDescription(Introduction) What is the field?

This can also be achieved through nested.ifJudgment can be achieved:

{% 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 refined code demonstrates a multi-level fallback logic:

  1. First, try to display the document'sContent.
  2. IfContentIf empty, try to retrieve the document.Description.
  3. IfDescriptionIf it exists, display it as a substitute and include a hint.
  4. IfContentandDescriptionIf all are empty, display the final general hint.

In this way, we ensure that even in the most sparse content, the page can maintain a certain amount of information and friendly interaction.

Summary

In the Aiqi CMS, make use ofarchiveDetailThe tag retrieves the content of the document and combinesifThe tag performs conditional judgment, it is for processing documentsContentAn effective strategy for empty fields. This method not only enhances user experience and avoids the appearance of blank pages, but also helps maintain the professional image of the website and SEO health.By flexibly using the template functions provided by Anqi CMS, we can build more robust and user-friendly websites.


Frequently Asked Questions (FAQ)

1. This judgment logic only applies toContentdo you?

No, it's not.{% if 变量名 %}This conditional judgment logic can be used to check if any variable contains valid content (non-empty string, non-zero number, non-empty object, etc.). Therefore, you can apply it toarchiveDetailother fields obtained by the label, for exampleTitle/Descriptioneven custom fields, to achieve more refined display control.

How do I show a default image instead of a text prompt when the content is empty?

You can use a similar conditional judgment logic as above.elseIn the branch, you can insert one<img>the tag and use it.srcThe attribute points 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 %}

3. How to avoid creating a completely blank document when editing documents in the background?

The AnQi CMS backend content editor usually provides rich text editing features and supports setting certain fields as required. To avoid creating a completely blank document, you can setContentThe field is required. In addition, when publishing documents, develop the habit of checking the completeness of the content, and make reasonable use of the document description (Description) field to ensure that there is also alternative information available for display even if the main content is missing, which is also a good operation practice.