How to effectively organize and reference website static resources (CSS/JS/images)?

Calendar 👁️ 95

Building and managing websites in AnQiCMS (AnQiCMS), efficient organization and referencing of static resources (CSS styles, JavaScript scripts, images, etc.) is the key to improving website performance, ease of maintenance, and user experience.AnQi CMS provides clear guidelines and practical features to help us easily handle these challenges.

A clear and orderly directory structure: the way to organize static resources

Good file organization is the foundation of efficient management of static resources.In AnQi CMS, the system has planned a unified and logically clear storage location for static resources, which greatly simplifies our management work.

All static resources such as styles, scripts, and images related to templates should be stored uniformly in the root directory of the website/public/static/In the catalog. This centralized management method is not only convenient for searching and maintenance, but also in line with the practice of web development.

Under this core directory, we can create more detailed subdirectories as needed to further classify:

  • CSS files:It is recommended to createcss/Subdirectory, storing all style sheet files of the website, for examplestyle.css/responsive.cssetc.
  • JavaScript files:It is recommended to createjs/Subdirectory, used to store all JavaScript script files, such asmain.js/jquery.min.jsetc.
  • Image file:It is recommended to createimages/Subdirectory for storing various images required by the website, such as Logo, banner image, content illustrations, etc.

When our website needs to support mobile adaptation and has adopted a separate template mode for PC and mobile, Anqi CMS also provides additional convenience. For mobile-specific static resources, we can store them in/public/static/mobile/In the catalog. This separation helps us to finely control the resource loading on mobile devices, ensuring lightweight and fast response. For example, the style sheet for mobile devices can be placed inpublic/static/mobile/css/, the script is placed inpublic/static/mobile/js/, the picture is placed inpublic/static/mobile/images/.

It is worth mentioning that the template files of Anqi CMS are stored in/templateIn each independent template directory under the root. Even if these template files (such as headers, footers, and other common code snippets) are stored inpartial/directory, or page skeletonbash.html)Need to refer to static resources, which should still follow the above rules and be placed in/public/static/or its subdirectory. The template file is only responsible for referencing and does not store the static resources themselves.

Flexible and secure referencing method: Make resources accessible

After the storage location of static resources is clarified, how to correctly and efficiently refer to them in templates is particularly important.AnQi CMS provides flexible tag syntax to ensure that we can dynamically and safely reference these resources.

1. Dynamically retrieve the base path:When referencing static resources in templates, we should not directly hardcode absolute paths or relative paths, as the domain name or subdirectory of the website may change. Anqi CMS provides a very practical system tag to dynamically obtain the root path of static resources: {% system with name="TemplateUrl" %}This tag will return the address of the static files corresponding to the current website template, for examplehttp://yourdomain.com/public/static/orhttp://yourdomain.com/template/your_template_name/(Depends on the specific configuration of AnQiCMS, but it will usually point to/public/static/)

Using this tag, we can<head>In the tag, include the CSS file, in<body>Include the JavaScript file before the end, for example:

<!-- 引入 CSS 样式表 -->
<link rel="stylesheet" href="{% system with name="TemplateUrl" %}/css/style.css">

<!-- 引入 JavaScript 脚本 -->
<script src="{% system with name="TemplateUrl" %}/js/main.js"></script>

This reference method ensures that even if the website address or template directory structure is adjusted, static resources can still be loaded correctly.

2. Reference of image resources:Images are an important part of a website's content, and there are many ways to reference them:

  • Images uploaded directly:If the images are manually uploaded to/public/static/images/The directory can be directly referenced using the dynamic path tag above:<img src="{% system with name="TemplateUrl" %}/images/logo.png" alt="网站Logo">

  • Image uploaded through the content editor:When we upload images through the backend rich text editor in articles, product details, or single pages, Anqicms will automatically handle the storage and paths of these images. They are usually stored in/uploads/Under the directory, and the system will automatically generate the correct access path. We just need to insert the image, no need to manually adjust the path.

  • Images referenced by specific fields (such as Logo, Thumb, Images):The Anqi CMS provides special image fields for documents, categories, single pages, and models such asLogo(Cover first image/large image),Thumb(Cover thumbnail) andImages(Group photo). These fields store the image paths that can be directly accessed in the template through corresponding tags. For example, on the document detail page:<img src="{{ archive.Logo }}" alt="{{ archive.Title }}">Or use:archiveDetailTags:<img src="{% archiveDetail with name="Logo" %}" alt="{% archiveDetail with name="Title" %}">

3. Image optimization and lazy loading:To further improve loading speed, we can take advantage of the image optimization features and lazy loading mechanism provided by Anqi CMS:

  • Thumbnail filter:For images referenced by fields, if you need to dynamically generate thumbnails, you can usethumbFilter:<img src="{{ item.Logo|thumb }}" alt="{{ item.Title }}">This will automatically generate and reference the corresponding thumbnail version under the thumbnail rules set in the background.
  • Lazy loading:For images with a large number or size on a page, lazy loading can effectively reduce the pressure of the first load. Anqi CMS'sarchiveDetailtags during outputContentsupportinglazy="data-src"Parameter, this will load the imagesrcattribute is replaced withdata-srcWith the help of a front-end lazy loading library to implement on-demand loading:{% archiveDetail archiveContent with name="Content" lazy="data-src" %}{{archiveContent|safe}}

3. Content Optimization Strategy:全面提升website performance

In addition to the organization and referencing of static resources, the Anqi CMS backend also provides powerful content settings functionality to help us optimize static resources from the source, and comprehensively improve website loading speed and user experience.

  • Image auto processing:
    • Convert to WebP format:In the 'Content Settings', we can choose whether to enable the Webp image format.After turning on, uploaded JPG and PNG images will be automatically converted to a smaller WebP format, significantly reducing image loading time.
    • Automatically compress large images:Similarly, in the "Content Settings", we can turn on the "Whether to automatically compress large images" and set the "Auto-compress to a specified width".This is very helpful in avoiding the upload of large images, saving storage space, and improving loading speed.
    • Unified thumbnails:We can set up the website

Related articles

What are the conventions for naming and storing AnQi CMS template files?

When you start using Anqi CMS to build a website, understanding the naming rules and storage path conventions of its template files is the key to efficient website design and content presentation.A clear file structure can not only help you quickly locate and modify templates, but also make team collaboration smoother, ensuring stable operation of the website. ### The "home" of template files: unified storage location All template files of AnQi CMS are stored centrally in the `/template` folder under the project root directory.This is like a large library, where all books (template topics) are collected here

2025-11-08

How can I customize the content display layout of the AnQi CMS website?

The layout of website content display is a key factor in attracting visitors, enhancing user experience, and optimizing search engine rankings.A clear, beautiful, and functional layout that can make your website stand out among competitors.AnQiCMS (AnQiCMS) is an efficient content management system that provides high flexibility, allowing you to customize the display of website content according to your actual needs.This article will deeply explore how to customize the content display layout of the Anqi CMS website, helping you turn your creativity into reality and create a website with a unique style.--- ###

2025-11-08

How to use a for loop to traverse an array and handle an empty array in Anqi CMS template

During the template development process of AnQi CMS, dynamic data display is one of the core requirements.No matter whether it is an article list, product display, or navigation menu, we cannot do without traversing array or collection data.How to elegantly handle empty data in this process, ensuring the beauty of the page and user experience is equally important.The AnQi CMS template engine supports powerful features similar to Django template syntax, among which the `for` loop is a powerful tool for handling data lists.

2025-11-08

How to use if/else condition judgment to control content display in Anqi CMS template?

In AnQi CMS template creation, flexibly controlling the display of content is the key to improving website user experience and management efficiency.Whether it is to display specific information based on different conditions, or to implement more complex personalized layouts, conditional judgment is an indispensable tool.Today, we will delve into how to use the powerful `if/else` conditional judgment in Anqi CMS templates to accurately control the presentation of content. The template engine of AnQi CMS adopts syntax similar to Django template syntax, which allows web developers familiar with web development to quickly get started. Among them

2025-11-08

How are the syntax rules of template variables and tags similar to Django?

When building and customizing the AnQiCMS website, the template plays a core role.AnQiCMS's template system draws on the power and simplicity of the Django template engine, which means that whether you are new to it or already familiar with other modern template languages, you can quickly get started.The syntax rules revolve around how to effectively display dynamic data and control page logic, making the website content flexible and organized.Understand the core of the AnQiCMS template system, you first need to distinguish between two main elements: variables and tags.

2025-11-08

How to ensure that the encoding of the template file is correct and avoid garbled characters on the front-end page?

The website front-end page displaying garbled characters is undoubtedly one of the most headache-inducing problems.When visiting a website, the originally clear and readable text turns into a pile of undecipherable symbols, which not only severely affects the user experience but also makes the website look unprofessional.For friends using AnQiCMS to build and manage websites, ensuring the correctness of the template file encoding is a key step to avoid such issues.

2025-11-08

What kinds of responsive or adaptive template display modes does AnQiCMS support?

In today's mobile internet age, the user experience of websites is no longer limited to computer screens.Whether it is a smartphone, tablet, or various sizes of displays, the website must be presented in **status**.AnQiCMS understands this and therefore offers a variety of display options in the template to ensure your content reaches every visitor seamlessly. ### One, Adaptive Template Mode (Responsive) Let's talk about the most common "adaptive template" mode first.The core of this pattern lies in 'adaptability', which allows the website to 'change with no change'

2025-11-08

How to design and deploy a template independently for mobile website?

With the popularity of smartphones, the user experience of mobile websites has become a key factor in determining the success of a website.A well-designed mobile website can not only effectively enhance customer satisfaction, but is also crucial for search engine optimization (SEO).AnQiCMS fully considers this requirement, providing flexible and diverse mobile template solutions, especially supporting independent design and deployment of mobile template, so that your website can show its posture on different devices.### Get to know AnQiCMS's mobile template strategy In AnQiCMS

2025-11-08