How to display the background custom banner (Banner) image list in the template?

Calendar 👁️ 77

The banner image on the website is an important element to attract visitors' attention and display core information.In AnQi CMS, you can flexibly customize these banner images in the background and elegantly display them in various template positions on the website.Below, we will discuss in detail how to display the custom banner image list in the template, making your website more attractive.

Configuration and management of background banner images

In AnQi CMS backend, banner image management is mainly divided into two cases: global homepage banners and specific content banners.

  1. Global homepage banner (Banner) managementThis usually refers to the carousel or important promotional images displayed on the homepage or other public areas of a website.You can find a dedicated 'Banner Management' module or similar in the background to configure.Here, you can upload multiple images, set link addresses for each image, image descriptions (for SEO and auxiliary descriptions), and specify the "group" they belong to.By using the grouping feature, you can categorize banners of different purposes, such as "home page large image", "sidebar advertisement", and so on, which provides a foundation for the flexible calling of front-end templates.

  2. Banner management for specific content (categories, single pages)Anqi CMS also provides the function to set exclusive banners for specific content such as article categories, product categories, or independent single pages.

    • When you edit aCategoryAt the time, there will be an option for the 'Banner image' in the category editing interface.You can upload a set or multiple images here for this category, which will only be displayed on the pages of this category and its subcategories.
    • Similarly, when editingsingle pageThere is also a "Banner Image" setting option.You can set unique banner images for independent pages such as "About Us", "Contact Information", and so on.When uploading these specific banners, please pay attention to the consistency of the image size to ensure a coordinated and aesthetically pleasing visual effect on the frontend.

Show the global banner list in the template

To display the global banner list you have set up in the background, AnQi CMS provides a very convenient tag:bannerList.

This tag's usage is very intuitive. You can use it to get a collection of banner images, and then display them on the page by iterating through them.

The basic call example is as follows:

{% 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 %}

In the code above:

  • {% bannerList banners %}Defined a namedbannersStore the retrieved banner list in a variable.
  • {% for item in banners %}Loop through this list, and in each iteration, the data of the current banner will be assigned toitemVariable.
  • itemThe variable contains various information about the banner, such as:
    • {{item.Logo}}The URL address of the banner image.
    • {{item.Link}}The link address where the banner jumps after clicking.
    • {{item.Alt}}The Alt attribute of the image, which is very important for SEO and accessibility.
    • {{item.Title}}The banner title, which can be used to display image descriptions or text overlay.
  • target="_blank"Make sure to open the link in a new window to improve user experience.

Call banners by group

If you have set different "groups" for banners in the background, you can use them totypeParameters to accurately call banners of specific groups. For example, if you create a group named "Home Carousel", you can call it like this:

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

Such banners belonging to the 'Homepage Carousel' group will be loaded and displayed in the template.

Special style processing: for example, the first banner

In some carousels, you may want the first banner to have a special style (such as 'active' state). You can useforin the loopforloop.Counterattribute to achieve this effect:

{% bannerList banners with type="首页轮播" %}
    {% for item in banners %}
    <a class="{% if forloop.Counter == 1 %}active{% endif %}"  href="{{item.Link}}" target="_blank">
        <img src="{{item.Logo}}" alt="{{item.Alt}}" />
        <h5>{{item.Title}}</h5>
    </a>
    {% endfor %}
{% endbannerList %}

Whenforloop.CounterWhen equal to 1, it indicates that the current element is the first element in the loop, add it foractiveclass, convenient for defining its style through CSS.

Display a banner list of specific content in the template (category/page)

For the exclusive banners you set in categories or single pages, the calling method is slightly different. These banner images are usually attributes of the content itself, so they need to be usedcategoryDetailorpageDetailtags to obtain.

Whether it is a category or a single page, they both have aImagesfield used to store multiple banner images uploaded, this field returns an array of image URLs.

Call the category banner image:

{% categoryDetail categoryImages with name="Images" %}
{% if categoryImages %} {# 判断是否有图片上传 #}
<ul>
{% for item in categoryImages %}
  <li>
    <img src="{{item}}" alt="{% categoryDetail with name="Title" %}" />
  </li>
{% endfor %}
</ul>
{% endif %}

here,categoryDetailTag to get the details of the current category and pass through:name="Images"Get the banner image array. Then, we pass through theforLoop throughcategoryImagesarray and display each image.

Call single page banner image:

{% pageDetail pageImages with name="Images" %}
{% if pageImages %} {# 判断是否有图片上传 #}
<ul>
{% for item in pageImages %}
  <li>
    <img src="{{item}}" alt="{% pageDetail with name="Title" %}" />
  </li>
{% endfor %}
</ul>
{% endif %}

Its logic is completely consistent with the category banner call, but justcategoryDetailReplacepageDetail.

Only display the first banner or as a background image

Sometimes, you may only need to display the first image in a specific banner or use it as the background image of the page. You can combineifsentences and array indices to achieve this:

{% pageDetail bannerImages with name="Images" %}
{% if bannerImages %}
  {% set pageBanner = bannerImages[0] %} {# 获取数组中的第一张图片URL #}
  <div class="page-banner" style="background-image: url('{{pageBanner}}'); background-size: cover; background-position: center;">
    {# 这里可以放置其他页面内容,例如页面标题等 #}
  </div>
{% endif %}

This code retrieves the first banner image on a single page and sets it as a background image for an element, achieving a beautiful visual effect.divElement background image, achieving a beautiful visual effect.

Practical Tips

  • Image optimization:No matter which banner, large images will seriously affect the website loading speed.Before uploading an image, it is recommended to compress it and consider using modern image formats like WebP.The Anqi CMS backend content settings also provide the function of automatically compressing large images, which can save some manual compression steps after turning it on.
  • Alt attribute: for<img>Add tags with meaningful contentaltThe attribute is very important. This not only helps search engines understand the content of the image but also provides text alternatives when the image cannot be loaded, improving accessibility.
  • Responsive DesignEnsure your CSS styles allow banner images to display well on various devices (PC, tablet, mobile), for example, usingmax-width: 100%; height: auto;etc. style rules.

By the above method, you can easily call and display the custom banner image list on the Anqi CMS template, adding vitality and liveliness to your website.


Frequently Asked Questions (FAQ)

Q1: I uploaded a banner image on the backend, but why isn't it showing in the front-end template?A1: First, please check if you are using the correct template tags (bannerListorcategoryDetail/pageDetailCombineImagesField). Secondly, if usedbannerListand specifiedtypeParameters, please ensure that the "Group Name" in the background banner matches the template intypeThe value matches completely. Finally, check if the image has been successfully uploaded and the image address is valid, and then try again after clearing the browser cache or the system cache of the CMS.

Q2: How to display only the first image in the category or single page banner list?A2: When throughcategoryDetailorpageDetailTag to get toImagesWhen a field is an array of image URLs. You can access the first image by using the array index[0]For example,{% set firstBanner = categoryImages[0] %}You can get the URL of the first image. It is best to add one before using it.{% if categoryImages %}Judgment, to avoid errors when there are no images.

Q3: Can I display different banner images on different devices (such as PC and mobile phone)?A3: Security

Related articles

How to convert image addresses to thumbnail format for display?

In website operation, images play a crucial role.They can not only attract users' attention but also effectively convey information.However, very large images can severely affect website loading speed, thereby damaging user experience and search engine rankings.Therefore, converting the image address to a thumbnail format for display has become an indispensable part of optimizing website performance.AnQiCMS provides very convenient features in this aspect, it can automatically generate and manage thumbnail images for your website, and also provides flexible calling methods to help you easily meet the challenges of image optimization.### AnQiCMS

2025-11-08

How to truncate long text content and add an ellipsis at the end to optimize list display?

In website operation, especially managing a content-rich platform, the display effect of the list page is crucial for user experience.Whether it is an article list, product overview, or news update, we hope the page is tidy and the information is clear at a glance.However, when the document content or description is too long, directly displaying it in the list often leads to layout confusion, affecting the overall aesthetics and information acquisition efficiency.At this moment, effectively truncating text and adding an ellipsis is particularly important.AnQiCMS (AnQiCMS) provides a powerful and flexible template system, in which built-in text filters can help us easily solve this problem

2025-11-08

How to safely output HTML tags in website content to avoid escaping?

In website content management, especially when content includes rich formats or user input, how to safely output HTML tags is a crucial issue.AnQiCMS (AnQiCMS) fully considered this from the beginning, its template engine defaults to escaping output variables to effectively prevent cross-site scripting attacks (XSS) and other security risks.### Understanding the Default Behavior of Template Engines The Anqi CMS template engine follows the syntax of mainstream frameworks like Django, one of its core concepts being 'Security First'

2025-11-08

How to embed mathematical formulas and flowcharts in a page and ensure correct rendering display?

In website operation, we often need to display some professional content, such as mathematical formulas involving scientific calculations, or complex flowcharts for business process descriptions.The traditional way of displaying this content is often inefficient, difficult to maintain, and also does not provide a good reading experience.AnQiCMS combines its powerful content management capabilities and support for Markdown to provide us with an elegant solution.By simply introducing some external libraries and enabling the Markdown editor, we can display the professional content correctly on the website page

2025-11-08

How to dynamically display different content blocks based on conditions (if tag)?

In Anqi CMS, the dynamic display of website content is the key to improving user experience and operational efficiency.You often encounter such needs: you want a certain content block to appear only under specific conditions, or to display different information for different situations.At this point, the powerful conditional judgment feature of Anqi CMS - the `if` tag, can be put to good use.The Anqi CMS template system adopts a syntax similar to the Django template engine, allowing you to implement logical judgments in templates in a straightforward manner.By flexibly using the `if` tag, you can easily control the display and hide of web elements

2025-11-08

How to use a loop (for label) to traverse and display a content list?

In Anqi CMS, whether it is displaying article lists, product catalogs, navigation menus, or friend links, we will frequently deal with content lists.To dynamically display this content on the website front-end, you can't do without powerful looping functions.Today, let's delve deeper into how to use the loop (`for` tag) in Anqi CMS templates to traverse and display your content list. ### The core syntax of the `for` tag: Bring content to life The Anqi CMS template syntax is similar to Django and Blade, very intuitive.When you need to handle a set of data

2025-11-08

How to format a timestamp into a readable date and time string for display?

In Anqi CMS, formatting timestamps into readable dates: A practical guide When managing website content, we often encounter the need to display article publishing time, update time, and user registration time information.These data are usually stored in the database in the form of a string of numbers - that is, a 'timestamp'.Although computers can easily understand these numbers, they may not seem so friendly to users visiting websites.For example, seeing a number like '1678886400' is not as intuitive as '2023 March 15 10:00:00'

2025-11-08

How to define template variables and reuse them in content display?

In AnQi CMS, managing and displaying website content, flexibly using template variables is undoubtedly the key to improving efficiency and maintaining consistency.Whether it is necessary to unify the information of the entire site or to customize unique attributes for specific content, understanding how to define and reuse template variables can make content operation more skillful. ### From the background source to define variables In AnQi CMS, many of the information we see on the website every day is actually managed through backend predefined variables.This means you don't have to touch a single line of code to easily define and modify these contents. First

2025-11-08