How to efficiently refer to external static resources (CSS, JS, images) in AnQiCMS template files?

Calendar 👁️ 85

When building a website with AnQiCMS, the design of the template and the effective reference to static resources are key to ensuring website performance and maintenance convenience.The loading speed and user experience of a website largely depend on whether its CSS style, JavaScript script, and image resources can be efficiently transmitted to the visitor.AnQiCMS provides a clear and practical mechanism for handling these external static resources, allowing us to focus more on content creation rather than tedious technical details.

AnQiCMS follows a clear convention in static resource management: all template files are stored in the root directory./templateIn the folder, and the style sheets (CSS), JavaScript scripts (JS), and static resources such as images related to these templates are placed uniformly in/public/static/Under the directory. This separation structure not only makes the project directory well-organized, but more importantly, it provides a foundation for dynamic resource referencing.Follow such a directory specification can greatly simplify the development process and help with team collaboration.

The core of referencing static resources lies in using the AnQiCMS provided{% system with name="TemplateUrl" %}The tag can dynamically obtain the base path of the static resources of the current template, thus avoiding potential problems caused by hardcoding absolute or relative paths in the code.Imagine if your website needs to change themes or be deployed to a different domain. Hard-coded paths would mean a lot of modification work. And withTemplateUrlThese paths will be automatically adjusted according to the system configuration, greatly enhancing the flexibility and maintainability of the template.

CSS file reference

When we need to introduce a stylesheet for a website, we can place the CSS file in/public/static/css/the directory. In the template's<head>section, we can reference it like this:

<link href="{% system with name="TemplateUrl" %}/css/style.css" rel="stylesheet">

TemplateUrlIt will automatically parse similarhttp://yourdomain.com/static/or/static/paths, thus loading correctlystyle.cssfiles. This method ensures that the stylesheet can be loaded accurately and correctly under any deployment environment.

JavaScript file reference

Similarly, JavaScript script files are usually stored in/public/static/js/directory. When referencing in a template, they are generally placed in<body>The bottom of the label to avoid blocking page rendering:

<script src="{% system with name="TemplateUrl" %}/js/main.js"></script>

In addition to local JS files, AnQiCMS also supports direct referencing of external CDN resources, which is very convenient when introducing commonly used libraries such as jQuery and Bootstrap. You can take advantage of the global distribution advantage of CDN to accelerate resource loading. For example, when supporting mathematical formulas or flowcharts in Markdown content, you may be inbase.htmlInclude CDN libraries such as MathJax or Mermaid:

<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

This method is a direct reference, no need to go throughTemplateUrl.

Image resource reference

The reference to image resources is more diverse. For images that are part of the template design, such as logos, background images, or UI icons, we can place them in/public/static/images/In the directory and throughTemplateUrlto refer:

<img src="{% system with name="TemplateUrl" %}/images/logo.png" alt="网站Logo">

And for the images uploaded by users through the backend editor, AnQiCMS will store them in/uploadsUnder the directory. The paths of these images are usually stored directly in the database, and their complete paths can be used directly when referenced. For example, the images in the article details:

<img src="{{archive.Logo}}" alt="{{archive.Title}}" />

AnQiCMS also provides practicalthumbA filter used to automatically generate thumbnails for images. This is crucial for responsive design and improving page loading speed.When you need to display a thumbnail version of the image, you can use it like this:

<img src="{{ item.Logo|thumb }}" alt="{{item.Alt}}" />

This filter will automatically process images according to the thumbnail rules set in the background and return the URL of the thumbnail, greatly simplifying the development work of adapting images to different display sizes.

War tactics and **practice

In order to further improve the efficiency of static resource references and website performance, it is recommended that you:

  • Maintain a clear directory structure:In/public/static/Create such as under the directory,css/js/imagesSub-folders should be created, and an independent static resource directory should be established under each template theme (for example/public/static/default/css), so that it is convenient for management and theme switching.
  • Using AnQiCMS image processing feature:Fully utilize the image auto-compression, WebP format conversion, thumbnail generation, and other built-in tools provided in the "Content Settings" of the background. These built-in tools can significantly reduce the size of image files, thereby speeding up page loading and automatically applying themthumbfilter.
  • Reasonably utilize CDN:For general and low-frequency update libraries (such as fonts, icon libraries, and commonly used JS frameworks), it is recommended to use public CDNs to alleviate server pressure and improve global access speed.
  • Pay attention to the cache strategy:Although AnQiCMS may have already handled some cache optimization on the backend, in actual deployment, it is necessary to configure reasonable HTTP cache headers through a web server (such as Nginx) or add version numbers/hashes to the filenames of static resource files (such asstyle.css?v=20231027It can effectively utilize browser cache, reducing unnecessary requests.

By understanding and applying the features and conventions provided by AnQiCMS, we can manage the external static resources of the website more efficiently, and build a website with excellent performance and easy maintenance.


Frequently Asked Questions (FAQ)

1. Why is my CSS or JS file showing a 404 error in the browser?

This is usually due to an incorrect path reference. Please first check if your CSS or JS files are placed correctly in/public/static/the corresponding subfolder under the directory (for example/public/static/css/style.css)。其次,确认在模板中引用时,您是否使用了{% system with name="TemplateUrl" %}标签来构建路径,例如<link href="{% system with name="TemplateUrl" %}/css/style.css" rel="stylesheet">When entering the path manually, a small spelling mistake may also cause the resource to fail to load.

How can I ensure that the images I upload also load efficiently, such as by creating thumbnails or converting to WebP format?

AnQiCMS provides powerful image processing features in the background "Content Settings".You can enable 'whether to start Webp image format' to automatically convert uploaded JPG/PNG images to more efficient WebP format;Turn on "Auto-compress large images" and set a specified width to optimize image size.Display these images in the template, and use{{ item.Logo|thumb }}such as this.thumbFilter, AnQiCMS will automatically generate and provide optimized thumbnails for you.

3. If my static resources are not/public/static/in the directory, can I still reference them?

In theory, it is possible to reference it through some advanced configurations (such as the reverse proxy rules of the web server), but this does not comply with the recommended practice of AnQiCMS. To maintain the clarity of the website structure, ease of maintenance, and make full use of the built-in optimization mechanisms of AnQiCMS, it is strongly recommended that you place all static resources related to the templates in a unified location./public/static/Under the directory. This can avoid unnecessary configuration complexity and ensure that the system can dynamically and efficiently manage these resources.

Related articles

How to implement code adaptation or independent site mode display for the mobile and PC templates of AnQiCMS?

When using AnQiCMS to build a website, we often encounter a core issue: how to ensure the website has an excellent browsing experience on different devices, whether it's a wide PC screen or a small mobile screen?AnQiCMS provides us with flexible and diverse solutions, mainly focusing on adaptive, code adaptation, and independent sites for PC and mobile, which help us achieve this goal.First, let's take a look at each of these patterns and their implementation in AnQiCMS.### 1. Adaptive Mode (Responsive

2025-11-07

How to create and manage multiple website templates for AnQiCMS and achieve differentiated display on different pages?

AnQiCMS provides high flexibility in the creation and management of website templates, allowing users to easily achieve differentiated display of different pages, thereby meeting diverse content operation needs.This is due to its concise and efficient architecture and support for various template patterns. ### Understanding AnQiCMS Template Mechanism The core of AnQiCMS templates lies in its easy-to-use Django template engine syntax, which allows even users without a strong development background to customize templates relatively easily.All template files are stored in a unified location

2025-11-07

How to customize the front-end page layout of the AnQiCMS website to meet brand requirements?

Customizing the front-end page layout in AnQiCMS is a key step to creating a brand image and providing a unique user experience.This system relies on its flexible template engine, rich functional modules, and user-friendly design concept to provide powerful customization capabilities for website operators. ### AnQiCMS Foundation for Layout Customization: Template System Overview AnQiCMS uses a template engine similar to Django syntax, which means the visual presentation of the website is separated from the core business logic.All elements of the front-end page, from overall structure to content display

2025-11-07

Does the `addslashes` filter in the future version of AnQiCMS add more configurable escape options?

AnQiCMS provides flexible and powerful features in content management and template rendering. The future development direction and configurability of details in data processing, such as the `addslashes` filter, are issues of concern for many users. Currently in the AnQiCMS template system, the `addslashes` filter is a basic string processing feature, which adds a backslash before the specified predefined characters.According to the document, these characters mainly include single quotes (`'`)

2025-11-07

How to ensure that the encoding format of AnQiCMS template files is correct and avoid display of garbled characters on the page?

In website operation, the display of乱码is undoubtedly one of the most annoying problems, it not only severely affects user experience, but may also damage your brand image.For friends using AnQiCMS, encountering such problems, the encoding format of the template file is often the primary object to investigate.Ensure that your AnQiCMS template files use the correct encoding format, which is the basis for ensuring the normal display of website content and providing a smooth user experience.

2025-11-07

How does AnQiCMS utilize modular design for template secondary development and personalized display adjustment?

AnQiCMS is an enterprise-level content management system based on the Go language, and its powerful modular design is one of its core advantages.This design concept is not only reflected in the system architecture, but also deeply affects the flexibility of secondary template development and personalized display adjustment, allowing website operators to easily create unique and highly customizable content platforms. **The Foundation of AnQiCMS Modular Design** AnQiCMS can achieve efficient and flexible template customization, which is inseparable from its underlying modular architecture.

2025-11-07

How to flexibly display personalized field content on the front-end page after customizing the content model?

With the increasing diversity of modern website content, relying solely on traditional content types such as 'articles' or 'products' often fails to meet complex business needs.The AnQiCMS custom content model feature is specifically designed to address this challenge, allowing us to create highly customized content structures based on actual business scenarios.But how can we carefully design these personalized contents in the background and then flexibly and efficiently present them on the website front-end?This requires us to deeply understand the powerful template tag system of AnQi CMS.###

2025-11-07

Does AnQiCMS support customizing the display style and content of 404 and 500 error pages?

In website operation, every detail of user experience is crucial, even when users accidentally visit a non-existent page (404 error) or when there are internal issues with the server (500 error), a well-designed, friendly error page can effectively reduce user loss and even guide them back to the correct path.AnQiCMS (AnQiCMS) fully understands this and provides users with a flexible way to customize these critical error pages, ensuring that the website maintains a professional and consistent user experience in any situation.### Core Feature Revelation

2025-11-07