How to reference and display external static resources (such as CSS, JS) in the template?

Calendar 👁️ 65

In AnQiCMS templates, referencing and displaying external static resources such as CSS stylesheets and JavaScript scripts is a key step in building a functional and visually appealing website.Understand its working principle and recommended practices, which can make our website development work more efficient and flexible.

Location of static resources

When using AnQiCMS to build a website, style sheets (CSS), JavaScript scripts (JS), and images, among other static files, are an indispensable part of构成网站视觉和交互体验。AnQiCMS has clear conventions for organizing these static resources: all static files should be stored in your AnQiCMS installation directory under/public/static/in the folder.

For example, if your CSS file is namedstyle.css, you would place it in/public/static/css/style.css. Similarly, a JavaScript file likescript.jscould be located in/public/static/js/script.js. This/public/static/The directory, after your website is deployed, will be directly mapped to the root directory of your website through a web server (such as Nginx or Apache), which means they can be accessed directly through the URL.

Refer to local static resources in the template

When you need to incorporate these locally stored static resources into the AnQiCMS template, the system provides a very convenient tag to help us dynamically generate the correct path, that isTemplateUrl. ThisTemplateUrlThe tag will automatically obtain the root path of the template file based on your website configuration, ensuring that your resource links are always correct, regardless of where your website is deployed in any subdirectory or subdomain.

Reference CSS stylesheet:

To reference a CSS file, it is usually in the template's<head>area<link>tags, and combine{% system with name="TemplateUrl" %}Use the tag to construct the path.

Assuming your CSS file is in/public/static/css/main.cssIn the template, you can write it like this:

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

here,{% system with name="TemplateUrl" %}It will be replaced with the URL of the root directory of your template, then added with/static/css/main.csswhich will point to your style file.

Reference JavaScript script:

Similarly, the introduction of JavaScript files is usually in<head>or<body>Used before the end tag<script>.

If your JS file is in/public/static/js/app.jsThe template reference method is as follows:

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

If you want to build a more complex URL directly in the template code, you can also first assign it toTemplateUrla variable and then use it, for example:

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

Reference external CDN static resources

In addition to local resources, we often also use static resources provided by external CDNs (Content Delivery Networks), such as some popular front-end frameworks (such as Bootstrap, jQuery) or specific library files.

Referencing CDN resources is more direct, as they usually provide complete URL addresses. You just need to put these URLs directly into<link>or<script>the tag.

For example, to include a GitHub Markdown stylesheet, you can do it like this:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />

Introduce JavaScript libraries such as MathJax or Mermaid and follow the same pattern:

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

Some practical tips

  • Organize files logically:To keep the project tidy, it is recommended to/public/static/Create in the directorycss/js/imagesetc. subfolders, and further subdivide according to function or module, for example,css/pages/home.cssorjs/modules/slider.js.
  • Handle cache issues:After developing or updating static resources, browsers may sometimes display old versions due to caching. In addition to clearing the browser cache, you can also force the browser to reload by adding a version number or timestamp to the resource link.

Related articles

How to reflect the high concurrency characteristics of the Go language in the front-end page loading speed?

In today's fast-paced online world, website loading speed has become a core factor in user experience and search engine rankings.Nobody likes to wait for a slow page, and search engines tend to rank websites that respond quickly higher.How can you make your website lightning fast?For users of AnQiCMS, the answer is hidden in its powerful Go language underlying architecture, especially the high concurrency features that Go language boasts.

2025-11-07

How to dynamically display the current year or current time in a template?

In website operation, we often need to dynamically display the current year or time, such as displaying the latest copyright year at the bottom of the website, or marking the generation time of the page accurately in articles.AnqiCMS provides flexible and powerful template functions, making these operations very simple.Next, we will discuss how to implement this requirement in the AnqiCMS template.

2025-11-07

How to safely display rich text content containing HTML tags on the front-end page?

In website operation, we often need to display rich content that includes images, links, bold text, and so on, which is known as rich text content.But it is a practical and careful task to safely display these rich text with HTML tags on the front-end page.AnQi CMS as an efficient content management system, fully considers this point, and provides us with a clear solution.

2025-11-07

How to use logical tags like `if` and `for` in templates to control conditional and repeated display of content?

In website content management, displaying dynamic and diverse content is the key to improving user experience and website attractiveness.AnQiCMS (AnQiCMS) provides a powerful template system, drawing inspiration from the Django template engine syntax, allowing you to easily control the display of content through logical tags.Among them, the `if` tag is used for conditional judgment, while the `for` tag is used for loop iteration, they are the foundation for building flexible and variable web pages.

2025-11-07

How to define and use variables for content display in AnQiCMS templates?

AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and flexible template mechanism, making content presentation both powerful and intuitive.In website content operation, we often need to display dynamic information, such as article titles, product prices, contact information, etc., which is inseparable from defining and using variables in templates.Understanding how to flexibly use variables in the AnQiCMS template is the key to building and maintaining an efficient, personalized website.The AnQi CMS template engine has borrowed the syntax from Django, making it very easy to understand the definition and reference of variables. Essentially

2025-11-07

How to categorize image resources and display them on the front end according to categories?

Images are an indispensable part of a website's content, as they can not only beautify the page but also effectively convey information and enhance the user experience.However, with the increase in the number of images on a website, how to efficiently manage these image resources and flexibly display them on the front page as needed has become a challenge for many website operators.AnQiCMS (AnQiCMS) is well-versed in this field, providing a user-friendly and powerful image resource management system that helps us easily manage these visual elements.

2025-11-07

How to configure 301 redirect rules to avoid SEO impact after content adjustment?

Website content operation is a continuous job, with the development of the business, content updates, adjustments, and even optimization of the website structure can lead to changes in page URLs.However, if these URL changes are not handled properly, they often have a significant negative impact on the website's search engine optimization (SEO), such as a drop in rankings, loss of traffic, and so on.This is when the 301 redirect becomes a key tool for us to avoid these negative impacts and maintain the SEO health of the website.### Understanding the value of 301 redirect In simple terms, 301 redirect (Moved

2025-11-07

How to organize reusable display modules in a template through `include`, `extends`, and other tags?

Aq enterprise CMS template: Use `include` and `extends` to create efficient and reusable display modules When using Aq enterprise CMS to build a website, you will quickly find that it is very flexible in organizing content display.Especially when dealing with templates, the built-in Django template engine syntax allows us to build pages efficiently like building with blocks.Today, let's talk about how to cleverly use the powerful tags `include` and `extends` to organize and manage reusable display modules on your website, making your templates cleaner and easier to maintain

2025-11-07