How to configure AnQiCMS to enable lazy loading of images on the page?

Calendar 👁️ 77

In today's rich internet world, the importance of website loading speed for user experience and search engine optimization (SEO) is self-evident.Especially when a page contains a large number of images, how to effectively manage the loading of these images, avoid the delay caused by loading all resources at once, has become a focus of many website operators.The Lazy Load technology is the tool to solve this problem, it allows images to be loaded only when the user is about to see them, thereby significantly improving page performance.

AnQiCMS as a high-performance content management system, fully considers the efficiency of content presentation.Although it is built on the Go language, it inherently has the advantage of fast response, but it also provides flexible mechanisms to achieve lazy loading effects in image processing.Next, let's discuss how to configure AnQiCMS to enable smooth lazy loading of images on the website.

Understanding the image processing and lazy loading mechanism of AnQiCMS

In AnQiCMS, content management and template rendering are the core elements for implementing lazy loading of images. The system itself focuses on backend data processing and the flexible application of template tags, providing a key template tag parameter that allows us to load images when rendering.srcProperty is converted to other custom properties (such asdata-src)。Image lazy loading usually depends on front-end JavaScript libraries, which assign custom attributes (such asdata-src)to the value reassigningsrcAttribute, thus triggering image loading.

Therefore, to implement lazy loading in AnQiCMS, mainly two steps are required:

  1. In the AnQiCMS template, configure to set the image'ssrcThe attribute should be replaced with the custom attribute required for lazy loading.
  2. Introduce a front-end JavaScript lazy loading library that is responsible for listening to whether the image enters the visible area and performs the loading operation.

Configuration steps in detail

First step: Optimize backend image settings (auxiliary but important)

Before starting lazy loading, it is advisable to check some image optimization settings on the AnQiCMS backend, which can further improve image loading efficiency. Enter the AnQiCMS backend, find "Global Function Settings" under "Content Settings":

  • Whether to enable Webp image formatIf your website images are mainly in JPEG and PNG formats, enabling WebP conversion can significantly reduce the image size and increase transmission speed.AnQiCMS supports automatic conversion to WebP format during image upload, which is an important step for image optimization.
  • Whether to automatically compress large images: Turn on this feature and set a reasonable width (such as 800px) to avoid uploading the original size of the image, which can reduce the file size.

These settings do not directly implement lazy loading, but they ensure that even if the images need to be loaded, their file size is optimal, and they complement the lazy loading technology.

Step 2: Modify the template to support lazy loading of property conversion

AnQiCMS uses a template engine syntax similar to Django. The system provides for images inserted in the document content,archiveDetailLabel to render document details. To implement lazy loading for these images, we need to callContentfield, usinglazyparameter to specify the imagesrcthe alternative name of the property.

Assuming your lazy loading JavaScript library expects images to usedata-srcproperties instead ofsrcto store the actual image address, then in your article detail template file (usually{模型table}/detail.htmlIn or customize the document template), find the tag to render the document content and make the following modification:

{# 假设archiveContent变量存储了文档的内容 #}
{% archiveDetail archiveContent with name="Content" lazy="data-src" %}
{{archiveContent|safe}}

here,lazy="data-src"The parameter will tell AnQiCMS's template engine to render in the tagarchiveContentof<img src="...">when the tag is rendered,srcattribute renamed todata-srcwhilesrcThe property itself may be set to empty or a placeholder image (the specific behavior depends on the internal implementation of AnQiCMS, but the core is that the real URL is transferred). At the same time,|safeThe filter is required to ensure that HTML content is parsed correctly and not escaped.

Images in non-document content areas (such as list page thumbnails, category banner images, etc.)

If your list page (such as{模型table}/list.html) or other templates directly use{{item.Thumb}}/{{item.Logo}}The image will display, and these images will not be applied automaticallylazyThe parameter. You need to manually changesrcThe property todata-srcand can add a unified CSS class name for front-end JavaScript to identify and handle them.

For example, in a loop of document lists:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <img data-src="{{item.Thumb}}" alt="{{item.Title}}" class="lazyload-image"> {# 手动改为data-src并添加类名 #}
        </a>
        <h5>{{item.Title}}</h5>
    </li>
    {% endfor %}
{% endarchiveList %}

Here, we added a class to all images that need lazy loadinglazyload-imagefor easy selection by JavaScript later.

Step 3: Introduce the front-end JavaScript lazy loading library

The AnQiCMS template system is responsible for preparing image properties, but the actual lazy loading logic needs to be implemented by the front-end JavaScript.You need to choose a lightweight lazy loading library and include it in your template.Modern browsers support nativelyIntersection ObserverAPI, you can use it to write simple lazy loading logic without relying on third-party libraries.

Generally, you would place this JavaScript code in the common header file of the template (such asbase.html)的<head>or within a tag if you want to start observing quickly or</body>Before the tag (if you want to execute after the DOM is loaded).

Here is an example of usingIntersection ObserverA simple JavaScript example to implement lazy loading:

<script>
document.addEventListener("DOMContentLoaded", function() {
    // 选择所有带有data-src属性的图片,以及带有lazyload-image类的图片
    const lazyImages = document.querySelectorAll('img[data-src], .lazyload-image');

    if ('IntersectionObserver' in window) {
        let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
            entries.forEach(function(entry) {
                if (entry.isIntersecting) {
                    let lazyImage = entry.target;
                    lazyImage.src = lazyImage.dataset.src; // 将data-src的值赋给src
                    lazyImage.removeAttribute('data-src'); // 移除data-src属性
                    lazyImage.classList.remove('lazyload-image'); // 移除辅助类名
                    lazyImageObserver.unobserve(lazyImage); // 停止观察已加载的图片
                }
            });
        });

        lazyImages.forEach(function(lazyImage) {
            lazyImageObserver.observe(lazyImage);
        });
    } else {
        // Fallback for older browsers (可选,此处简化为直接加载)
        lazyImages.forEach(function(lazyImage) {
            lazyImage.src = lazyImage.dataset.src;
            lazyImage.removeAttribute('data-src');
            lazyImage.classList.remove('lazyload-image');
        });
    }
});
</script>

This code will:

  1. Wait for the page DOM content to load.
  2. Find all elements withdata-srcproperties<img>tags, as well as those we manually addedlazyload-imageclass<img>.
  3. If the browser supportsIntersection Observer, then create an observer to monitor these images.
  4. When the image enters the visible area, it willdata-srcthe value tosrcand then removedata-srcattributes andlazyload-imagethe class and stop observing the image.
  5. For those that do not supportIntersection ObserverAn outdated browser, provide an alternative solution (as shown in the example, it is loaded directly, you can also use other compatibility libraries).

Test and verify

After completing the above configuration, you can open your website and verify it through the browser's developer tools:

  1. Open Developer ToolsPress in Chrome, Firefox, and other browsersF12to open the developer tools.
  2. Switch to the "Network" (Network) tab.This will display all resources loaded on the page.
  3. Filter image resourcesIn the Network tab, select the 'Image' (Img) or 'Media' filter.
  4. Scroll the page: Observe the loading of image resources. You will find that only when the image is about to enter or has already entered your screen's visible area, the corresponding image file will appear in the list of network requests, rather than all being requested when the page is initially loaded.

If everything goes well, the images on your AnQiCMS website have successfully achieved lazy loading effects.

Summary

By combining AnQiCMS flexible template tags with front-end JavaScript, implementing lazy loading of images is not complicated.This not only effectively improves the website's loading speed and user experience, reduces unnecessary bandwidth consumption, but also greatly benefits the website's SEO performance.Remember, the image format conversion and compression on the backend is a cherry on top, while the template inlazy="data-src"The clever application and the cooperation with front-end lazy loading JavaScript are the core of image 'on-demand loading'.


Frequently Asked Questions (FAQ)

1. Why did I set up in the AnQiCMS templatelazy="data-src"and the image still loaded immediately?

AnQiCMS'lazyThe parameter is only responsible for the image tag'ssrcattribute renamed todata-srcetc., it does not itself

Related articles

How to display only articles of specific categories in the article list?

When managing website content in Anqi CMS, we often need to flexibly control the display of article lists, for example, only displaying articles under specific categories on a certain page.This not only helps to organize the content of the website accurately, but also greatly improves the browsing experience of visitors, helping them find the information they are interested in faster.AnQi CMS provides powerful template tag features, making this requirement simple and intuitive.The core of achieving this goal lies in the `archiveList` template tag of Anqi CMS.This tag is used to obtain the 'universal key' for the article list from the database

2025-11-09

Does AnQiCMS support displaying different detail page styles based on different content models?

In website operation, we often encounter such needs: different types of content, even on the same website, also need to have completely different display styles.For example, a product detail page of an e-commerce website needs to highlight product images, prices, inventory information, etc., while the blog article detail page may focus more on the reading experience, author information, and publication date.This ability to customize the style of detail pages based on content type (also known as content model) is crucial for improving user experience and content professionalism.Then, while using AnQiCMS

2025-11-09

How to use AnQiCMS to customize the display layout of the article detail page?

AnQiCMS provides a very flexible and powerful content management system, allowing us to easily customize the display layout of article detail pages.In order to enhance user experience, optimize SEO, or better showcase the brand characteristics, AnQiCMS can help us achieve these goals through its intuitive template system and rich tag features.How can we proceed specifically? We can delve into these aspects step by step.### 1. Understanding AnQiCMS template mechanism First

2025-11-09

How to display or customize the error pages (such as 404, 500) and shutdown notifications of a website?

In the operation of the website, visitors may encounter pages that cannot be found for various reasons or temporary issues with the server, and may even be unable to access the website during maintenance and upgrades.In these situations, a well-designed error page (such as 404, 500) and a friendly shutdown prompt can not only greatly improve user experience but also maintain the professional image of the website to a certain extent and search engine optimization (SEO).AnQi CMS is an efficient content management system that provides a flexible way to display and customize these important pages.###

2025-11-09

How to use template tags to dynamically display the associated Tag list in article content?

How can we make users see the related keywords or topic list in an article at a glance while browsing, so as to guide them to discover more interesting content and thus improve user experience and website stickiness, which is an important link.AnQiCMS (AnQiCMS) provides powerful template tag features, allowing you to easily display related Tag lists dynamically in article content, which is both beautiful and practical.**Understanding the Tag Function of AnQi CMS** Tags in AnQi CMS are not just simple classifications of articles

2025-11-09

How does the multi-site feature of AnQiCMS affect the display and switching of front-end content?

When using a website content management system, the multi-site feature has always been a topic of concern.It not only concerns the operational efficiency, but also directly affects the organization, display of website content, and the smooth switching experience of users between different sites.For AnQiCMS users, its powerful multi-site feature is the tool to solve these challenges.Let's delve into how the multi-site feature of AnQiCMS affects the display and switching of front-end content.One of the design初衷 of AnQiCMS is to meet the needs of users managing multiple brands, sub-sites, or multilingual content

2025-11-09

How to use AnQiCMS's pseudo-static rules to optimize the display structure of article URLs to improve SEO?

In website operation, the URL is not only the entrance for users to access content, but also an important basis for search engines to understand and crawl website content.A clear and semantically friendly URL can significantly improve user experience and website performance in search engines.One of the key factors to achieve this goal is to skillfully use pseudo-static technology.AnQiCMS as a high-efficiency and customizable content management system, fully considers the needs of SEO optimization, and provides powerful and flexible pseudo-static rule configuration functions. Today

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