How to determine if the `linkList` friendship link list returns an empty result and decide whether to display the friendship link block?

Calendar 👁️ 54

In website operation, friendship links are an important link to enhance website authority and promote SEO, and can also provide users with more valuable external resources.However, during the website construction process, friendship links are not always full.If the friend link list is empty, but still displays an empty link area on the page, it will undoubtedly affect the overall aesthetics and professionalism of the website.

AnQiCMS provides flexible template tags, allowing us to intelligently determine whether the friend link list is empty and decide whether to display the corresponding block according to the actual situation.Next, we will discuss how to implement this feature in your AnQiCMS template.

How to get the friend link list in AnQiCMS

Firstly, we need to obtain the data of the friend link in the template. In AnQiCMS, it is very simple to obtain the list of friend links, we just need to use the built-inlinkListThe label. It retrieves all the configured friend links from the background and assigns them to a variable defined by us, such asfriendLinks.

Basic usage is as follows:

{% linkList friendLinks %}
    {# 此时,friendLinks变量就包含了所有友情链接信息的数组对象 #}
    {# ...这里是处理friendLinks的代码... #}
{% endlinkList %}

OncefriendLinksThe variable is assigned, and we can use it to determine whether the list has content.

How to check if the list is empty: two practical methods

According to how you want to handle the display effect when the list is empty, AnQiCMS template engine provides two practical and flexible methods to judge and control the display of the link area.

Method one: utilize{% if ... %}The label wraps the entire block

This is the most intuitive and concise way to judge. You can use{% if ... %}condition labels to checkfriendLinksDoes the variable contain any data. In AnQiCMS template logic, a non-empty array or list is considered true (true), while an empty list is considered false (false).

This method is suitable when you want the entire friend link block (including the title, border, etc.) not to be displayed when there are no friend links.

{% linkList friendLinks %}
    {% if friendLinks %} {# 判断friendLinks是否非空 #}
        <div class="friendship-links-block">
            <h3>友情链接</h3>
            <ul class="link-list">
                {% 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 %}

In the above code, only whenfriendLinksWhen the variable indeed exists friendship links, the whole<div class="friendship-links-block">The content inside will be rendered to the page. In this way, if your website does not have any friend links, this area will not occupy any page space, keeping the page neat.

Method two: use{% for ... empty ... endfor %}Loop structure is displayed within the block

AnQiCMS template engine provides a more elegant, more suitable loop structure for displaying prompts when the list is empty:{% for ... empty ... endfor %}.

This structure is traversedfriendLinksWhen traversing the array, if there is content in the array, it will be executedforThe code within the block will be executed one by one to display the friendship links; if the array is empty, it will executeemptyBlock code, at this time you can display a prompt message instead of leaving it blank.

This method is suitable for when you want the title and border of the friendship link block to always be displayed, but when there are no specific links, a prompt such as "Currently no friendship links" is displayed in the list position.

{% linkList friendLinks %}
    <div class="friendship-links-block">
        <h3>友情链接</h3>
        <ul class="link-list">
            {% 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 class="no-links-message">目前暂无友情链接,敬请期待。</li>
            {% endfor %}
        </ul>
    </div>
{% endlinkList %}

In this way, even without a friendship link, users can see a friendly prompt instead of an empty area that might cause confusion.

Considerations in practical applications.

Choose which method depends on the design requirements and user experience goals of your website.

  • If your goal is to hide completely.That is, when there are no links, do not display any elements related to the friendship link, thenMethod one (using{% if friendLinks %}Wrap the entire blockis your first choice.
  • If you want the friendship link block to always be visibleBut the content varies depending on whether there is a link (the link is displayed if there is no link, the prompt is displayed otherwise), thenMethod two (using{% for ... empty ... endfor %}structure)It is more appropriate.

It is recommended to organize the relevant HTML and AnQiCMS template logic in a separate template fragment file (such aspartial/friendship_links.html), and then referenced by{% include "partial/friendship_links.html" %}Introduce at the required display location, which can improve the reusability and maintainability of the template.

Summary

By reasonably utilizing the AnQiCMS provided{% if %}Conditional tags and{% for ... empty ... endfor %}Loop structure, we can easily control the display logic of the friend link block.This can not only keep your website's interface dynamic when the data changes, but also enhance the user's browsing experience, showing the professionalism and detail of the website.Hope these tips can help you better operate the website built with AnQiCMS.


Frequently Asked Questions (FAQ)

1.linkListin the labelfriendLinksIs the variable name fixed?

It is not fixed.{% linkList friendLinks %}offriendLinksjust

Related articles

How to dynamically display the character length of the website name `SiteName`, such as for SEO title preview?

In content operation and website SEO optimization, the title plays a crucial role.A well-constructed SEO title can not only attract the attention of users, but also effectively improve the visibility of the page in the search engine results page (SERP).Among them, the character length of the title is a detail that should not be ignored.Title length can cause it to be truncated, affecting information delivery; too short may not fully utilize the display space, missing the opportunity to attract users.AnQiCMS (AnQiCMS) is a powerful content management system that provides a rich set of tools for SEO optimization

2025-11-08

How to get the total length of the document list `archives` being traversed in the `for` loop?

In AnQiCMS template development, we often need to traverse document lists, such as through the `archiveList` tag content.In such a `for` loop, understanding the total length of the current list is a very practical need, as it can help us implement some specific display logic, such as showing 'Article N of M' or determining if the list is empty.The AnQi CMS template engine adopts Django's syntax, providing a set of intuitive and powerful tags and filters to process data.To get `for`

2025-11-08

How to judge if the length of the user comment content `comment.Content` exceeds the limit for front-end verification?

In website operation, the comment feature is an important part of user interaction.To maintain a good community environment and data quality, we usually limit the length of the comments submitted by users.Front-end validation plays a crucial role in this process, providing immediate feedback before submission to avoid failure due to content length, thereby enhancing user experience and reducing server load.How can we judge whether the length of the user comment content `comment.Content` exceeds the limit in the website built using AnQiCMS?

2025-11-08

How to get the number of top-level menu items in the website navigation list `navList`?

In website content operation, the navigation menu is the first door for users to interact with the website content.AnQiCMS (AnQiCMS) provides a flexible `navList` tag to help us easily manage and display website navigation.Sometimes, in order to achieve a specific layout, style, or dynamic adjustment, we need to know the specific number of top-level navigation menu items on the website.This article will thoroughly introduce how to obtain the number of top-level menu items in the `navList` navigation list of AnQiCMS templates.### Understand `navList`

2025-11-08

How to get the total number of categories returned by `categoryList` to control the page layout or display the 'All' option?

In Anqi CMS template development, the `categoryList` tag is one of the core tools for website content organization and navigation construction.It can help us easily display the classification structure of articles, products, and other content.However, in practical applications, we may not only need to display the categories themselves, but also need to flexibly adjust the page layout based on the number of categories, or decide whether to display an 'All' option so that users can browse the content under all categories.How do we use `categoryList`

2025-11-08

How does AnQiCMS manage the display of content for different brands or sub-sites uniformly?

In today's complex network environment, many enterprises or individuals need to manage multiple brand sites, product line special theme sites, or regional sub-sites.How to efficiently and uniformly manage the massive content of these sites to ensure that each site has a unique brand image and content display, which has become a major challenge for operators.AnQiCMS is a powerful content management system dedicated to solving this problem, it makes it easy and convenient to manage the unified content display of multiple brands or sub-sites through its unique design concept and rich features.### Unified Management

2025-11-08

How to customize the content structure according to business requirements and display it on the front end?

In the daily operation of websites, we often encounter such needs: standard content structures, such as articles and products, cannot fully meet the unique display requirements of our business.It may be necessary to add 'author source' and 'publishing organization' fields for a specific type of information, or to add 'size' and 'material' and other personalized attributes for a product series.AnQiCMS (AnQiCMS) provides high flexibility in content management, allowing us to customize the content structure according to actual business scenarios and display it accurately on the front end. Next

2025-11-08

How does AnQiCMS support the switching of multilingual content on the page?

In today's globalized digital world, website multilingual support has become a key step for enterprises and content operators to expand into international markets and serve diverse user groups.AnQiCMS as an efficient enterprise-level content management system, fully considers this requirement, and provides a flexible and practical multi-language content switching display solution.AnQiCMS' multi-language strategy lies in its powerful multi-site management function and the flexible calling mechanism of the front-end template.It does not simply provide content automatic translation, but encourages providing carefully planned, independently maintained localized content for users of different languages

2025-11-08