How to render the category Banner image set in the backend in the front-end template?

Calendar 👁️ 64

In Anqi CMS, displaying the Banner images of categories on the front-end template is a common operation that can effectively enhance the visual effects of the page.AnQi CMS provides flexible backend settings and powerful front-end template tags, making this process simple and intuitive.


Step 1: Set the category Banner image in the backend

First, we need to upload a Banner image to the target category in the Anqi CMS backend management system. This operation is very intuitive:

  1. Log in to your Anqi CMS backend.
  2. Navigate to the "Content Management" section and click on "Document Category".
  3. Here, you can choose to edit an existing category or create a new one. After entering the category editing page, you will see various settings related to categories.
  4. Scroll down the page to find the "Other parameters" section. After expanding this section, you will see an option named "Banner image".
  5. Click the upload button, you can upload one or more Banner images for this category.If you need to display a carousel effect, it is recommended to upload multiple images and ensure that all images are of the same size, so that the visual effect will be better when displayed on the front end.
  6. Moreover, you will also see the 'thumbnail' and 'thumbnail large image' options, which are usually used to display a single category image, such as in the icon or main image of the category list page.Although the 'Banner Image' is mainly used for large horizontal banners at the top of the page, in some designs, you can also use the 'Thumbnail Large Image' as a single Banner.
  7. After uploading the image, remember to save your category settings.

Step two: Understand the principle of front-end template rendering.

The Anqi CMS front-end template system is designed to be very flexible, it adopts a syntax similar to the Django template engine. This means that in the template file, you will use double curly braces{{变量}}To output data, as well as single curly braces and percentages{% 标签 %}To perform logical judgments or loop operations.

Template files are usually named with.htmlsuffix and stored in/templateUnder the directory. For pages related to categories, such as category list pages, template files may be named as{模型table}/list.html, or you can customize templates for specific categories, such as{模型table}/list-{分类ID}.html.

To render the background-set category Banner image, the core is to use the built-in template tags of Anqi CMS to obtain the detailed information of the category. Among them,categoryDetailThe tag is a key tool for obtaining detailed information about a single category, it can access all category attributes including the Banner image.

Step 3: Call the Category Banner image in the frontend template

The way to call it will be slightly different depending on which page you want to display the Category Banner.

1. Display Banner Image on Current Category Page

If you want to display a category-specific Banner at the top of a list page or detail page for a category, you can use thecategoryDetailtag to get the data for the current category.

For example, in/template/{您的模板目录}/{模型table}/list.html(Or custom category template) in the file, you can call it like this:

{# 获取当前分类的Banner组图 #}
{% categoryDetail categoryBanners with name="Images" %}
{% if categoryBanners %} {# 判断是否存在Banner图片 #}
<div class="category-banner-slider">
    {# 遍历Banner组图中的每一张图片 #}
    {% for item in categoryBanners %}
    <img src="{{ item }}" alt="{% categoryDetail with name='Title' %}" class="banner-image">
    {% endfor %}
</div>
{% else %}
{# 如果没有设置Banner图,可以显示一个默认图片或留空 #}
<div class="default-banner">
    <img src="/public/static/images/default-banner.jpg" alt="默认分类横幅">
</div>
{% endif %}

{# 如果只需要显示分类的“缩略图大图”(Logo),可以这样调用: #}
{% categoryDetail categoryLogo with name="Logo" %}
{% if categoryLogo %}
<div class="category-main-image">
    <img src="{{ categoryLogo }}" alt="{% categoryDetail with name='Title' %}" class="logo-image">
</div>
{% endif %}

In this code block:

  • {% categoryDetail categoryBanners with name="Images" %}It will get all the Banner image arrays set in the background of the current category and assign them tocategoryBannersVariable.
  • {% if categoryBanners %}Used to determine if the Banner image has been uploaded, to avoid errors or blank displays when there is no image.
  • {% for item in categoryBanners %}Loop through the image array,itemHere is the image URL directly.
  • {% categoryDetail with name='Title' %}Used to get the current category title, as the image'saltattribute, which is helpful for SEO.
  • name="Logo"Then you can get the thumbnail and full image you uploaded on the backend.

2. On other pages (such as the homepage) call the specified category of Banner images

If you need to display a specific category Banner on the homepage or other non-category pages, you cancategoryDetaillabel'sidspecify the parameter to get the data of which category.

Assuming you want to display the Banner of the "Product Display" category with ID 5 on the homepage:

{# 调用ID为5的分类的Banner组图 #}
{% categoryDetail productCategoryBanners with name="Images" id="5" %}
{% if productCategoryBanners %}
<div class="homepage-product-banner">
    {% for item in productCategoryBanners %}
    <img src="{{ item }}" alt="{% categoryDetail with name='Title' id='5' %}" class="banner-image">
    {% endfor %}
</div>
{% endif %}

{# 也可以结合categoryList标签,遍历多个分类并显示它们的Logo或Banner #}
{% categoryList featuredCategories with moduleId="2" parentId="0" limit="3" %} {# 假设模型ID为2是产品模型,获取3个顶级分类 #}
<div class="featured-categories-banners">
    {% for category in featuredCategories %}
    <div class="category-card">
        <h3><a href="{{ category.Link }}">{{ category.Title }}</a></h3>
        {# 获取每个分类的Logo作为小Banner展示 #}
        {% if category.Logo %}
        <img src="{{ category.Logo }}" alt="{{ category.Title }}" class="category-card-logo">
        {% endif %}
    </div>
    {% endfor %}
</div>
{% endcategoryList %}

This code demonstrates how to specifyidTo get the Banner of a specific category and how to iterate over multiple categories to fetch and display theirLogo(usually the main category image or small Banner).

Step 4: Optimization and Advanced Application

  • Style control:The above code provides only the basic HTML structure. You need to combine CSS to beautify the Banner display effects, such as setting width, height, margin, animation, etc.
  • Carousel effect:If you upload multiple Banner images, you may need to introduce JavaScript libraries (such as Swiper.js, Owl Carousel, etc.) to implement automatic scrolling, switch buttons, and pagination indicators for interactive effects to enhance the user experience.
  • Responsive design:

Related articles

How to display a custom shutdown prompt to users when the website is under maintenance in AnQiCMS?

During the operation of the website, maintenance and upgrades are inevitable steps.Whether it is system updates, data migration, or solving some unexpected problems, the website needs to be temporarily closed to ensure the smooth progress of operations and the integrity of the data.How can you inform the visitor in a friendly manner that the website is under maintenance, rather than making them face a blank page or error message, which is particularly important.AnQiCMS provides a flexible shutdown prompt mechanism to help you display professional and clear custom information to users during website maintenance.### Enable Station Mode

2025-11-07

How can I use the custom URL alias of articles or categories for pseudo-static link display?

## Leverage the advantages of AnQi CMS custom URL alias to create SEO-friendly static links In website operations, a clear and semantically meaningful URL address not only helps users understand the content of the page at a glance, but also wins the favor of search engines, thus improving the inclusion and ranking of the website.AnqiCMS (AnqiCMS) is well-versed in this, providing powerful custom URL alias and flexible static configuration features to help website operators easily achieve this goal.Why is it so important to use custom URL aliases and permalinks?Imagine

2025-11-07

How to control the style of each element within a loop in AnQiCMS templates (such as odd and even rows differently)?

In website content display, we often need to present data in a list form, such as article lists, product lists, or category navigation.To make the page more attractive or to better organize information, we often hope that each element in the list has a different style, such as the common even-odd row background **split.AnQiCMS provides a powerful and flexible template system, making it very simple to implement these fine style controls.AnQiCMS's template system borrows the syntax of the Django template engine, which is characterized by the use of double curly braces for variables

2025-11-07

How to display website traffic statistics and spider crawling information on the front end?

We often care about the number of visits to our website, and which search engine spiders have crawled our content.This data can not only help us understand the health status of the website, optimize the content strategy, but also improve user trust.The Anqi CMS provides detailed traffic statistics and crawler monitoring functions in the background, allowing us to have a thorough understanding of the website's operational status.If you want to present valuable data such as today's visitor count and yesterday's spider visit count directly on the website front end, how can you achieve this?### AnQi CMS data statistics capability First

2025-11-07

How to use the content model in AnQiCMS to define different content display structures?

AnQiCMS Content Model: The Smart Choice for Creating Flexible and Variable Website Content In the digital age, a website is not just a platform for displaying information, but also a gathering point for diverse content such as corporate brands, product services, and professional knowledge.If always using a fixed display structure for different types of content, the website content is likely to become monotonous and difficult to manage efficiently.AnQiCMS fully understands this, the "Content Model" feature it provides is the core tool to solve this pain point, helping us easily define and manage various unique content display structures.Imagine

2025-11-07

How to display the reading and comment counts of articles on the front end?

In website operation, the number of article views and comments is an important indicator of content popularity and user interaction.AnQiCMS as a flexible content management system provides a convenient way to display these data on the website front end.By using simple template tags, even without delving into programming, these functions can be easily realized.The AnQiCMS template system borrows the syntax of the Django template engine, using double curly braces `{{variable}}` to output variable content, and using single curly braces and percentages `{% tag %}` to call function tags

2025-11-07

How to prevent malicious collection of content in AnQiCMS and affect the exclusivity of front-end content?

Original content is the core value of the website, it condenses our thoughts, time and energy.However, the content has been maliciously collected, plagiarized, and published on other platforms, not only diluting the exclusivity of our content, but also potentially affecting the SEO performance of the website and even damaging the brand image.As website operators, we all hope that our front-end content can maintain its original exclusivity. 幸运的是,AnQiCMS was designed with this pain point in mind from the very beginning, incorporating multiple powerful features to help us effectively prevent content from being maliciously scraped and protect our original value. ### 1. Core Defense

2025-11-07

How to dynamically add a website name suffix to the page title to optimize SEO display?

In content operation, the page title is undoubtedly the first barrier to attracting the attention of search engines and users.A clear, keyword-rich, and brand-consistent title that not only improves user click-through rate but is also a key element of search engine optimization (SEO).AnQiCMS is an enterprise-level content management and SEO optimization system that provides a flexible and powerful mechanism, allowing you to easily add a website name suffix to the page title, thereby achieving a win-win situation for brand enhancement and SEO.### Unified Brand Image

2025-11-07