How to determine if a content has a thumbnail in the front-end template and display it conditionally?

Calendar 👁️ 66

In the display of website content, images, especially thumbnails, play a crucial role.They not only attract the reader's attention, but also enhance the beauty and information transmission efficiency of the page.However, in actual operation, we often encounter cases where some content may not have uploaded thumbnails.If the front-end template tries to display without distinction, it may result in an ugly blank area or a damaged image icon at best, or it may affect the page layout and even the user experience at worst.

AnQi CMS as an efficient and flexible content management system fully considers such needs.With its powerful and easy-to-use template engine, we can easily determine on the front end whether a piece of content has a thumbnail and display it conditionally based on the result.

Identify the thumbnail field in the content

In Anqi CMS, whether it is articles, products, single pages, or categories, the core content data usually includesLogoandThumbtwo fields to represent image information.LogoIt usually refers to the main image or cover large image, whileThumbit focuses more on the thumbnail processed by the system, which may be smaller in size and suitable for display on list pages or in recommended areas.

In the template, when you get the content object (such as the items in the listitemor the details pagearchive), you can directly access{{item.Thumb}}or{{archive.Thumb}}the thumbnail URL.

Use conditional judgment tags to display conditionally

The Anqi CMS template system is based on Django template engine syntax, which means we can take advantage of its powerful conditional judgment tags{% if %}To checkThumbDoes the field exist and have a valueThumbThe condition is true when the field has content (i.e., not an empty string); otherwise, it is false.

1. Determine and display the thumbnail on the content list page:

Suppose you are usingarchiveListTag loop to display the article list. In each loop, you can check the current article objectitemWhether there is a thumbnail.

{# 使用 archiveList 标签获取文章列表 #}
{% archiveList archives with type="list" limit="6" %}
    {% for item in archives %}
    <div class="article-item">
        {# 首先判断 item.Thumb 是否有值 #}
        {% if item.Thumb %}
            <a href="{{ item.Link }}" class="article-thumb">
                <img src="{{ item.Thumb }}" alt="{{ item.Title }} 缩略图">
            </a>
        {% else %}
            {# 如果没有缩略图,可以显示一个默认的占位图,或者干脆不显示图片区域 #}
            <a href="{{ item.Link }}" class="article-thumb-placeholder">
                <img src="/public/static/images/default-placeholder.webp" alt="{{ item.Title }} 无图">
            </a>
        {% endif %}
        <div class="article-info">
            <a href="{{ item.Link }}">
                <h3 class="article-title">{{ item.Title }}</h3>
            </a>
            <p class="article-description">{{ item.Description|truncatechars:100 }}</p>
        </div>
    </div>
    {% empty %}
        <p>暂时没有文章内容。</p>
    {% endfor %}
{% endarchiveList %}

In the above example, ifitem.ThumbIf it exists, it will display the thumbnail of the article; if it does not exist, it will display a preset default placeholder image.You can choose not to display the image area according to your design needs, or use CSS to hide the area.

2. Determine and display the thumbnail on the content detail page:

When you visit the detail page of an article, usuallyarchivethe object is directly accessible. You can also judge itsThumbfields.

{# 假设这是文章详情页模板,archive 对象直接可用 #}
<article class="post-detail">
    <h1 class="post-title">{{ archive.Title }}</h1>

    <div class="post-meta">
        <span>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
        <span>浏览量:{{ archive.Views }}</span>
    </div>

    {# 判断详情页的缩略图 #}
    {% if archive.Thumb %}
        <div class="post-thumbnail">
            <img src="{{ archive.Thumb }}" alt="{{ archive.Title }} 封面图">
        </div>
    {% endif %}

    <div class="post-content">
        {{ archive.Content|safe }}
    </div>
</article>

In this example of the detail page, ifarchive.ThumbIf the field has a value, a thumbnail will be displayed below the article title; ifarchive.Thumbit is empty, the entirepost-thumbnailarea will not be rendered, keeping the page simple.

Enhance thumbnail display logic using backend settings and filters

The Anqi CMS considers thumbnail processing very thoroughly.If you do not manually upload a thumbnail when posting content, the system will automatically try to extract the first image from the content as a thumbnail.Further, you can also upload a default thumbnail in the "System Settings" -> "Content Settings" on the backend.This, when any content does not have an explicit thumbnail, the system will automatically use this default image.

This means, in many cases,item.ThumbThe field may already contain the URL of an automatically extracted image or a global default image, even if no thumbnail has been uploaded. However, it is used in the template despite this.{% if item.Thumb %}Judging remains **practical, as it ensures that the page can degrade gracefully in any unforeseen circumstances (such as the system failing to automatically extract images or not setting a global default image).

If your business logic is very complex, you need to display it on the front end based on various conditions (for example, when the content has no thumbnail, the category thumbnail is displayed first, followed by the next one

Related articles

Does AnQiCMS support setting different quality levels for generated thumbnails?

Images occupy an important position in website content, and thumbnails, as key elements for page performance and visual presentation, naturally receive the attention of website operators.Many users want to know, whether AnQiCMS can allow us to finely control the quality level when generating thumbnails.In order to clarify this point, we carefully reviewed the relevant documents of AnQiCMS, especially the sections on image and content settings.

2025-11-08

Does the `thumb` filter support adding watermarks to the generated thumbnails?

In daily website content operation, images, with their intuitiveness and appeal, have become indispensable elements in attracting users and improving the quality of content.Especially the thumbnail, as the 'facade' of the content, its display effect directly affects the user's willingness to click.At the same time, adding watermarks to protect the copyright of original content has become a routine operation for many operators.When using AnQiCMS, we might be curious: Can the commonly used `thumb` filter directly add a watermark to the generated thumbnails?

2025-11-08

How much impact does the operation of batch regenerating thumbnails have on server resources?

## Deep Dive: Server Resource Consumption and Optimization Strategies for AnQi CMS Bulk Resizing Thumbnails In website operation, images are an important part of the content, and their loading speed and display effects directly affect user experience and SEO performance.AnQiCMS (AnQiCMS) fully understands this and provides flexible image management functions, including automatic generation of thumbnails, custom sizes, and various processing methods.

2025-11-08

How does AnQiCMS handle remote images, is it downloaded to the local or directly referenced?

Website operation is in progress, image content is an indispensable element, but how to deal with remote images referenced in articles often makes content creators and operators hesitate: should we directly reference external links, or download them locally for management?AnQiCMS provides a flexible and comprehensive solution to this issue, allowing users to make **choices according to their own needs.AnQiCMS offers a very crucial and practical option in image processing, which can be found in the content settings.

2025-11-08

Does the `thumb` filter automatically adapt to different screen sizes to achieve responsive image display?

When building a website using a content management system, the display effect of images on different devices is a very important issue for users.Especially in today's increasingly popular responsive design, whether images can automatically adapt to various screen sizes and provide an excellent browsing experience is an important standard for measuring the completeness of a system's functions.Today, let's delve deeply into the role and mechanism of the `thumb` filter in responsive image display of Anqi CMS.### `thumb` filter working principle: image generation and optimization Anqi CMS `thumb` filter

2025-11-08

Under the multi-site management mode, can each site independently configure image processing and thumbnail rules?

Under the multi-site management mode of AnQiCMS, it is肯定的 that each site can independently configure image processing and thumbnail rules.AnQiCMS was designed with flexibility and independence in mind for multi-site operations, therefore each site has complete autonomy in the configuration of image processing and thumbnail rules.The multi-site management feature of AnQiCMS is one of its core highlights, allowing users to create and manage multiple independent websites under the same system architecture.

2025-11-08

How to get the original resolution and file size information of images in AnQiCMS?

The website is operational, images are not only an important part of content presentation, but their file size and resolution also directly affect the speed of web page loading and user experience.Get a clear understanding of these core information of your website's image assets, which is crucial for optimizing website performance and managing content quality.AnQiCMS as an efficient content management system provides a straightforward and convenient way to obtain these image metadata.

2025-11-08

Does the `thumb` filter support additional image processing, such as grayscale, blur, etc.?

When using AnQiCMS to manage website content, image processing is an indispensable part of daily operations.We often hope to optimize the display effect of images in various ways, among which the 'thumbnail' feature is particularly commonly used.Many friends may be curious, does the built-in `thumb` filter of Anqi CMS support deeper image processing when generating thumbnails, such as converting it to a grayscale image or applying a blur effect?To answer this question

2025-11-08