In website operation, friendship links are an important part of improving website weight and promoting SEO, and they can also provide users with more valuable external resources.However, friendship links are not always full in the process of website construction.If the friend link list is empty but an empty link area is still displayed on the page, it will undoubtedly affect the overall aesthetics and professionalism of the website.

AnQiCMS (AnQiCMS) provides flexible template tags, allowing us to intelligently determine whether the friendship 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 of AnQiCMS

Firstly, we need to retrieve the data of the friend link in the template. In AnQiCMS, it is very simple to get the list of friend links, we just need to use the built-inlinkListTags. It will fetch all the configured friendship links from the background and assign them to a variable we define, such asfriendLinks.

Basic usage is as follows:

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

OncefriendLinksThe variable is assigned, and we can use it to determine if there is content in the list.

Two practical methods to determine if the list is empty.

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 friendship link block.

Method one: use{% if ... %}Label wraps the entire block

This is the most intuitive and concise way to judge. You can{% if ... %}check with the condition labelfriendLinksVariable contains 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 for the situation where you want the entire friendship link block (including the title, border, etc.) not to be displayed when there are no friendship 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 %}

Only when the above code,friendLinksWhen the friendship link indeed exists in the variable,<div class="friendship-links-block">The content inside will be rendered to the page. This way, your website will not occupy any page space when there are no friend links, keeping the page tidy.

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 executeforThe code in the block, and display the friendship link one by one; if the array is empty, it will executeemptyBlock-level code, at this point 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 like “Currently no friendship links” is displayed at 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 %}

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

Considerations in practical application

The choice of method depends on the design requirements and user experience goals of your website.

  • If your goal is to hide completely,that is, when there is no link, do not display any elements related to friend linksMethod one (using{% if friendLinks %}wrap the entire block)it is your best choice.
  • If you want the friend link block to always be visible,but the content varies depending on whether there is a link (display the link if there is a link, display the prompt if there is no link), thenMethod two (using{% for ... empty ... endfor %}structure)is more appropriate.

No matter which method you choose, it is recommended to organize the relevant HTML and AnQiCMS template logic into a separate template fragment file (for example,partial/friendship_links.html), and then through{% include "partial/friendship_links.html" %}Introduce it at the required display position, which can improve the reusability and maintainability of the template.

Summary

By reasonably utilizing the AnQiCMS provided{% if %}condition tags and{% for ... empty ... endfor %}Loop structure, we can easily control the display logic of the友情链接区块.This can not only keep your website dynamically adaptable to changes in data, but also enhance the browsing experience of users, showcasing the professionalism and meticulousness of the website.These tips are hoped to help you better operate the website built with AnQiCMS.


Common Questions (FAQ)

1.linkListthe tag infriendLinksIs the variable name fixed?

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