How to correctly display images in the article content of AnQiCMS templates and support lazy loading of images?

Calendar 👁️ 59

In content operation, exquisite images can significantly enhance the attractiveness and reading experience of articles.However, image files are often large, and if not handled properly, they may slow down page loading speed, affect user experience, and even search engine rankings.AnQiCMS is well aware of this, providing a flexible way to correctly display images within the article content in templates, and supports lazy loading of images to balance aesthetics and performance.

Core: Display images in the article content

In the AnQiCMS template, we mainly rely on{% archiveDetail with name="Content" %}This powerful tag. When you insert images in the article editor in the background, whether they are uploaded local images or referenced external images, they will be stored as part of the HTML structure in the article content (Content field).

This tag extracts the content of the article editor that includes all rich text formats, such as images, text styles, etc. To ensure that these HTML codes are correctly parsed and displayed by the browser, we usually work with|safeto use the filters together.|safeThe filter's role is to inform the template engine that this content is safe HTML code, which does not require escaping and can be output directly to the page.

For example, if you want to display the article content on the article detail page, the template code might look like this:

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

In this way, all images and text formats in the article content will be displayed completely and correctly on your website page.

Improve loading efficiency: Image lazy loading

To optimize the user experience and speed up page loading, the Lazy Loading feature becomes particularly important.It allows images to start loading only when they enter the user's visible area, thereby reducing resource consumption during the initial page load, especially for articles with many images, the effect is significant.

AnQiCMS is inarchiveDetailThe tag is built-in with lazy loading support for article content images. You just need to add anContentparameter when callinglazyexample.lazy="data-src".

The purpose of this parameter is to, when AnQiCMS renders article content containing<img>tags, it will replace or move the realsrcattribute values todata-src(or any specified attribute name, such asdata-originalon it, and mergesrcLeave the attribute blank or set it to a placeholder image.

However, setting this parameter is not enough. Browsers will not automatically recognize it.data-srcAnd load the image. You also need to introduce a front-end JavaScript lazy loading library (such as lazysizes, vanilla-lazyload, etc.), which this library detectsdata-srcThe image attribute, and dynamically load it when the image enters the visible areadata-srcAssign the value againsrcThus achieving lazy loading.

Practice: Template code for image display and lazy loading

Combine both and your article's code display will become more efficient:

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

<script>
// 在这里引入并初始化您的前端懒加载 JavaScript 库
// 例如,如果您使用 lazysizes 库,通常只需要在 HTML 页面中引入其 JS 文件即可,
// 库会自动检测带有 data-src 属性的图片并进行懒加载。
// <script src="path/to/lazysizes.min.js" async=""></script>
</script>

Please note,lazy="data-src"ofdata-srcThe attribute name must match the one recognized by the front-end lazy loading library you are using. Most mainstream libraries supportdata-srcHowever, if your library uses a different attribute, please modify it accordingly.

More image optimization suggestions

In addition to lazy loading, a series of image processing options are provided in the 'Content Settings' of AnQiCMS backend, which can further optimize image performance:

  • Whether to enable Webp image format:After enabling, the system will automatically convert uploaded JPG, PNG, and other format images to a smaller WebP format, significantly improving image loading speed without losing visual quality.
  • Whether to automatically compress large images:If the size of the image you upload is too large, the system can automatically compress it to the specified width, reducing the size of the image file.
  • Do you want to download remote images:Enable this option to ensure that external images referenced in the article are also downloaded to your server, making it easier to manage and optimize, and to avoid the situation where images cannot be displayed due to the failure of external image beds.

By combining the AnQiCMS template tags,lazywith these backend optimization features, your website will be able to present to visitors at a faster speed and a smoother experience.


Frequently Asked Questions (FAQ)

Q1: I have set it in the templatelazy="data-src", but the lazy loading of images seems not to work, why is that?

A1: Only in AnQiCMS templatelazy="data-src"The parameter, which just makes the system move the real address of the image fromsrcthe attribute todata-srcProperty. To truly implement the lazy loading effect, you also need to introduce a corresponding JavaScript lazy loading library (such as lazysizes.js or vanilla-lazyload.js), and ensure that the library is properly initialized.This JS library is responsible for detecting the page indata-srcImages, and load them dynamically when they enter the visible area. Please check if your frontend has introduced this library and if its configuration is correct.

Q2: Will the images in the article content be automatically generated as thumbnails?

A2: AnQiCMS does not automatically generate thumbnail versions for images in the article content for lazy loading.lazy="data-src"The parameter is used to convert the original image'ssrcAttribute replacement to delay loading the original image. If you want to use a lower quality or smaller thumbnail as a placeholder for lazy loading, this usually requires configuration in the front-end lazy loading library or through custom development.

Q3: Besides the article content images, how are the cover image or thumbnail of the article displayed and supported for lazy loading?

A3: The cover image (Logo) and thumbnail (Thumb) are usually through{% archiveDetail with name="Logo" %}or{% archiveDetail with name="Thumb" %}Called individually. These images are not embedded in the rich text of the article content. To implement lazy loading, you need to manually adjust thesrcattribute is replaced withdata-srcFor example<img data-src="{% archiveDetail with name="Thumb" %}" class="lazyload" alt="文章标题" />Then it also relies on a front-end lazy loading JS library to identify and load.

Related articles

How to obtain and display the title and detailed content on the article detail page?

Manage website content in Anqi CMS, the article detail page is an important window for displaying core information to visitors.Whether it is corporate news, product introduction, or technical articles, clearly presenting the title and detailed content is the key to improving user experience and information communication efficiency.The Anqi CMS provides intuitive and powerful template tags, making content presentation easy and flexible. ### Understanding Anqi CMS Template Structure Anqi CMS template files are usually stored in the `/template` directory and follow a set of simple naming conventions.for the detail page of an article or product document

2025-11-08

How to declare a temporary variable in AnQiCMS template and use it to display content, improving template flexibility?

In the template creation of Anqi CMS, we often need to display various dynamic content.It is crucial to be proficient in using temporary variables to make templates more flexible and code more concise.The Anqi CMS template engine provides powerful functionality for declaring temporary variables, which helps us better organize and process data, thereby improving the efficiency and maintainability of content display.### Understanding the Value of Temporary Variables Imagine that you need to display processed data at multiple locations on the page, or that a piece of data is used repeatedly in conditional judgments, or that data obtained from a tag needs to be further processed before it can be presented

2025-11-08

How to optimize page layout and content display by using the template inheritance (`extends`) tag?

In AnQiCMS, templates are the foundation for building the appearance and layout of a website.A well-designed template not only makes the website look professional and beautiful, but also greatly enhances the efficiency of content operation and the convenience of website maintenance.Among many powerful template tags, the `extends` (template inheritance) tag is undoubtedly one of the key tools for optimizing page layout and content display.It can help us build a unified and flexible website structure, making content operation and frontend development more effortless.### Understanding `extends`: The core of template inheritance `extends`

2025-11-08

How to define and call the `macro` tag in AnQiCMS template to display reusable code blocks?

AnQiCMS provides a flexible and powerful template system, making the display of website content efficient and beautiful.In template development, in order to enhance the reusability and maintainability of code, we often encounter the need to encapsulate a commonly used code snippet so that it can be called in different places.This is when the `macro` tag becomes the key tool to achieve this goal.It allows us to define reusable code blocks, like functions in programming languages.Why do we need the `macro` tag?

2025-11-08

How to display the name, link, and hierarchy of the article category on the article detail page?

AnQi CMS is a flexible and efficient content management system that provides great freedom in content display.For a website, the article detail page not only needs to display the content of the article itself, but also the name, link, and even the hierarchical relationship of the category it belongs to, which are all important components for improving user experience, optimizing website structure, and promoting SEO.Clear categorization information can help users quickly understand the positioning of the article, facilitate their further browsing of related content, and also enable search engines to better crawl and understand the structure of the website.Next, we will discuss how to use the Anqi CMS article detail page

2025-11-08

How to retrieve and display the publish time, update time, view count, and comment count of an article?

## Mastering AnQi CMS: A Comprehensive Guide to Article Publishing and Interactive Data Display In website operation, clearly displaying the publication and update time of articles, popularity (page views), and reader feedback (number of comments) can not only effectively improve user experience but also enhance the authority and timeliness of the content.AnQiCMS (AnQiCMS) relies on its flexible template tag system to make the acquisition and display of this key information simple and efficient.This article will explain in detail how to implement these functions in Anqi CMS.### One

2025-11-08

How to display the cover image, thumbnail, or group image of an article on the article detail page?

In Anqi CMS, adding images to the article detail page is an important link to improve the attractiveness of the content and the reading experience, whether it is the cover main image, thumbnail, or multiple group images.Anqi CMS provides a flexible and intuitive way to manage and display these image resources, allowing you to easily achieve diverse visual presentations. ### The Flexible Use of Article Cover Image and Thumbnail In the article detail page, the cover image (Logo) and thumbnail (Thumb) are the most common image elements.They are usually used to represent the main visual content of an article and are widely used on list pages

2025-11-08

How to get and display the list of tags and links associated with the article in the template?

In website operation and content management, article tags (Tag) are key tools for improving content organization, user experience, and search engine optimization (SEO) effects.They can not only help users quickly find relevant content of interest, but also provide clearer content theme information for search engines, thus improving the overall performance of the website.AnqiCMS as an efficient content management system provides intuitive template tags, allowing you to easily obtain and display the list of associated tags and links in the template.

2025-11-08