安企CMS, as an enterprise-level content management system built with 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 and intelligent websites, we often need to flexibly adjust the display of the page according to the presence or absence of content.tag-tagDataListThe label to determine whether the document list under the specified label is empty.
In AnQi CMS, tags (Tag) are a powerful tool for organizing content, enhancing user experience, and SEO.A user clicks on a tag and usually expects to see a series of related articles or products.However, imagine that if there are no documents under a certain tag at the moment, and the user clicks to enter, 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 tag is empty when rendering the front-end page, and to adopt different display strategies accordingly.
Deep understandingtag-tagDataListtags
First, let's briefly review.tag-tagDataListThe role of tags. As the name suggests, it is specifically used to retrieve and output the document list under a specific tag from the database. You can specify the tag ID as needed.tagId)、内容模型ID (moduleId) of sorting method (order) of display quantity (limit) and list type (type) and other parameters, so as to accurately obtain the dataset you want.
For example, if you want to get the list of all articles under the tag with 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 internalforloop is responsible for traversing and displaying each document one by one. However, whenarchivesis an empty set, the code snippet mentioned above containsforThe loop will not execute, and nothing will be displayed on the page, which is exactly the 'blank canvas' situation we hope to avoid.
The core method for elegantly determining if 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 where the list is empty, that is, to introduce in the loopforintroducing in the loop{% empty %}Clause.
This{% empty %}A clause is likeforan alternate or backup option for the loop. Whenforthe set to be traversed by the loop tries to iterate (for example, here thearchives) discovers it is empty, or there are no elements that can be traversed at all,foris the content of the loop body (i.e.),{% for ... %}and{% empty %}part between them) will be skipped,{% empty %}and{% endfor %}The content between the brackets will be executed and rendered.
Let's see how to apply this mechanism totag-tagDataListTags inside:
{% 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 additionalifCondition judgment or manual check of list length, the template engine will automatically handle the logic for you. IftagIdThe page will display a list of documents under the "10
The practical application scenario and content operation suggestions
The ability to determine whether a list is empty has multi-faceted value in content operation:
- Improve user experienceAvoid users 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.
- Optimize SEO strategyFor pages that may contain empty lists, if the blank content is presented directly, search engines may consider it a low-quality or 'thin content' page, which may affect indexing and ranking.
{% empty %}The block, you can fill in meaningful prompts, even guide search engines to capture other related content (such as redirecting to a more comprehensive category page through 301 redirection, or placing links to popular articles in the prompts), thereby maintaining the overall SEO health of the website. - Dynamic content display:You can decide whether to render certain page elements based on whether the list is empty. For example, if there are documents under a certain tag, display the