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