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