How to reference static resources (such as CSS/JS/images) in AnQi CMS template to beautify content display?

Calendar 👁️ 75

In content operation, the aesthetics and user experience of the website are crucial, and this largely depends on the reasonable reference and management of static resources (such as CSS stylesheets, JavaScript scripts, and images).For users of AnQiCMS, mastering how to efficiently and规范ly reference these resources in templates is a key step in building a high-quality website.

AnQi CMS has taken full consideration of the management of static resources, providing a clear set of conventions and convenient template tags, allowing developers to easily realize the visual beauty and interaction enhancement of content.

AnQiCMS Static Resource Storage Agreement

Firstly, understanding the static resource storage structure of Anqi CMS is fundamental. According to the system convention, the styles, JavaScript scripts, images, and other static resources used by the template are all uniformly stored on the site./public/static/Under the directory. When you create or modify a set of templates, you usually will/templateCreate a dedicated folder for your template under the directory, for exampledefault), and all the static resources (CSS, JS, images, etc.) will be placed in/public/static/your_template_name/This structure. This separation helps maintain the purity of the template file, and also facilitates the efficient processing and caching of static files by the server.

Core: How to reference static resources in the template

In the Anqi CMS template files, we can use the system providedSystemThe label is used to dynamically obtain the static file address of the current template, thereby realizing the flexible reference of resources. This key variable is{{ System.TemplateUrl }}It will be parsed as the URL path of the static resource directory corresponding to the currently enabled template.

1. Refer to the CSS stylesheet

CSS files are responsible for the layout and visual style of the website. CSS is usually referenced in templates.<head>Use within tags<link>elements.

For example, if your template has a static resources directory with a file namedcss/style.cssThe style file, you can refer to it like this:

<link rel="stylesheet" href="{{ System.TemplateUrl }}/css/style.css">

This method ensures that the CSS path can be correctly resolved regardless of the domain or subdirectory where the website is deployed.

2. Reference JavaScript script

JavaScript files are used to implement page interactions. They are usually in<body>Referencing before closing the tag to avoid blocking page rendering.

If there is a script file named under your template's static resource directory namedjs/script.jsYou can refer to it like this:

<script src="{{ System.TemplateUrl }}/js/script.js"></script>

Sometimes, you may need to execute some scripts immediately when the page loads, or refer to some external libraries, in which case you can also place them inside<head>tags, but be aware of the performance impact.

3. Reference Image

Images are an indispensable part of website content, they may be decorative images from the template itself, or content images uploaded by users through the backend.

  • Default image provided by the template:For images stored in the template's static resource directory, for example:img/logo.pngYou can directly go through{{ System.TemplateUrl }}to refer:

    <img src="{{ System.TemplateUrl }}/img/logo.png" alt="网站Logo">
    
  • User uploaded images:Images uploaded by users on the backend, for example as document thumbnails or images in the content, the path is usually dynamically generated by the system and is not directly stored in the static resource directory of the template, but located in/uploads/In the directory. You can directly obtain the path to these images through the corresponding variables in the template, for example:

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

    Herearchive.LogoIt will directly output the complete image URL. Anqi CMS also providesthumbA filter that can generate thumbnails of images according to the system settings, which helps to optimize loading speed:

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

    If the document content contains images and lazy loading is enabled,archiveDetailTags also supportlazyParameters, convenient for integrating front-end lazy loading libraries.

4. Refer to external static resources (CDN)

In addition to locally hosted static resources, we sometimes also use third-party CDNs (Content Delivery Networks) to reference some public libraries or frameworks, such as jQuery, Bootstrap, font icons, etc.In this case, since the resource is not on your AnQiCMS server, you just need to use the complete URL provided by the CDN directly in the template.

For example, refer to an external CSS file from CDN:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">

Reference an external JavaScript file from a CDN:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>

For rendering mathematical formulas and flowcharts in Markdown editors, the document also mentions implementing it by introducing external scripts like MathJax and Mermaid through CDN.

Some practical tips and precautions

  • Path normalization:Always use{{ System.TemplateUrl }}To reference the static resources included in the template, this ensures the universality of the path, even if the website changes the domain name or is deployed in a subdirectory, there is no need to manually modify the template code.
  • Resource organization:Under your template static resource directory, it is recommended to create subfolders according to the type of resources, such ascss//js//img/etc., to maintain a clear file structure for easy management and maintenance.
  • Browser Caching:After modifying CSS or JS files, the page may not take effect immediately, which may be due to browser caching. You can try clearing the browser cache or adding a version number to the resource URL (for example?v=1.0.0or timestamp to force the browser to load the latest file.
  • AnQiCMS cache:In addition to browser cache, AnQiCMS may also have its own caching mechanism.After modifying the template or system configuration in the background, if the front-end page does not update in time, you can try to clear the system cache in the "Update Cache" feature in the background.
  • Performance optimization:Compress CSS and JavaScript files as much as possible, merge small CSS/JS files to reduce HTTP requests.For images, choosing the appropriate format (such as WebP) and size, and considering the use of lazy loading technology, can significantly improve page loading speed.

By following these principles and techniques, you can effectively manage and reference static resources in the AnQiCMS template, thereby creating a website that is both beautiful and efficient.


Frequently Asked Questions (FAQ)

  1. Ask: Why did the front page not take effect after I modified the CSS or JS files in the template?

    • Answer:This is usually due to browser caching. Your browser may still be loading old versions of styles or scripts. You can try clearing your browser cache or pressingCtrl + F5(Windows)/Cmd + Shift + R(Mac) Perform a forced refresh. Additionally, if AnQiCMS has enabled system caching, you may also need to log in to the backend and clear the system cache in the "Update Cache" feature.
  2. Ask: How should I organize my template static resources to facilitate management?

    • Answer:It is strongly recommended that you organize the static resources directory exclusive to your template (for example/public/static/your_template_name/Under, create subfolders according to the resource type. For example, there can becss/Store all style sheets,js/Store all JavaScript scripts,img/Store all images. This not only makes the file structure clear at a glance, but also helps with team collaboration and subsequent maintenance.
  3. Ask: Can I put all static resources on CDN? Does AnQiCMS support it?

    • Answer:AnQiCMS itself does not provide CDN integration functionality (i.e., it will not automatically push your local static files to CDN), but of course, you can manually upload your theme CSS, JS, and image static files to a CDN provider. Once the files are on CDN, you simply need to use the complete URL provided by CDN to reference these resources in the template, rather than using{{ System.TemplateUrl }}This approach helps to improve the global access speed and concurrent carrying capacity of the website.

Related articles

What is the organization method of the AnQi CMS template file to ensure the correct loading and display of content?

In AnQiCMS (AnQiCMS), the organization of template files is directly related to the correct loading, display efficiency, and subsequent maintenance convenience of website content.A clear and reasonable template structure that allows you to handle complex content display requirements with ease and greatly enhances team collaboration efficiency.The AnQi CMS uses Go language as the development foundation, its template engine syntax is similar to Django, which provides powerful and flexible tools for template developers.Understanding its core principles and following certain organizational standards will make your website operation twice as effective.

2025-11-07

How to use the Django template engine of Anqi CMS to create highly customized content display pages?

Today, with the increasing importance of digital content, how a website efficiently and beautifully presents its content is directly related to user experience and brand image.AnQiCMS (AnQiCMS) relies on its flexible and powerful Django template engine, providing infinite possibilities for highly customized content display pages.This article will delve into how to make use of the core capability of Anqi CMS to create content display pages that meet business needs and are uniquely crafted.

2025-11-07

Which website modes does AnQi CMS support to adapt to the display of content on PCs and mobile devices?

In this diverse digital age of devices, whether browsing news, shopping, or accessing services, users may visit websites through PCs, laptops, tablets, or smartphones.Therefore, whether your website content can be displayed clearly and beautifully on different screen sizes is directly related to user experience and brand image.AnQi CMS understands this core requirement and provides a variety of flexible website modes to ensure that your content is displayed in a perfect posture on any device.

2025-11-07

How does Anqi CMS provide a high-performance architecture to ensure stable display of website content under high concurrency?

In the current digital age, whether a website can stably display content under high concurrent traffic is a key indicator of whether a content management system (CMS) is excellent.For those of us who operate websites on a daily basis, focusing on user experience and the effectiveness of content marketing directly relates to brand image and business conversion.How does AnQiCMS (AnQiCMS) achieve this, still keeping the website content stable as a mountain under high concurrency?

2025-11-07

In Anqi CMS template, how are `{{variable}}` and `{% tags %}` used for displaying dynamic content and logical control?

AnQiCMS with its high efficiency and flexibility has become a good helper for content management.The presentation of website content cannot do without templates, and in the AnQiCMS template system, `{{variable}}` and `{% tag %}` are the core elements for building dynamic pages and realizing powerful functions.They each have different responsibilities, and they collaborate closely to make our website content vibrant. --- ### `{{variable}}`:The intuitive presentation of data Imagine what you want to display on the page, such as the title of an article or the name of a website.

2025-11-07

How to handle the UTF-8 encoding of Anqi CMS template to avoid garbled display of website content?

When using Anqi CMS to manage website content, you may occasionally encounter situations where乱码 appear on the page, which is usually related to the encoding settings of the template file.The AnQi CMS has specific requirements for the encoding of template files - it must use UTF-8 encoding uniformly.Understanding and properly handling this stage is the key to ensuring that the website content is displayed normally and provides a good browsing experience. Why do character codes appear乱码?

2025-11-07

How to set up a separate display template for specific documents, categories, or single pages in AnQi CMS?

In website operation, we often need to present distinctive design or functional layout for specific content (whether it is detailed documents, entire category pages, or independent single pages).AnQi CMS provides a flexible mechanism that allows users to easily specify independent display templates for this content, thereby achieving highly personalized website display effects. To understand how to set up an independent template, we first need to have a basic understanding of the template mechanism of Anqi CMS.

2025-11-07

How to create and store a mobile template in Anqi CMS to ensure the normal display of a mobile website?

Build and manage mobile templates in AnQiCMS to ensure your website displays correctly on mobile devices, you need to understand its flexible template adaptation mechanism and clear file storage rules.AnQiCMS provides various mobile adaptation modes, allowing you to choose the most suitable method according to your actual needs. ### Understanding AnQiCMS' Mobile Adaptation Mode Firstly, we need to understand the three main mobile adaptation modes supported by AnQiCMS: 1.

2025-11-07