How to determine if the `linkList` friendship link tag list is empty to avoid displaying an empty tag?

Calendar 👁️ 66

As an experienced website operations expert, I have accumulated rich experience in content management and template optimization on AnQiCMS.I deeply know that a smooth, beautiful, and fast-loading website cannot be separated from the meticulous attention to every detail.Today, let's delve deeply into a problem that is often overlooked in template development but is crucial for user experience and page cleanliness: how to judgelinkListWhen the friend link label list is empty, avoid displaying redundant empty tags.

Avoid displaying empty tags: Anqi CMSlinkListThe elegant way to handle friend link labels

Friendship links are an important way for websites to mutually refer traffic and improve SEO weight, they are usually presented in the form of a list at the footer or sidebar of a website. The powerful template system of AnQiCMS passes throughlinkListLabels that can easily call and display these links. However, in actual operation, friendship links may temporarily be empty due to various reasons (such as cooperative adjustments, data migration, or initial unconfiguration).If our template does not handle this situation properly, the page may also display an empty one even if there is no friend link data<ul>/<div>Or other wrapping HTML tags, this not only affects the page aesthetics but may also cause unnecessary DOM structure redundancy.

How can you elegantly solve this problem, making the friendship link area 'disappear quietly' when the list is empty? AnQiCMS provides several flexible and practical methods.

Analyze the problem: Why would empty tags appear?

When we uselinkListWhen obtaining friendship links, the following structure is usually adopted:

{% linkList friendLinks %}
    <div class="friend-links-section">
        <h3>友情链接</h3>
        <ul>
            {% for item in friendLinks %}
            <li><a href="{{item.Link}}" {% if item.Nofollow == 1 %} rel="nofollow"{% endif %} target="_blank">{{item.Title}}</a></li>
            {% endfor %}
        </ul>
    </div>
{% endlinkList %}

In this code block,{% for item in friendLinks %}The loop will only process actual existing friendship link data. IffriendLinksThe list is empty,forThe content inside the loop will not be rendered, but this does not mean that the external<div class="friend-links-section">/<h3>and<ul>The label will disappear as well. The browser will still parse and display these empty tags, leading to an empty title and list area on the page, which is clearly not what we want.

Solution one: clever useifConditional judgment

AnQiCMS template engine supports powerful conditional judgment syntax similar to Django.linkListThe label will assign the result to a variable after obtaining the data (for example, in the previous example, thefriendLinks). If no friendship links are configured, thisfriendLinksThe variable will be an empty array or slice. We can use this to check if the variable contains data before rendering any HTML structure.

{% linkList friendLinks %}
    {% if friendLinks %} {# 核心判断:如果friendLinks不为空,则渲染以下内容 #}
    <div class="friend-links-section">
        <h3>友情链接</h3>
        <ul>
            {% for item in friendLinks %}
            <li><a href="{{item.Link}}" {% if item.Nofollow == 1 %} rel="nofollow"{% endif %} target="_blank">{{item.Title}}</a></li>
            {% endfor %}
        </ul>
    </div>
    {% endif %} {# 结束条件判断 #}
{% endlinkList %}

By adding an outer layer,{% if friendLinks %}we ensure that only whenfriendLinksdata really exists in the variable, including<h3>the title and<ul>the entire friend link block, including the list, will be rendered. IffriendLinksIf it is empty, thenifThe code within the statement will be completely skipped, and no HTML tags related to the friendship link will appear on the page, thus achieving perfect 'invisibility'.

Solution two:for...emptyThe beauty of syntactic sugar

AnQiCMS template engine also provides a more concisefor...emptyA structure that is specifically used to handle the case of an empty circular list. This syntactic sugar not only avoids rendering an empty list, but also provides a friendly prompt when the list is empty.

{% linkList friendLinks %}
    <div class="friend-links-section">
        <h3>友情链接</h3>
        <ul>
            {% for item in friendLinks %} {# 循环友情链接项 #}
            <li><a href="{{item.Link}}" {% if item.Nofollow == 1 %} rel="nofollow"{% endif %} target="_blank">{{item.Title}}</a></li>
            {% empty %} {# 当friendLinks为空时,执行这里的内容 #}
            <li>暂无友情链接,敬请期待!</li>
            {% endfor %}
        </ul>
    </div>
{% endlinkList %}

This method compared to the pureifjudgment, is more focused on handling the empty state within the *list container*. It will retain<h3>the title and<ul>labels, but when the list is empty, it will be<ul>The system displays a prompt such as 'No friendly links available, please wait!' instead of an empty one.<ul>If your design requires that the title and the 'no link' prompt be retained when there is no link, thenfor...emptyis a very suitable choice.

If you want the entirefriend-links-sectionThe block does not display at all when there are no links, thenif friendLinksthe plan will be more direct. But if you just want to replace the list content,for...emptyit is more elegant and concise.

**Practice and Template Design Thoughts

In actual project development, the choice of method depends on your specific design requirements.

  • If both the list content and title need to disappear completely when there is no data.: Preferred{% if 变量名 %}...{% endif %}Wrap the entire content block.
  • If the list container (such as<ul>) and the title need to be retained, but a prompt text is displayed when the list items are empty:{% for item in 变量名 %}...{% empty %} 提示信息 {% endfor %}Would be a better choice.

No matter which way you choose, the core idea is to utilize the judgment ability of the AnQiCMS template engine to perform effective data validation before the data is passed to the template, thus avoiding the rendering of unnecessary HTML structures and maintaining the semantics and cleanliness of the page.This not only improves the website's loading efficiency, but also makes the user's visit experience smoother and more professional.


Frequently Asked Questions (FAQ)

Q1: Why did I use{% if friendLinks %}but still showed an emptydivorulTag?

A1:This is usually because yourifThe range of judgment is not wide enough. Make sure your{% if friendLinks %}condition judgment wraps all *HTML tags you want to hide when the friendship link is empty, including the outermostdivcontainer and titleh3as well asulIf the tagifonly wrappedulthendivandh3will still be rendered.

Q2:if friendLinksandfor...emptyWhat are the main differences between these two methods of handling empty lists? Which one should I choose?

A2:The main difference between them lies in the granularity of control.

  • if friendLinksUsed to determine whether the entire data set exists or contains content. IffriendLinksit is empty, it will prevent the entire friend link *block* from being rendered, including the title.
  • for...emptyIt is mainly used to handle *internal* loop content. IffriendLinksis empty, it will skipforthe rendering of loop data items, instead renderingemptythe prompt information inside the tag, but it will not hide the wrapped contentforThe external HTML container of the loop (such asulordiv). Choose based on your design goal: If you want the entire friend link area (including the title) to disappear when there are no links, chooseifIf you wish to retain the title and display prompts such as "No link".for...emptyit is more appropriate.

Q3: Besides the link tag, what other tags in AnQiCMS can be used in a similar way to determine if a list is empty?

A3:This pattern of judging whether the list is empty is universal in AnQiCMS templates, applicable to all tags returning array or slice type data. For example:

  • archiveList(Document list)
  • categoryList(Category List)
  • pageList(Single Page List)
  • navList(Navigation List)
  • tagList(Tag List)
  • commentList(Comment List) etc. Any tags whose variables may return empty data can be used.{% if 变量名 %}or{% for item in 变量名 %}{% empty %}Optimize template display.

Related articles

How to determine if the `navList` menu item has child navigation (`item.NavList`) and perform nested rendering?

## AnQi CMS navigation menu: skillfully use `navList` to implement multi-level nesting and child navigation judgment As an experienced website operations expert, I fully understand the importance of a clear and flexible navigation system for user experience and the overall architecture of the website.In AnQiCMS (AnQiCMS), the `navList` tag is the core tool we use to build a powerful navigation system.

2025-11-06

How to highlight the current page in the `navList` navigation menu based on the `IsCurrent` property?

In the fast-paced digital world, a clear and intuitive website navigation menu is the foundation of a good user experience.As an experienced website operations expert, I am well aware of AnQiCMS's powerful capabilities in providing efficient and customizable content management solutions.It not only performs excellently but also considers the operator's needs in detail, for example, the built-in `navList` tag combined with the `IsCurrent` attribute can easily highlight the current page, greatly enhancing the professionalism and user-friendliness of the website.

2025-11-06

How to judge whether a contact field (such as Qrcode) in the AnQiCMS template has a value?

In website operation and template development, we often need to dynamically adjust the display of the front-end page based on the setting of the background content.Especially for important information such as contact information, ensure that it is displayed after it exists, which can effectively avoid broken links, blank areas, or incomplete information on the page, thereby improving the user experience.Today, let's delve into how to elegantly and accurately determine whether a contact field (such as `Qrcode`) obtained by the `contact` tag in the AnQiCMS template has been set.

2025-11-06

How to judge whether the website is closed and display a prompt based on the `SiteCloseTips` of the system settings (`{% system %}`)?

As an experienced website operations expert, I know that the stable operation of the website is of great importance, but sometimes, due to reasons such as maintenance, upgrades, or data migration, the website has to temporarily shut its doors.How to communicate with visitors elegantly during a closure period, ensuring user experience while effectively managing expectations, is a detail that every operator needs to pay attention to.AnQiCMS (AnQiCMS) provides a flexible mechanism in this regard, allowing us to deal with it easily.Today, let's delve deeply into how to set up tags in Anqi CMS through system settings `{% system

2025-11-06

How to conditionally output the standard link according to the `CanonicalUrl` tag in AnQiCMS templates?

Hello! As an experienced website operations expert, I fully understand that in daily work, we not only need to pay attention to the quality of the website content, but also to the impact of technical details on SEO.The canonical URL is one of the crucial details that can effectively solve the problem of duplicate website content, concentrate page authority, and thus improve search engine rankings.Today, let's delve deeply into how to intelligently and elegantly output standard links based on the existence or absence of the `CanonicalUrl` field within the `tdk` tag in AnQiCMS templates

2025-11-06

In the `pagination` tag, how to determine if the current page is the first or last page to control the style?

As an experienced website operations expert, I know the importance of a powerful and flexible content management system for the success of a website.AnQiCMS leverages the efficiency of the Go language and its deep consideration for content operation, providing us with many conveniences.Today, let's delve into the wonderful use of the `pagination` tag in AnQiCMS, especially how to accurately judge whether the current page is the first page or the last page, thereby flexibly controlling the style to create a smoother and more intuitive navigation experience for users.

2025-11-06

How to conditionally display the 'Previous' and 'Next' buttons in the `pagination`?

As an experienced website operation expert, I am willing to analyze the conditional display techniques of the pagination navigation buttons in AnQi CMS in detail.AnQi CMS is known for its efficiency and flexibility, its template engine provides powerful functions, allowing us to easily achieve both beautiful and practical page interactions.In website content management, pagination is an indispensable element, and how to intelligently display the "Previous Page" and "Next Page" buttons is directly related to the smoothness of the user's browsing experience.### SecureCMS

2025-11-06

How to judge whether the comment (`Status`) in the AnQiCMS template has passed the review and be displayed differently?

As an experienced website operation expert, I know the importance of the comment system for website user interaction and content ecology.In such a powerful content management system as AnQiCMS, flexibly controlling the display of comments, especially the differentiated display based on the review status, is a key factor in improving user experience and maintaining the quality of website content.Today, let's delve into how to use the `commentList` tag in the AnQiCMS template to judge the `Status` field of comments and implement intelligent differentiated display.

2025-11-06