How to write AnQiCMS templates according to Django template engine syntax rules?

Calendar 👁️ 73

AnQiCMS (AnQiCMS) is an efficient and customizable content management system, with its powerful template engine as the core of diversified website display.The template engine adopted by the system is highly similar to Django's syntax rules, which allows users familiar with web development to get started faster, even beginners can gradually master the skills of template writing through the guidance of this article.

Mastering the writing of AnQiCMS templates requires understanding its basic syntax structure and file organization, then delving into various functional tags and filters, and finally being able to flexibly present data on the website front-end.

Familiar with the core syntax of template engines

AnQiCMS template language mainly interacts with dynamic data and logic through two tags: variable output and logical control.

1. Variable output: double curly braces{{ }}When you need to display data from the backend on the page, such as article titles, category names, system settings, etc., you will use double curly braces{{ }}. For example, to display the title of a document, you would write{{archive.Title}}. Here,archiveis a variable name,TitleIt is an attribute of the variable. Variable names in AnQiCMS usually follow camelCase notation, starting with an uppercase letter, for examplearchive.Id/category.Link.

2. Logical control: single curly braces and percentage signs{% %}This part is used to implement complex logic such as conditional judgment, loop traversal, and including other template files in the template. Unlike variable output, logic control tags need to appear in pairs, that is,{% 标签名 %}Following{% end标签名 %}For example, a simple conditional judgment:{% if archive.Id == 10 %} 这是文档ID为10的文档 {% endif %}.

3. Comment:{# #}and{% comment %}To maintain the clarity and maintainability of template code, you can use comments to explain the code. Single-line comments use{# 这是单行注释 #}and multi-line comments use{% comment %} 这里可以写多行注释 {% endcomment %}These comments are not output to the final HTML when rendering the page.

4. Encoding and file extensionsAll template files are with.htmlSuffix, and use UTF-8 encoding uniformly. If the encoding is incorrect, the page may appear garbled.

Organization and conventions of template files

AnQiCMS has a set of conventions for the storage location and naming of template files, which helps the system automatically identify and apply the correct template.

1. Template root directory and configurationAll templates are stored in/templatethe directory. Each independent template set needs to have its own subdirectory in this directory, and the subdirectory must contain aconfig.jsonFile. This file describes the template name, version, author, type (adaptive, code adaptation, PC+mobile end), and other basic information.

2. Storage of static resourcesThe CSS styles, JavaScript scripts, images, and other static resources used in the template should be stored separately in/public/static/the directory for unified system management and optimization.

3. Common Template File NamingAnQiCMS supports two file organization modes: folder organization and flat organization. Regardless of which one you choose, the system pre-sets some default template names, and if you create these files, the system will automatically apply them based on the context:

  • Home: index/index.htmlorindex.html
  • Model Home: {模型table}/index.htmlor{模型table}_index.html(For example)article/index.htmlFor article model home)
  • Document detail page: {模型table}/detail.htmlor{模型table}_detail.html
  • Document list page: {模型table}/list.htmlor{模型table}_list.html
  • Single page detail page: page/detail.htmlorpage.html
  • Error page: errors/404.htmlorerrors_404.htmlIn addition, you can create custom templates for documents, categories, or single pages with specific IDs, such asarticle/detail-10.htmlWill be used specifically for the article detail page with ID 10, orpage/about.htmlUsed for the single page of "About Us".

4. Mobile templateIf your website needs to provide a separate template for mobile devices, you can create it in the template root directorymobile/Subdirectory. The directory structure and file naming of the mobile template are consistent with the PC side, and the system will automatically select the corresponding template according to the access device.

Flexible application of core function tags

AnQiCMS provides rich built-in tags for retrieving and displaying various data. These tags are usually used in the form{% 标签名 参数 %}and{% end标签名 %}end.

1. System and General Information

  • systemTag: Get website name{{system.SiteName}}, Website Logo{{system.SiteLogo}}, Record number{{system.SiteIcp}}And global settings.
  • contactTag: Call contact{{contact.UserName}}Phone{{contact.Cellphone}}Address{{contact.Address}}Contact information.
  • tdkTag: Used to output the current page's SEO title<title>{% tdk with name="Title" siteName=true %}</title>Keywords and description.

2. Content structure and list

  • categoryListLabel: Get the category list of articles or products. You can specifymoduleIdto get the category under a specific model, orparentIdTo get the sub-categories. For example, get all the top-level categories under the article model:
    
    {% categoryList categories with moduleId="1" parentId="0" %}
        {% for item in categories %}
            <li><a href="{{item.Link}}">{{item.Title}}</a></li>
        {% endfor %}
    {% endcategoryList %}
    
  • archiveListTags: used to get the document list, supports categorizationcategoryId, ModelmoduleId, Recommended propertiesflag, Sortingorder, Display QuantitylimitWith multiple condition filtering. It can also be used to retrieve related documentstype="related"Or to implement search functionalityq="关键词".
    
    {% archiveList articles with categoryId="1" limit="5" order="views desc" %}
        {% for item in articles %}
            <li><a href="{{item.Link}}">{{item.Title}}</a><span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span></li>
        {% endfor %}
    {% endarchiveList %}
    
  • pageListTag: Get all single page lists, commonly used to build footer navigation or sidebar links.

3. Display details page data

  • archiveDetailTags: Used on the document detail page to get the current document ID{{archive.Id}}, headings{{archive.Title}}、Content{{archive.Content|safe}}、Thumbnail{{archive.Thumb}}etc.|safeThe filter is very important here, it ensures that HTML content is parsed correctly rather than being escaped.
  • categoryDetailTag: Get the details of the current category, such as the category title{{category.Title}}Description,{{category.Description}}.
  • pageDetailLabel: Get detailed content of the current single page, such as the title of the single page{{page.Title}}、Content{{page.Content|safe}}.

4. Navigation and breadcrumbs

  • navListLabel: Build the navigation menu of the website, supporting multi-level navigation and custom navigation categories.
  • breadcrumbLabel: Automatically generate breadcrumb navigation from the current page to the homepage, improving user experience and SEO.

5. Pagination function

  • paginationLabel: WitharchiveListUsed together

Related articles

How to use AnQiCMS template tags correctly to avoid common errors?

AnQiCMS provides a powerful and flexible template system, allowing the display of website content to be highly customizable.The syntax of its template tag language draws inspiration from the Django template engine and integrates the ease of use of Blade templates, making it easy for users unfamiliar with the Go language to get started.However, to fully utilize the potential of template tags and avoid common mistakes, it is crucial to understand their correct usage and potential pitfalls.### AnQiCMS Template Tag Basics: Understanding Core Syntax and Conventions AnQiCMS template files are usually formatted with `

2025-11-07

Does the statistics code tag support loading based on user role or login status?

In website operation, data statistics is the core to understand user behavior, optimize content strategy, and improve conversion rate.However, not all user data needs to be tracked in the same way, especially for internal team members, administrators, or specific VIP users, where we may want to avoid mixing their behavioral data into public-facing statistical reports or load statistical codes for specific user groups due to privacy considerations.For AnQi CMS users, a common and very practical question is: Can we flexibly decide whether to load the page statistics code based on user roles or login status?

2025-11-07

Does AnQi CMS provide the ability to integrate statistical code required for A/B testing?

The AnQi CMS is an efficient and customizable content management system, playing an important role in website operations.When it comes to the statistical code integration capabilities required for A/B testing, although Anqin CMS itself does not directly provide an integrated A/B testing platform and traffic distribution logic, its design philosophy and rich features provide a solid foundation and high flexibility for integrating third-party A/B testing tools. Understanding this question requires looking at it from two levels: first, the embedding of statistical code, and secondly, the creation and management of page variants required for A/B testing.

2025-11-07

Does the 'Traffic Statistics and Spider Monitoring' feature consume a lot of server resources?

As website operators, we are always pursuing data growth while also being vigilant about any factors that may slow down the website speed.Especially when it comes to modules that need to collect and process a large amount of data in real time, such as traffic statistics and crawler monitoring, performance consumption is often our biggest concern.In the end, even if a feature is powerful, it is not worth it if it sacrifices the website's access speed.Then, will the built-in traffic statistics and crawler monitoring function of AnQiCMS (AnQiCMS) also cause the server to be overloaded and become a potential bottleneck for website performance?My opinion is, there is no need to worry too much

2025-11-07

What is the difference between the variable output and logic control tags in AnQiCMS templates?

In AnQiCMS template development, we often deal with two core template elements: variable output tags and logical control tags.They all serve to present dynamic content to the user, but there are essential differences in their functional positioning, syntax form, and actual effects.Understanding and mastering their similarities and differences is the key to efficiently building AnQiCMS website templates.### Variable output label (`{{ ... }}`): The direct window for data display Imagine that your website is a beautiful showcase, and the variable output label is like the merchandise displayed in the showcase

2025-11-07

How to include external HTML fragments in AnQiCMS templates?

When building website templates in AnQiCMS, we often encounter some HTML code blocks that need to be reused, such as the website header, footer, sidebar navigation, advertisement slots, or some general content modules.If each time you repeat writing these codes in different template files, it is not only inefficient but also very麻烦 to find and update them one by one when modifications are needed.

2025-11-07

How to implement page skeleton reuse through inheritance in AnQiCMS templates?

In website construction and operation, we often face a challenge: how to ensure the consistency of the overall style of the website while being able to flexibly modify the specific content of each page, and at the same time, taking into account the development efficiency and the convenience of later maintenance?AnQiCMS provides a powerful and intuitive template inheritance mechanism, which is the core of solving these problems.It's like creating a "template" for your website, allowing you to easily reuse common structures and focus on content creation.

2025-11-07

What are the application scenarios of AnQiCMS template macro functions (macro) and include tags?

In AnQiCMS template development, in order to improve code reusability and reduce maintenance costs, we often use some powerful template functions, among which 'macro function' and 'Include tag' are two great assistants.They each have unique advantages and applicable scenarios. Understanding and appropriately applying them can make template development twice as efficient and build efficient, easy-to-maintain websites.

2025-11-07