How to use the `archiveDetail` tag to implement lazy loading (LazyLoad) of images in AnQiCMS?

Calendar 👁️ 60

The implementation of lazy loading of content images in AnQiCMS mainly utilizesarchiveDetailA very practical parameter to complete. This feature aims to optimize website performance and enhance user experience, especially when there are many images in the article content, which can significantly reduce the initial page loading time.

Understanding the principle of lazy loading and its operation in AnQiCMS

Lazy loading (LazyLoad) is a common web optimization technique that refers to the deferral of loading non-critical resources on a webpage (such as large images at the bottom of the page) until these resources appear in the user's current viewport.The benefits of doing this are obvious: the page loads faster, bandwidth consumption is less, and users can see the main content of the page faster.

In AnQiCMS, article content is usually rendered througharchiveDetaillabel'sContentfield. ThisContentThe field contains all the rich text content of the article body, including the images. AnQiCMS is thisContentThe field provides a built-in lazy loading support parameter, which allows us to indicate how the system should handle these images at the template level.

UsearchiveDetailThe tag implements image lazy loading.

To enable lazy loading of images in the article content, we need to modify the template file of the article detail page. According to the template design conventions of AnQiCMS, the template of the article detail page is usually locatedtemplates/{你的模板目录}/{模型table}/detail.html.

In your article detail template, find the tag used to display the article contentarchiveDetailIt usually looks like this:

{% archiveDetail archiveContent with name="Content" %}{{archiveContent|safe}}{% endarchiveDetail %}

To enable lazy loading, we just need to add a tag to thelazyParameter, and set its value to the attribute name recognized by your front-end lazy loading library. The AnQiCMS document recommends usingdata-srcThis is the default or supported attribute of many mainstream lazy loading libraries.

The modified code will be like this:

{% archiveDetail archiveContent with name="Content" lazy="data-src" %}{{archiveContent|safe}}{% endarchiveDetail %}

Thislazy="data-src"The parameter will indicate that AnQiCMS will use it when generating HTML.ContentAll fields in<img>label'ssrcattribute is replaced withdata-src. For example, an original image tag.<img src="https://en.anqicms.com/uploads/image.jpg" alt="描述">After processing by AnQiCMS, it will become<img data-src="https://en.anqicms.com/uploads/image.jpg" alt="描述">.

Integrate the front-end JavaScript lazy loading library

It should be noted that,archiveDetaillabel'slazyThe parameter is only processed in the back-endsrcThe attribute is converted todata-src. The browser itself does not implement lazy loading. To truly achieve lazy loading effects, you still need to introduce a JavaScript lazy loading library on the front end.

The working principle of these front-end libraries is usually:

  1. They scan all elements on the page that contain:data-src(or other specified properties) without:srcproperties<img>.
  2. They listen to the user's scroll events.
  3. When these images are about to enter or have already entered the user's visible area, the JavaScript library willdata-srcthe value of thesrcProperty.
  4. Oncesrcthe attribute is assigned, and the browser will trigger the loading of the images.

You can find many excellent, lightweight frontend lazy loading libraries on the Internet, such aslazysizes.js/vanilla-lazyloadChoose a library you like and integrate it into your AnQiCMS template according to its documentation, usually before thebase.htmlthe file</body>tag.

For example, if you choose a library namedmyLazyLoad.jsa library, and it will automatically scandata-srcattribute, then you may need tobase.htmladd code similar to this:

<script src="{% system with name='TemplateUrl' %}/js/myLazyLoad.js"></script>
<script>
  // 如果你的懒加载库需要初始化,在这里进行
  // 例如:LazyLoad.init();
</script>
</body>
</html>

Make sure the JS file path is correct, and if the library needs initialization, follow its instructions.

Summary

By adding inarchiveDetaillabel'sContentUsed in fieldlazy="data-src"The parameter, combined with the front-end JavaScript lazy loading library, allows us to easily implement the lazy loading display of article content images in AnQiCMS.This can not only effectively improve the loading speed of the website, reduce the server pressure, but also provide users with a smoother browsing experience.Remember to do more testing after completing the settings to ensure that the lazy loading feature works as expected.


Frequently Asked Questions (FAQ)

  1. Why did I setlazy="data-src"But the image still loads immediately, and there is no lazy loading effect?This is usually because you have only configured in the AnQiCMS templatelazy="data-src"but have not introduced and initialized the corresponding JavaScript lazy loading library correctly on the front-end page.lazy="data-src"Just to display the image'ssrcattribute is replaced withdata-srcAnd the actual lazy loading logic needs to be implemented by the frontend JavaScript. Make sure your template (usuallybase.html) includes a lazy loading JS library, and the library can recognize and handledata-srcProperty.

  2. Can I implement lazy loading for images in other locations (such as thumbnails, Logo, etc.) as well? archiveDetaillabel'slazyThe parameter is mainly for itsContentThe field contains rich text images. For similarLogo/ThumborImagesSuch independent image fields, which directly output image URLs without going through the rich text editor. To implement lazy loading for these images, you need to manually modify their HTML output, bysrcattribute is replaced withdata-src(For example)<img data-src="{{item.Thumb}}" alt="...">Ensure that your front-end lazy loading JS library can handle these manually modified image tags.

  3. lazy="data-src"ofdata-srcCan the name of this property be customized?Can.data-srcIs a commonly used placeholder attribute name, but you can change it according to the requirements of the front-end JavaScript lazy loading library you choose or your own preference. For example, if your lazy loading library recognizesdata-originalThen you can set the parameter tolazy="data-original"The key is that the attribute names generated by AnQiCMS should be consistent with the attribute names expected by your front-end JS library.

Related articles

How to customize the URL static rules of AnQiCMS article detail page to improve SEO effect?

## Mastering SEO: How to Customize the URL Static Rules of AnQiCMS Article Detail Page In this era where content is king, the SEO effect of a website is directly related to the exposure and user access of the content.Among them, the URL structure acts as the first checkpoint for communication between a website and a search engine, and its friendliness is crucial.A clear, concise, and descriptive URL can not only help search engines better understand the page content, but also improve user experience and increase click-through rate.AnQiCMS as an enterprise-level content management system focusing on SEO

2025-11-09

How can the advanced SEO tools of AnQi CMS help improve?

In the era of intense digital marketing competition, the search engine ranking of a website directly determines its traffic and business opportunities.For many content operators and small and medium-sized enterprises, a powerful and easy-to-use CMS system is the key to achieving SEO goals.AnQiCMS, as a platform focused on efficient content management, took into account the needs of search engine optimization from the very beginning, built-in a series of advanced SEO tools, aiming to help users improve the visibility and traffic performance of their websites.

2025-11-09

How to optimize URL structure through pseudo-static rules to improve search engines' crawling and ranking display of website content?

In website operation, we all hope that our content can be better discovered by search engines and presented to users in a clear and friendly manner.An important aspect is to optimize the URL structure, making it convey the page theme and convenient for search engines to understand and crawl.The pseudo-static feature of AnQiCMS (AnQiCMS) is the key tool that helps us achieve this goal.Why Is It Important for Websites to Have Static URLs?Imagine two types of URLs: one is `/article.php?id=123&category_id=45'

2025-11-09

How to configure and display multilingual content in AnQi CMS to meet the access needs of users from different regions?

Under the trend of globalization, providing multilingual content has become a basic requirement for websites to reach a wider audience.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, providing users with an effective way to configure and display multilingual content through its powerful multi-site management and flexible template mechanism, to meet the needs of visitors from different regions.### Understanding the multilingual operation mechanism of AnQi CMS In AnQi CMS, the implementation of multilingualism is mainly divided into two levels: one is the language switching of the system backend and frontend interface

2025-11-09

Under AnQiCMS multi-site management, how to set up independent templates for different sites to display content?

It is crucial to maintain a unique style and presentation for each site when operating multiple websites, as this not only helps to build the brand but also better meets the needs of different audiences.AnQiCMS provides strong support for multi-site management, one of its core advantages is that it can flexibly set independent templates for each site, making content display more personalized.

2025-11-09

How to make full use of the flexible content model feature of AnQiCMS to detail product information

In website content operations, the product detail page is the key link between users and products, and its richness of content, clarity of structure, and flexibility of display directly affect users' purchase decisions and the conversion rate of the website.AnQiCMS (AnQiCMS) provides an ideal solution with its powerful flexible content model features, allowing us to break free from the constraints of traditional CMS fixed fields, and build highly customized, attractive product detail pages based on product characteristics and business needs.### One, Flexible Content Model

2025-11-09

How to use the “Recommended Attributes” feature of Anqi CMS to flexibly display specific content on the front page?

When operating a website, we always hope to prioritize the most important and popular content for visitors, so that the focus area of the website can always present the freshest and most attractive information.Manually updating this content is time-consuming and prone to errors. Fortunately, Anqi CMS provides a very practical 'recommended attribute' feature that allows us to easily achieve flexible content display and automated management.What is the "recommended attribute" feature of Anqi CMS?In simple terms, 'Recommended Properties' is like putting a special tag on your website content

2025-11-09

How to customize the Anqi CMS content model to meet the personalized display needs of different types of content?

Today, with the increasing sophistication of content operation, personalized display of website content has become a key to attracting users and enhancing user experience.AnQiCMS (AnQiCMS) provides us with powerful tools with its flexible content model, allowing us to customize the structure and display of content according to various business needs.This article will delve into how to flexibly use the content model in Anqi CMS, making your website content more distinctive and practical.

2025-11-09