How to determine if the thumbnail of a single page exists before displaying it in a template?

Calendar 👁️ 59

As a senior CMS website operator for a safety company, I fully understand the importance of the details of content presentation for user experience and brand image.In daily work, we often encounter the need to flexibly adjust the display method according to the characteristics of the content, including the judgment and display of single-page thumbnails.A page without a thumbnail if forced to display an empty<img>The label is not only unattractive but may also affect the page loading performance. Therefore, conditional judgments in the template are crucial.

I will now detail how to determine if the thumbnail of a single page exists in the AnQiCMS template and display it based on the result.


In AnQiCMS, single pages (such as "About Us", "Contact Us", etc.) usually have a thumbnail field for display in lists or navigation.However, not all single pages will upload thumbnails. To avoid broken image icons or blank areas on the front-end page, we can cleverly use conditional judgment logic in the template to ensure that thumbnails are displayed only when they actually exist.

Firstly, we need to obtain the thumbnail data of the current single page in the single page detail template (usuallypage/detail.htmlor a custom single page template, such aspage/detail-{{单页ID}}.html). AnQiCMS providespageDetailTag to get the detailed information of a single page, including its thumbnail field.

We will usepageDetailTag to get the thumbnail address of a single page and assign it to a variable. For example, we can assign the thumbnail address to a variable namedpageThumb.

{% pageDetail pageThumb with name="Thumb" %}

In this code block:

  • {% pageDetail ... %}Is the tag used by AnQiCMS to obtain detailed information of a single page.
  • pageThumbIt is a custom variable name we use to store the thumbnail URL obtained.
  • with name="Thumb"We specified that we want to retrieve the thumbnail field on a single page.

After obtaining the thumbnail address, the next step is to make a conditional judgment. The AnQiCMS template engine supports{% if %}The tag, we can use it to checkpageThumbWhether the variable contains valid content. IfpageThumbThe variable is not empty (i.e., the thumbnail has been uploaded and there is a URL), then display<img>Label; otherwise, you can choose to display a default placeholder image, or not display the image at all to keep the page clean.

{% pageDetail pageThumb with name="Thumb" %}

{% if pageThumb %}
    <img src="{{ pageThumb }}" alt="{% pageDetail with name='Title' %}" />
{% else %}
    <!-- 如果没有缩略图,可以显示一个默认占位图,或者不显示任何图片 -->
    <img src="/public/static/images/default-fallback-thumb.png" alt="无缩略图" />
{% endif %}

In this complete template snippet:

  • We first pass through{% pageDetail pageThumb with name="Thumb" %}Store the thumbnail URL of a single page topageThumbthe variable.
  • Immediately thereafter,{% if pageThumb %}the judgment.pageThumbDoes the variable have a value. IfpageThumbExists and is a non-empty string, if the condition is true, enterifthe block.
  • InifBlock, we use<img src="{{ pageThumb }}" ... />To display the thumbnail. For SEO and user experience,altThe property dynamically used{% pageDetail with name='Title' %}to obtain the title of a single page as the image description.
  • IfpageThumbIf it is empty, the condition is false, enter{% else %}the block.
  • Inelsewithin the block, I provided an example showing a named/public/static/images/default-fallback-thumb.pngThe generic placeholder. You can replace this path according to your actual needs, or simply omit it.elseBlock, leaving whitespace on the page when there are no thumbnails.

In this way, we can flexibly and elegantly handle the thumbnail display problem on a single page in the AnQiCMS template, ensuring the professionalism and consistency of the website interface.


Frequently Asked Questions (FAQ)

Q1: What will the frontend display on the single page if I haven't set a thumbnail?

A1:If you have made conditional judgments in the template as shown above and have not setelseIf you do not set it, no images will be displayed. If you have setelseA block has specified a default image, and it will display the default image.In addition, the AnQiCMS backend also provides a 'default thumbnail' option in the 'Content Settings', if the document or single page does not have a specific thumbnail, the system may use the default image set here as an alternative.

Q2: BesidesThumbfield, does the single-page have other image fields available as alternatives?

A2:Yes, the single page of AnQiCMS usually has another field called "Single Page Slideshow Group Image", and its data is stored in an array.ImagesYou can access it in the field.ThumbWhen the field is empty, further judgment is madeImagesIf the field exists and is not empty, try to displayImagesThe first picture in the array is used as a backup. This can be done by{% pageDetail pageImages with name="Images" %}obtained, and then used{% if pageImages %}{% set fallbackImage = pageImages[0] %}{% endif %}way to deal with.

Q3: Can I make all web pages without thumbnails display a unified default thumbnail instead of setting it separately in each template, is it possible?

A3:It can be realized. In the AnQiCMS backend, you can navigate to the "Backend Settings" -> "Content Settings" page.There is an option for a 'default thumbnail' on this page.You can upload an image as the default thumbnail for the entire site.After configuration, when any page (including single-page) does not specify its own thumbnail, the system will automatically use the default thumbnail you set here.This can greatly simplify the maintenance of the template.

Related articles

How to only get the first picture in the single-page slide group?

As a website operator deeply rooted in AnQiCMS, I know that every detail of content display is related to user experience and website effect.In daily operations, we often encounter the need to accurately obtain a specific image from a slide set, such as using the first image of a single-page slide as a page header image, cover thumbnail, or background image for a module.The powerful template tag system of AnQi CMS provides us with a flexible and efficient solution.The composition of the single-page slide group chart in AnQi CMS

2025-11-06

How to display a single-page slideshow (Images) in a frontend template?

As a senior enterprise CMS website operations personnel, I know how to use the system functions efficiently to produce high-quality content and present it elegantly on the front end.How to display a slide group image (Images) in a single-page front-end template?This is the question, which is a major advantage of AnQi CMS in content display flexibility.Through the following article, I will provide a detailed explanation of how to implement this feature in front-end templates, helping you enhance the visual appeal of your website.### Understanding the storage of single-page and slide group charts In AnQi CMS

2025-11-06

If the single page does not set a description, will the system automatically extract it?

HelloAs a senior CMS website operator, I fully understand your attention to detail in content management.Regarding the setup of a single-page introduction, as well as the issue of whether the system will automatically extract content, this is indeed a frequently encountered problem in actual operation. ### Introduction to the setup logic of the single-page introduction in AnQi CMS Content management in AnQi CMS has clear hierarchy and processing rules.For daily documents (such as articles, product details) and single-page applications, there are some subtle but important differences in the handling of the "summary" field by the system.

2025-11-06

Does the single-page support the display of multilingual content?

As a website operator who is deeply familiar with the operation of AnQiCMS, I fully understand the importance of content in attracting and retaining users.This question of whether a single-page content supports multilingual display is not only related to user experience, but is also a key factor in enterprises expanding into international markets and enhancing brand influence.AnQiCMS is a system focused on providing an efficient and customizable content management solution, one of its core advantages is support for multilingualism.It is explicitly pointed out in the project positioning that the system is designed to help users efficiently carry out various activities including multilingual promotion

2025-11-06

Does AnQi CMS provide version management or recovery features for single-page content?

AnQiCMS (AnQiCMS) is an efficient enterprise-level content management system dedicated to providing users with convenient content creation, publishing, and optimization experience.After getting to know the various functions of AnQiCMS, we can discuss whether it provides version management or recovery mechanisms in single-page content management.

2025-11-06

How to associate a custom URL of a single page with the `{filename}` field in the pseudo-static rules?

In website operation, a clear, semantically structured URL is crucial for search engine optimization (SEO) and user experience.AnQiCMS provides flexible pseudo-static rule configuration, allowing operators to customize URLs according to actual needs.In which, associating a custom URL with the `{filename}` field in the static rules of a single page is a crucial step in implementing customized URLs.

2025-11-06

Does the URL alias of a single page support the automatic generation of Chinese pinyin?

As an experienced AnQi CMS website operation person, I fully understand your concern about the website URL structure, especially whether the single-page URL alias can automatically generate pinyin in Chinese.This not only concerns the SEO performance of the website, but also directly affects the user experience and content management efficiency.Based on my in-depth understanding of the AnQiCMS system and the system documentation you provided, I can clearly tell you that the single-page URL alias function of AnQiCMS supports the automatic generation of pinyin in Chinese.

2025-11-06

Will external links in single-page content be automatically filtered or added with nofollow tags?

As an expert in the operation of Anqing CMS, I know that every detail of content publishing can affect the SEO performance and user experience of the website.External links are an important part of the content, and their handling directly affects the authority transmission and risk control of the website.In Anqi CMS, how will external links in single-page content be handled, which is a focus for many operators.The AnQi CMS provides an automated and flexible mechanism for handling external links in content, mainly through the unified management of the "Content Settings" feature in the backend.

2025-11-06