AnQi CMS, as an enterprise-level content management system built on Go language, has always been committed to providing users with efficient, customizable, and secure content operation solutions.In our daily content management work, especially when building dynamic, intelligent websites, we often need to flexibly adjust the display of the page according to the presence or absence of content.Today, let's delve deeply into a very practical and elegant feature of Anqi CMS - how to utilizetag-tagDataListLabel to determine if the document list under a specified label is empty.

In Anqi CMS, tags are a powerful tool for organizing content, enhancing user experience, and improving SEO.The user clicks on a tag and usually expects to see a series of related articles or products.However, imagine if there is no document under a certain tag, and when the user clicks in, they find an empty page, which undoubtedly affects their browsing experience, and may even cause search engines to think that your website has a "sparse content" issue.Therefore, it is particularly important to accurately judge whether the document list under a certain label is empty when rendering on the front-end page, and to adopt different display strategies accordingly.

Deep understandingtag-tagDataListTag

First, let's briefly review.tag-tagDataListThe role of the tag. As the name implies, it is specifically used to retrieve and output the document list belonging to a specific tag from the database. You can specify the tag ID as needed (tagId) Content model ID (moduleId) Sorting method (order) Display quantity (limit) and list type (type) parameters to accurately obtain the dataset you want.

For example, if you want to get the list of all articles under the Tag ID 10, you might use it like this:

{% tagDataList archives with tagId="10" type="page" limit="10" %}
    {% for item in archives %}
        <div class="document-item">
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>{{item.Description|truncatechars:100}}</p>
            <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
        </div>
    {% endfor %}
{% endtagDataList %}

Here, archivesThe variable holds all the document data that meets the conditions, while the internalforThe loop is responsible for traversing and displaying each document one by one. However, whenarchivesis an empty set, the code snippet mentioned above isforThe loop will not execute, nothing will be displayed on the page either, which is the 'blank canvas' situation we hope to avoid.

The core method for elegantly determining whether a list is empty:{% for...empty...endfor %}

The template engine design of AnQi CMS is very thoughtful, it provides us with an extremely elegant and intuitive solution to handle the case of an empty list, that is, inforloop.{% empty %}clauses.

This{% empty %}a clause is likeforThe "backup" or "alternative". WhenforThe set being traversed in a loop (for example, thisarchives) is found to be empty, or there are no elements to traverse at all.forThe main content of the loop (i.e.,{% for ... %}and{% empty %}the part between) will be skipped, while{% empty %}and{% endfor %}the content between will be executed and rendered.

Let's see how to apply this mechanism totag-tagDataListtags:

{% tagDataList archives with tagId="10" type="page" limit="10" %}
    {% for item in archives %}
        <div class="document-item">
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>{{item.Description|truncatechars:100}}</p>
            <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
        </div>
    {% empty %}
        <div class="empty-state-message">
            <p>很抱歉,当前标签下暂时没有找到任何文档。</p>
            <p>您可以尝试访问其他热门标签或使用站内搜索功能。</p>
            <!-- 您可以在这里添加更多引导内容,例如热门推荐文章 -->
        </div>
    {% endfor %}
{% endtagDataList %}

In this way, we do not need to write extraifCondition judgment or manually check the length of the list, the template engine will automatically handle the logic for you. IftagIdUnder the tag 10, there are documents, the page will display a list of documents; if there are no documents, the page will display a custom "No documents" prompt, greatly enhancing the smoothness and friendliness of the user experience.

Actual application scenarios and content operation suggestions

The ability to judge whether a list is empty has many values in content operation:

  1. Improve user experienceAvoid users from feeling confused or frustrated when facing a blank page. Clear prompts can guide users to the next step, such as returning, searching, or browsing other recommended content.
  2. Optimize SEO strategyFor pages that may have empty lists, if blank content is displayed directly, search engines may consider it a low-quality or 'sparse content' page, which may affect inclusion and ranking. By{% empty %}Block, you can fill in meaningful prompts, even guide search engines to capture other relevant content (such as through 301 redirects to more comprehensive category pages, or placing links to popular articles in the prompts), thereby maintaining the overall SEO health of the website.
  3. Dynamic content display: You can decide whether to render some page elements based on whether the list is empty. For example, if there are documents under a tag, display that