Does the `bannerList` tag support random display of an item from the Banner list?

Calendar 👁️ 64

In modern website operations, Banner (banner ad) as a visual focus, is crucial for attracting user attention and conveying core information.Elegantly manage and display banners, especially when it is necessary to bring users a sense of novelty, random display has become a highly sought-after need.Today, as an experienced website operation expert, I will take everyone deep into the exploration of AnQiCMSbannerListThe function of the tag, and answer a common question: bannerListDoes the tag support random display of an item from the Banner list?

Unveiling AnQiCMS'sbannerListLabel: Its core responsibility

First, let's understand what AnQiCMS providesbannerListWhat are tags used for? According to the design philosophy of AnQiCMS, its template tags are designed to provide powerful and flexible data calling capabilities.bannerListThe core function of the tag, as the name implies, is used forGet the Banner list data under the home page of the website or a specific group.

When you use{% bannerList banners %}When this syntax is used, AnQiCMS will extract the corresponding Banner data from the background configuration and encapsulate it into an object namedbannersThe array object is used for front-end template. This tag also supports some parameters, such assiteId(used for multi-site data calls) andtype(Used to retrieve the Banner of a specific group). For example, if you create a Banner group named "幻灯" in the background, you can access it through{% bannerList banners with type="幻灯" %}Call all the Banners under this group accurately.

It is important that the document clearly states.bannersIs an "array object", which means it returns a collection of Banners, not a single Banner. Therefore, in order to display these Banners on the front end, we usually cooperate withforLoop to traverse this array, output the picture, link, title information of each Banner one by one, like this:

{% bannerList banners %}
    {% for item in banners %}
    <a href="{{item.Link}}" target="_blank">
        <img src="{{item.Logo}}" alt="{{item.Alt}}" />
        <h5>{{item.Title}}</h5>
    </a>
    {% endfor %}
{% endbannerList %}

From here we can see,bannerListThe main design goal of the label itself is to provide a Banner data collection, and it does not have a built-in feature to directly select an item randomly.

Explore the implementation path of random display

Then, let's go back to our original question:bannerListDoes the tag support random display of an item in the Banner list?

The answer is:bannerListTagDoes not support directlyRandomly display an item because it returns a complete list.However, AnQiCMS's powerful template engine provides a variety of filters and auxiliary tags, and we can cleverly combine these features to achieve the need for random display.

The core of implementing this feature lies in the AnQiCMS template engine providedrandomFilter. This filter can randomly select and return an element from a string or array. SincebannerListThe tag has provided us with a Banner array, we can use thisbannersarray asrandomThe input of the filter, thus easily achieving the purpose of randomly selecting one Banner.

Practice: How to randomly display a Banner

Now, let's look at how to implement the random display of banners in AnQiCMS through specific code examples.

Firstly, we need to use as usual.bannerListLabel retrieves all Banner data and stores it in a variable. Then, we use this variable as input, applyingrandomThe filter is used to select a random Banner.

{% bannerList allBanners %} {# 首先,获取所有Banner数据并存储到allBanners变量中 #}

{% if allBanners %} {# 确保allBanners不为空,避免运行时错误 #}
    {% set randomBanner = allBanners|random %} {# 使用random过滤器从allBanners中随机选择一个Banner #}

    {# 现在,我们可以像访问普通Banner item一样,访问randomBanner的各个属性 #}
    <div class="hero-banner">
        <a href="{{ randomBanner.Link }}" target="_blank" title="{{ randomBanner.Alt }}">
            <img src="{{ randomBanner.Logo }}" alt="{{ randomBanner.Alt }}" />
            <h4>{{ randomBanner.Title }}</h4>
            <p>{{ randomBanner.Description }}</p>
        </a>
    </div>
{% else %}
    <p>暂无Banner可显示。</p>
{% endif %}

By the above code, AnQiCMS will first callbannerListGet all available Banners and then userandomThe filter randomly selects a Banner from this set and assigns it torandomBannerthe variable. Finally, we can display it like we would for a single Banner data.randomBannerThe image, link, and other information was provided. This method fully utilizesbannerListthe data acquisition capabilities, and also takes advantage of the flexibility of the template engine to achieve customized requirements.

Why choose such an implementation method?

One of the design philosophies of AnQiCMS is to provide a content management solution that is 'efficient, customizable, and easily scalable'.bannerListLabels focus on obtaining data lists, leaving the flexibility of data processing and display to the filters and logical judgments of the template engine. The benefits of this modular design include:

  1. Label function is clear:Each label has a clear responsibility,bannerListResponsible only for “data collection”, avoiding the label function from being too bloated.
  2. Template is highly flexible:Operators and developers can utilize rich filters and logical statements (such asif/for/setAnd various filters) to process the obtained data twice, realizing infinite possibilities. This is more flexible than embedding all possible display logic in each tag.
  3. Performance optimization:IfbannerListThe tag itself has a random function, which may increase the complexity of its internal logic, and placing the random logic in the front-end template layer can better utilize the optimization mechanism of the template engine.

In conclusion, althoughbannerListThere is no direct "random" parameter for the label, but the AnQiCMS template engine provides a "combo" solution. BybannerListgathering data, then coordinatingrandomThe filter randomly selects, thus elegantly achieving the need to randomly display a single Banner, bringing dynamic and fresh user experience to the website.


Frequently Asked Questions (FAQ)

  1. bannerListDoes the tag support displaying a Banner based on a group?Of course, it does.bannerListTags providedtypeParameters, you can go throughtype="你的分组名称"Specify precisely which group's Banner you want to call. For example:{% bannerList banners with type="最新活动" %}This allows you to display different banner sets based on different areas or event themes.

  2. How to ensure that the Banner image displays well on different devices?AnQiCMS offers powerful image processing features in the content settings, such as enabling Webp image format conversion, automatically compressing large images, and various thumbnail processing methods (such as proportional scaling based on the longest edge, trimming based on the shortest edge, etc.). In the template, you can directly call the Banner item'sLogoorThumbfields, they usually automatically point to optimized image resources. In addition, you can also combinethumbThe filter (if the image URL needs additional processing to generate a specific thumbnail size) to ensure image loading speed and display quality.For example: `<img src=“{{ item.Logo

Related articles

How to integrate third-party ad code in the home page Banner?

As an experienced website operations expert, I know that how to efficiently use website space for monetization and promotion is a focus for many enterprises and content operators in an increasingly competitive online environment.AnQiCMS is an efficient and flexible content management system, its powerful customizability provides a solid foundation for us to achieve these operational goals.Today, let's delve into a common operation requirement: how to integrate third-party advertising code into the AnQiCMS homepage banner.

2025-11-06

The `bannerList` tag provides additional filters to handle the images or text of the Banner?

As an experienced website operation expert, I am happy to delve into the function of the `bannerList` tag in Anqi CMS and answer questions about its filters.When building a visually appealing and content-effective website, the Banner (banner ad or carousel) is undoubtedly the key element in attracting users' attention.Safe CMS provides a convenient `bannerList` tag, but its functionality is far more than just simple content calls.--- ### Flexible use of the `bannerList` tag in AnQi CMS

2025-11-06

How can I use the `type` parameter to display multiple different styled Banner areas on the homepage?

As an expert in website operation who is well-versed in AnQi CMS, I fully understand your need to display a diversified Banner area on the homepage.A vibrant and rich homepage is often the key to attracting users and enhancing brand image.AnQi CMS is born to meet such needs with its powerful flexibility and customizability.Today, let's delve into how to巧妙运用 `type` parameter skillfully in Anqi CMS to achieve diversified display of the home page Banner.### Display of various banners on the AnQi CMS homepage

2025-11-06

How to ensure the compatibility of the home page Banner across different browsers?

In the world of website operation, the homepage banner acts as the "facade" of the website, and its display effect directly affects the first impression and conversion rate of users.However, due to the variety of browsers used by users and the rapid iteration of versions, how to ensure that this crucial Banner maintains a consistent and excellent visual experience on all browsers has become a challenge for many website operators.As an experienced website operator who has been deeply involved with AnQiCMS for many years, I am well aware of AnQiCMS' strong ability to solve such problems

2025-11-06

How to add a mouse hover effect to the homepage Banner?

As an experienced website operation expert, I fully understand your pursuit of website visual effects and user experience.The home page banner acts as the "face" of the website, and its design and interaction directly affect the first impression of visitors.Today, let's delve into how to add an attractive mouse hover effect to your homepage banner in AnQiCMS, making your website more vibrant.### Ingenious Use of CSS

2025-11-06

The `bannerList` tag affects the page loading speed when fetching Banner data?

As an experienced website operations expert, I fully understand your concern about website loading speed.In today's internet environment where user experience is paramount, even milliseconds of delay can affect user retention and conversion.AnQiCMS (AnQiCMS) is a modern content management system developed based on the Go language, which has always put high performance and high concurrency as the core goal from the very beginning.Therefore, when you ask whether the "bannerList" tag affects the page loading speed when fetching Banner data?When this question is asked

2025-11-06

How to set the display priority or weight of the Banner in the Anqi CMS backend?

As a senior operation expert of Anqi CMS, I am well aware of the crucial role of website banners in attracting user attention and conveying core information.The display order of the banner, which seems like a minor detail, actually directly affects the effective delivery of marketing content and user experience.In Anqi CMS, although there is no direct numeric input box labeled "priority" or "weight" to control the arrangement of banners, the system provides a set of intuitive and flexible management methods that allow you to easily control the display order of banners.

2025-11-06

Does the `bannerList` tag support displaying different Banners based on user permissions?

In website operation, how to provide customized content experience according to different user groups has always been the key to improving user stickiness and conversion rate.For Banner elements with strong visual impact and direct information delivery, the need for differentiated display is particularly common.As an experienced user and operations expert of AnQiCMS, I will discuss whether the `bannerList` tag supports displaying different Banners based on user permissions?This topic, combined with the functional features of AnQiCMS, is deeply analyzed for everyone.### One

2025-11-06