How to display the image gallery based on whether there is an album in the `Images` field of `archiveDetail` or `pageDetail`?

Calendar 👁️ 56

Smartly use AnQi CMS: intelligently judge and display the image gallery in articles/pages

As an experienced website operation expert, I am well aware of the importance of content display for user experience and website attractiveness.In AnQiCMS (AnQiCMS) such an efficient and customizable content management system, how to flexibly present image content, especially dynamically displaying the image gallery based on whether there is an album, is a focus of many operators.Today, let's delve deeply into this practical skill, making your website content more visually striking while also maintaining the simplicity and elegance of the code.

AnQi CMS with its high-performance architecture in Go language and flexible content model provides us with great convenience. In the article (archiveDetail) and single page (pageDetailDuring the editing process, we usually use a name calledImagesThe field.This field is specifically used to store a collection of images related to the content, which can be illustrations for articles, multi-angle displays of products, or carousel images on a single page.Its design purpose is to make it convenient for content managers to upload and manage a set of images, while the front-end template is responsible for presenting these images beautifully to visitors.

However, having just oneImagesThe field is not enough.A mature website should have intelligent and dynamic content display.We do not want to display an empty image gallery area when no images are uploaded to an article or page, as this not only wastes page space but also affects the overall user experience.Only whenImagesThe field indeed contains images when the image gallery is rendered; otherwise, let this area 'disappear'.

The Anqi CMS template engine syntax is similar to Django, making conditional judgments and loop traversal intuitive and easy to understand.ImagesThe field is considered to be an array of image links (or a slice) in the template.This means we can easily check if it is empty and traverse each image link when it is not empty.

Practical exercise: Implementing intelligent display of image galleries in templates.

Now, let's implement the smart image gallery feature step by step through specific template code.

Step 1: Safely obtain image data

Whether it is an article detail or a single page detail, we need to get it through the corresponding tags firstImagesthe value of the field. Suppose we are on an article detail page, we can usearchiveDetailTags:

{% archiveDetail articleImages with name="Images" %}

Here, we declared a variable namedarticleImagesthat will carryImagesall the image URLs in the field. IfImagesthe field is empty,articleImageswill be an empty array.

If it is a single-page detail page, justarchiveDetailReplacepageDetailand it is done:

{% pageDetail pageImages with name="Images" %}

Similarly,pageImagesThe variable will save the image array of the single page.

Second step: determine if there are images available to display

After obtaining the image data, the key judgment link comes. Anqing CMSifThe logical judgment tag can complete this task very simply. When an array or slice (such asarticleImagesorpageImages) is usedifWhen a statement is executed, the template engine automatically determines whether it contains any elements. If it contains any, the condition is true; if it is empty, the condition is false.

{% if articleImages %}
    {# 这里是当有图片时才显示的内容 #}
{% endif %}

Or for a single page:

{% if pageImages %}
    {# 这里是当有图片时才显示的内容 #}
{% endif %}

In this way, we ensure that onlyImagesthe container of the gallery is rendered to the page when the field has actual content.

Step three: loop to display images and build the gallery

Once confirmed,articleImages(orpageImagesThe condition is not empty, we can use it.forLoop through this array and render each image.

{% if articleImages %}
    <div class="image-gallery">
        {% for imageUrl in articleImages %}
            <div class="gallery-item">
                <img src="{{ imageUrl }}" alt="文章图片" loading="lazy">
            </div>
        {% endfor %}
    </div>
{% endif %}

In the above code snippet:

  • We created a namedimage-gallerycontainer, this is the external framework of the gallery.
  • {% for imageUrl in articleImages %}The loop will iterate overarticleImageseach image link in the array. In each iteration, the current image link will be assigned toimageUrlVariable.
  • {{ imageUrl }}directly output the image link asimglabel'ssrcProperty.
  • alt="文章图片"This is an image description added for SEO and accessibility, it is recommended to dynamically generate based on actual content.
  • loading="lazy"It is a very practical HTML5 attribute that can implement lazy loading of images and optimize page performance.

The same logic applies to single-page applications, you just need to replace the variable name:

{% if pageImages %}
    <div class="page-gallery">
        {% for imageUrl in pageImages %}
            <div class="gallery-item">
                <img src="{{ imageUrl }}" alt="页面图片" loading="lazy">
            </div>
        {% endfor %}
    </div>
{% endif %}

Comprehensive example: Article detail page image gallery

Let's integrate the above steps together and see what the code for the image gallery section of a complete article detail page would look like:

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

    {# 智能图片画廊区域 #}
    {% archiveDetail articleImages with name="Images" %}
    {% if articleImages %}
        <div class="dynamic-image-gallery">
            <h3>图集欣赏</h3>
            <div class="gallery-grid">
                {% for imgUrl in articleImages %}
                    <a href="{{ imgUrl }}" data-lightbox="article-gallery" class="gallery-link">
                        <img src="{{ imgUrl }}" alt="{{ archive.Title }} - 图{{ forloop.Counter }}" loading="lazy">
                    </a>
                {% endfor %}
            </div>
        </div>
    {% else %}
        {# 如果没有图片,也可以选择显示一个默认提示或什么都不显示 #}
        <p class="no-images-tip">该文章暂无图片图集。</p>
    {% endif %}

    <div class="article-content">
        {%- archiveDetail articleContent with name="Content" %}
        {{ articleContent|safe }}
    </div>

    {# 其他文章内容,如相关推荐、评论等 #}
</article>

In this example, we also addeddata-lightbox="article-gallery"Property, this is usually combined with the front-end Lightbox (lightbox) JS library to provide users with the function of zooming in on images by clicking, greatly enhancing the user experience. At the same time,altThe attribute combines the article title with a loop counter, which is helpful for SEO optimization.

Advanced thinking and optimization

  1. Lazy loading of images and performance optimization: Besidesloading="lazy", Anqi CMS also providesImagesField supportslazy="{定义的src名}"For example, to handle lazy loading of images,lazy="data-src". In the content settings, you can also configure whether to automatically compress large images, convert to WebP format, and other functions to further improve image loading performance.
  2. **Front-end

Related articles

How to determine whether the current page is the homepage, category page, detail page, or single page, and load the corresponding SEO information?

As an experienced website operations expert, I am well aware of the importance of Search Engine Optimization (SEO) in the increasingly fierce online environment.To do SEO well, the primary task is to ensure that every page of the website provides clear and accurate metadata to search engines.How can one determine the current page type (home page, category page, detail page, single page, etc.) and load the corresponding SEO information, which is a core issue often encountered by many operators and developers.Today, let's delve into the intelligent design and practical strategies of AnQiCMS (AnQiCMS) in this aspect

2025-11-06

How to determine whether a user is logged in or has specific permissions to display private content in AnQiCMS templates?

As an experienced website operation expert, I fully understand the importance of realizing fine-grained content display in a content management system.Especially for systems like AnQiCMS that focus on enterprise-level applications and user experience, dynamically presenting content based on the user's login status or their permission group is a core strategy to enhance website personalization, achieve content monetization, and even build a membership system.Today, let's delve into how to skillfully utilize the powerful functions of the AnQiCMS template to achieve this goal

2025-11-06

How to use the `block` tag under the `if` condition to decide whether to override the parent template content when inheriting (`extends`) a template?

## Advanced Anqi CMS Template: Skillfully Using `extends` and `block` to Achieve Conditional Content Coverage We are well aware of the importance of website content maintainability and flexibility in the daily operation of website management for efficient operation.AnQi CMS provides a solid foundation for content management with its efficient architecture based on the Go language and a Django-like template engine syntax.Among them, template inheritance (`extends`) and content blocks (`block`) are the core of building reusable, easily expandable website layouts.But just inheriting and overriding is not enough, many times

2025-11-06

The AnQi CMS template development tool: gracefully handle missing files using the `if_exists` parameter of the `include` tag

As an experienced website operations expert, I know how important efficiency and stability are in building and managing corporate websites.AnQiCMS (AnQiCMS) boasts its high-performance architecture based on the Go language and a flexible template system, providing us with powerful content management capabilities.In daily template development, the `include` tag is undoubtedly our powerful assistant for achieving modularization and improving code reusability.However, convenience also hides a small trap: if the template file referenced by `include` is unfortunately missing, the website page will immediately throw an error, affecting user experience

2025-11-06

How to determine whether a site has multilingual versions and display a language switch button using the `tag-languages` multilingual site tag?

AnQi CMS, as a Go language system deeply rooted in the field of enterprise-level content management, its powerful multilingual support is undoubtedly one of the core advantages to help enterprises expand into the global market.How can we intelligently determine whether a website is configured with multiple language versions and elegantly display language switch buttons when building websites for global users, which is a common problem encountered by content operation experts in template design.Today, let's delve into the `tag-languages` tag of Anqi CMS together, uncovering the mysteries of language switching.###

2025-11-06

How to judge whether the `userDetail` user group is VIP and display exclusive identification in AnQiCMS templates?

In the daily operation of AnQiCMS, we often encounter the need to provide differentiated services and display content for different user groups.For VIP members, displaying a dedicated identifier not only enhances their sense of belonging but is also an important part of the website's content monetization strategy.As an experienced website operation expert, I know the importance of accurately identifying user identity and flexibly displaying content in templates.

2025-11-06

How to display different fields in `moduleDetail` based on model type (such as `article model` or `product model`)?

Deep analysis of AnQi CMS: How to display different fields in `moduleDetail` based on model type to create a personalized content experience As a senior website operation expert, I know that a truly excellent content management system is not just about publishing content, but also needs to be flexible to adapt to various content forms.AnQiCMS (AnQiCMS) performs exceptionally well in this aspect, its powerful 'content model' function is the secret weapon for us to achieve personalized content display.

2025-11-06

How to determine if the document list under a specified tag is empty?

AnQi CMS, as an enterprise-level content management system built with Go language, has always been committed to providing users with efficient, customizable, and secure content operation solutions.In our daily content management work, especially when building dynamic, intelligent websites, we often need to flexibly adjust the display of the page based on the presence or absence of content.Today, let's delve deeply into a very practical and elegant feature of Anqi CMS - how to use the `tag-tagDataList` tag to determine if the document list under a specified tag is empty.In AnQi CMS

2025-11-06