How to implement conditional judgment in a template, for example, whether to display an image based on whether the article has a thumbnail?

Calendar 👁️ 77

In website content presentation, images play a crucial role.However, not all content has corresponding thumbnails, or for design aesthetics and loading speed considerations, we may need to decide according to the actual situation whether to display images, even different placeholder images.AnqiCMS provides a flexible and powerful template engine, allowing us to easily implement this conditional image display logic, thereby enhancing the flexibility and user experience of website content.

This article will focus on how to determine whether to display an image based on whether an article has a thumbnail in the AnqiCMS template and will explain the implementation method in detail.

Basic conditional judgment in AnqiCMS template

AnqiCMS template system has borrowed the syntax of Django template engine, and its conditional judgment is mainly through{% if 条件 %}and{% endif %}Tag to implement. WhenifWhen the condition expression in the tag is true (True), the content inside it will be rendered and displayed; otherwise, the content will be ignored.

For example, to judge a variablemyVariableIs or non-empty, can be used directly:

{% if myVariable %}
    {# myVariable 存在且非空时显示这里的内容 #}
{% else %}
    {# myVariable 不存在或为空时显示这里的内容 #}
{% endif %}

This concise syntax makes handling various logical judgments in templates very intuitive.

Scenario one: Determine if a single article has a thumbnail on the article detail page

In the article detail page, the data of the current article is usually directly exposed in the template.archiveIn the object. Therefore, we can directly pass througharchive.Thumbfield to determine whether the current article has a thumbnail.archive.ThumbThe path string of the thumbnail image will be returned, if the article does not have a thumbnail set, this field will usually be an empty string.

Here is an example of a code snippet that determines and displays a thumbnail on an article detail page:

{# 假设这是文章详情页的模板(例如 detail.html) #}

<div class="article-content">
    <h1>{{ archive.Title }}</h1> {# 文章标题 #}
    <div class="article-meta">
        <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
        <span>浏览量:{{ archive.Views }}</span>
    </div>

    {# 判断文章是否有缩略图 #}
    {% if archive.Thumb %}
        <div class="article-thumbnail">
            <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}" loading="lazy">
        </div>
    {% else %}
        {# 如果没有缩略图,可以显示一个默认的占位图 #}
        <div class="article-thumbnail-placeholder">
            <img src="/public/static/images/default-thumb.png" alt="无图" loading="lazy">
        </div>
        {# 或者,如果不想显示任何图片,可以直接省略 else 部分 #}
    {% endif %}

    <div class="article-body">
        {{ archive.Content|safe }} {# 文章内容,使用 safe 过滤器防止 HTML 被转义 #}
    </div>
</div>

In this example, we first check:archive.ThumbIs there a value. If it exists, display the thumbnail; if not, display a preset default placeholder image.

Scenario two: Loop through each article on the article list page to determine if it has a thumbnail

On the article list page, we usually use{% archiveList %}Tag to get the list of articles and loop through them. In the loop, the data of each article item will be accessed through a variable (such asitemorarticle) to access. Similarly, we can also throughitem.ThumbTo determine the thumbnail status of each article.

Here is an example of a code snippet that implements conditional judgment and displays thumbnails on the article list page:

{# 假设这是文章列表页的模板(例如 list.html) #}

<div class="article-list">
    {% archiveList articles with type="page" limit="10" %} {# 获取分页文章列表,每页10篇 #}
        {% for article in articles %}
            <div class="article-item">
                {# 判断当前文章是否有缩略图 #}
                {% if article.Thumb %}
                    <div class="item-thumbnail">
                        <img src="{{ article.Thumb }}" alt="{{ article.Title }}" loading="lazy">
                    </div>
                {% else %}
                    {# 如果没有缩略图,显示一个列表页的默认占位图 #}
                    <div class="item-thumbnail-placeholder">
                        <img src="/public/static/images/default-list-thumb.png" alt="无图" loading="lazy">
                    </div>
                {% endif %}

                <div class="item-info">
                    <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
                    <p>{{ article.Description|truncatechars:120 }}</p> {# 截取文章描述 #}
                    <a href="{{ article.Link }}" class="read-more">阅读详情</a>
                </div>
            </div>
        {% empty %}
            {# 如果文章列表为空,显示提示信息 #}
            <p class="no-articles">目前还没有文章发布。</p>
        {% endfor %}
    {% endarchiveList %}

    {# 列表页的分页导航 #}
    <div class="pagination">
        {% pagination pages with show="5" %}
            {# 分页代码,此处省略具体实现,参考 AnqiCMS 分页标签文档 #}
        {% endpagination %}
    </div>
</div>

In this example on the list page, we traversearticlesEach one in the collectionarticleObject, and for eacharticle.ThumbPerform conditional judgment, thereby dynamically displaying thumbnails or placeholders for each article.

More flexible image judgment: Logo, Images and custom fields

exceptThumbField, AnqiCMS also provides other fields related to images, you can judge according to your needs:

  1. archive.Logo(Cover Main Image)It usually represents the cover image or main image of an article. Its judgment method isarchive.ThumbSimilar.

    {% if archive.Logo %}
        <img src="{{ archive.Logo }}" alt="{{ archive.Title }}">
    {% endif %}
    
  2. archive.Images(Group photo)If the article has uploaded multiple images as a group photo,archive.ImagesIt will be an array of image paths (Slice). You can check if the collage exists by checking the length of this array.“`twig {% if archive.Images|length > 0 %}

    <div class="article-gallery">
        <img src="{{ archive.Images[0] }}" alt="{{ archive.Title }} - 第一张图片"> {# 显示组图的第一张图片 #}
        {# 如果需要,也可以循环显示所有组图 #}
        {% for image_url in archive.
    

Related articles

How to control the number of characters displayed for the article abstract (Description) in the list and automatically add an ellipsis in AnQiCMS?

When operating website content, the display length of the article summary (Description) on the list page is crucial for the neatness of the page and the user experience.A long summary can make the page look bloated and difficult to browse quickly, while a short one may not attract readers to click.AnQiCMS as a feature-rich content management system, provides flexible template tags and filters, allowing us to easily implement precise control over the word count displayed in article summaries and automatically add elegant ellipses.###

2025-11-09

How to set up a dynamic carousel effect for the Banner image on the homepage of AnQiCMS website?

In AnQiCMS, setting a dynamic banner slideshow on the homepage can effectively enhance the visual appeal of the website and convey key information.AnQiCMS provides flexible and intuitive features to help us achieve this goal.Next, we will step by step understand how to set up from the background to the front-end template editing, creating an animated carousel effect for the homepage.### 1. Backend configuration of Banner image: Content and grouping Firstly, we need to prepare the images and relevant information for the carousel in the AnQiCMS backend

2025-11-09

Does AnQiCMS support displaying the number of articles under each category on the category list page?

When operating a website, we often encounter such needs: when displaying the website category list, we hope to see intuitively how many articles are under each category.This not only helps visitors quickly understand the richness of a topic's content, improve user experience, but also enables us content operators to better plan content strategies, and even has unique value in SEO optimization. Then, does AnQiCMS support displaying the number of articles under each category on the category list page?The answer is affirmative, and it is very convenient to implement. In the template design of AnQiCMS

2025-11-09

How to call and display the title and link of the previous and next articles on the article detail page?

In AnQiCMS, adding navigation for the previous and next articles on the article detail page is an important step to improve the user reading experience and optimize the internal link structure of the website.This feature not only guides readers to continue browsing related content, but also helps search engines better understand the website structure.AnQiCMS's powerful template system provides a very convenient way to meet this requirement.### AnQiCMS Template Basics Review The AnQiCMS template system is simple and efficient, using syntax similar to Django, allowing developers to build pages intuitively

2025-11-09

How to correctly render Markdown-formatted article content as HTML and display mathematical formulas in AnQiCMS?

In AnQi Content Management System (AnQiCMS), we are committed to making content creation and display more flexible and efficient.For users accustomed to writing articles in Markdown format, we provide powerful support, ensuring that Markdown content is correctly rendered into HTML, and can easily handle complex mathematical formulas and flowcharts, bringing vitality to technical documents, tutorial articles, and other content.### Enable Markdown Editor: The First Step of Content Creation Enjoy Markdown Convenience in AnQiCMS

2025-11-09

How to display the custom parameters of the product on the product detail page (for example: color, size)?

In website operation, adding custom parameters to the product detail page, such as color, size, etc., is an important link to improve user experience and provide more detailed product information.AnQiCMS (AnQiCMS) can easily meet this requirement with its flexible content model and powerful template system.Next, we will discuss in detail how to display these custom parameters on the product detail page.--- ### Step 1: Define custom fields in the product content model backend One of the core advantages of AnQi CMS is its highly flexible content model

2025-11-09

What thumbnail processing methods does AnQiCMS support to optimize the effect of images in different display scenarios?

In modern website operations, images are not only an important part of the content, but also a key factor affecting user experience and website performance.A well-managed image system can significantly improve the loading speed, visual appeal, and search engine optimization effect of a website.AnQiCMS understands the importance of image optimization and therefore provides users with a variety of flexible thumbnail processing methods to ensure that images achieve the desired effect in various display scenarios.AnQiCMS provides three core strategies for handling image thumbnails, each suitable for different display needs

2025-11-09

How can you implement a multi-dimensional filtering function on the article list or product list page (such as by price, attributes)?

I am glad to discuss with you how AnQi CMS can help us achieve multi-dimensional content filtering.In this era of information explosion, users are increasingly pursuing efficiency and personalization in obtaining content.A website that allows users to quickly locate content according to their own needs will undoubtedly greatly improve the user experience and conversion rate.AnQi CMS provides very powerful and flexible functions in this aspect, especially through custom content models and specific template tags, we can easily build multi-dimensional filtering functions on article list or product list pages, just like the "price filtering" commonly seen on e-commerce websites

2025-11-09