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

Calendar 👁️ 72

When building and customizing the AnQiCMS website, the template plays a core role.AnQiCMS template system has borrowed 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 well-organized.

To understand the core of the AnQiCMS template system, it is first necessary to distinguish between two main elements: variables and tags.

Display of dynamic data: template variables

In the AnQiCMS template, you will see data enclosed in double curly braces{{ 变量名 }}The form appears.These are template variables, they are placeholders for various dynamic data stored in your website backend management system.When the page is accessed, AnQiCMS will replace these variables with the actual content.{{ archive.Title }}It may display the title of an article, and{{ system.SiteName }}It will display the global name of your website.

To maintain clarity and consistency, AnQiCMS variables are usually named following the camelCase convention, which means the first letter of each word is capitalized, for exampleArchive.Id/Archive.TitleThis specification helps to improve the readability of the template.

In addition, the values of these variables can also be further processed through 'filters'. Filters are applied through the pipe character.|Appended to the variable, for example{{ 对象 | 过滤器名称:参数 }}They can format, truncate, or convert variables, for instance{{ archive.CreatedTime | stampToDate("2006-01-02") }}You can format timestamps to dates,{{ archive.Content | safe }}It indicates that the template engine safely renders HTML content without escaping.

Controls page logic and data retrieval: template tags

Variables are different, template tags use curly braces and percent signs{% 标签名 参数 %}Define it.They are functional code blocks used for executing logic operations, obtaining data sets, referencing other template files, and so on.{% if 条件 %}...{% endif %}or{% for 循环变量 in 数据集合 %}...{% endfor %}.

  1. Control flow tags:The AnQiCMS template provides powerful logical control capabilities, allowing you to display different content based on different conditions, or iterate through data sets.

    • Conditional judgment (if/elif/else/endif):Similar to conditional statements in programming languages,{% if 条件 %}Allow you to render part of the content based on the truth value of the expression. You can also use{% elif 其他条件 %}and{% else %}to handle multiple branch conditions, and finally, in order to{% endif %}end.
    • Loop traversal (for/empty/endfor):When you need to display list data,{% for item in 集合 %}The label comes into play. It will iterate over each element in the collection and allow you to access it within the loop bodyitem(or any other variable name you define). This label also provides some convenient auxiliary variables, such asforloop.CounterUsed to get the current loop index (starting from 1),forloop.RevcounterIt indicates the remaining number of loop iterations. When the collection is empty, you can use{% empty %}a clause to display a default content instead of an empty loop. You can even addreversedorsortedKeywords to change the traversal order.
  2. Data acquisition tags:AnQiCMS has built-in multiple tags, specifically used to fetch specific types of data from the background.

    • Content list and details (archiveList/archiveDetail):This is one of the most commonly used tags.{% archiveList archives with type="page" categoryId="1" limit="10" %}Can retrieve the list of articles or products under a specified category, and supports pagination.{% archiveDetail with name="Title" id="1" %}Then you can retrieve the detailed information of a specific ID article, such as title, content, etc. They usually have parameters likemoduleId(Model ID),categoryId(Category ID),idContent ID,nameField name,typeList type such as page/list/related,limitNumber limit andorderSorting method.).
    • Category information (categoryList/categoryDetail):Similarly,{% categoryList categories with moduleId="1" parentId="0" %}Can get the website's category structure,{% categoryDetail with name="Title" %}It is used to display the title or description of the current category.
    • Site configuration (system/contact/tdk):These tags are used to obtain the global configuration of the website, such as{% system with name="SiteName" %}Get the website name,{% contact with name="Cellphone" %}Get the contact phone number,{% tdk with name="Title" %}Get the page TDK information.
    • Navigation menu (navList): {% navList navs %}Tags can conveniently retrieve the website navigation menu configured on the background, supporting multi-level structure.
    • Pagination feature (pagination):When配合archiveListUsetype="page"then,{% pagination pages with show="5" %}The label will automatically generate a beautiful pagination navigation.
  3. Auxiliary label:

    • Template reference (include):To modularize, you can use{% include "partial/header.html" %}Insert a template fragment into the current template. This helps improve the reusability and maintainability of the template.
    • Template inheritance (extends/block):AnQiCMS supports template inheritance by{% extends 'base.html' %}you can specify a parent template, then by{% block 区块名 %}...{% endblock %}Rewrite the specific content block of the parent template. This is very useful for creating a unified layout for websites.
    • Macro function (macro):Similar to functions in programming,{% macro my_function(param1, param2) %}...{% endmacro %}You can define reusable code blocks to reduce repetitive writing.
    • Variable definition (with/set): {% with my_var="some_value" %}or{% set another_var = "another_value" %}It allows you to define temporary variables in templates for local use convenience.
    • Timestamp formatting (stampToDate):This is a very useful built-in function,{{ stampToDate(时间戳, "2006-01-02 15:04:05") }}It can convert Unix timestamp to a human-readable date and time format.

Template Creation Conventions

Following some basic conventions during AnQiCMS template creation can make your work smoother:

  • File Format:Template files are usually named with.htmlas a suffix, and placed uniformly on the site/templatedirectory.
  • Static resources:Styles, scripts, images, and other static files referenced in the template, it is recommended to place uniformly in/public/static/In the catalog.
  • Encoding:All template files should use UTF-8 encoding to avoid garbled text.
  • Clean up whitespace:Sometimes, for code readability, tags may be placed on a separate line, but this may cause unnecessary blank lines during rendering. You can add a hyphen after or before the tag's%symbol.-Remove spaces, such as{%- if condition %}or{% endfor -%}.

In summary, AnQiCMS provides a set of powerful and easy-to-learn tools for website customization and content presentation, making it intuitive and efficient.Master these Django-like syntax rules, and you will be able to navigate AnQiCMS more freely, creating websites with rich features and excellent user experience.


Frequently Asked Questions (FAQ)

1. How to obtain custom field information in the content model within a template?

The AnQiCMS content model allows you to add custom fields for different types of content (such as articles, products). If you want to display these custom fields in the template, you can usearchiveDetailor labelarchiveParamsFor example, if you have customized a namedauthorfield, you can access it directly through{{ archive.Author }}to access. To iterate over multiple custom fields, you can use{% archiveParams params %}Label to retrieve all custom parameters, then through{% for item in params %}{{ item.Name }}:{{ item.Value }}{% endfor %}Loop

Related articles

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

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.Our Anqi CMS provided us with clear guidelines and practical features, helping us easily cope with these challenges. ### One: A Clear and Ordered Directory Structure: The Way to Organize Static Resources Good file organization is the foundation of efficient management of static resources.In AnQi CMS

2025-11-08

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 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

How to use custom templates to display content for specific documents or categories?

In the daily operation of websites, the flexibility of content display is often the key to whether a website can stand out and meet the diverse needs of users.AnQiCMS (AnQiCMS) fully understands this point and provides powerful custom template features, allowing content to not only be efficiently managed but also presented in a way that conforms to brand characteristics and user habits. ## Unlock the Custom Template Ability of AnQi CMS AnQi CMS, as an enterprise-level content management system, has always focused on the customization needs of content display from the very beginning.

2025-11-08