As a senior security CMS website operation person, I know well the core position of content in the digital age.An efficient content management system, especially its template functionality, is the foundation to ensure that website content can be presented flexibly and accurately to readers.It does an excellent job in this aspect, drawing on the mature Django template engine syntax, allowing content creators and template developers to build pages in an intuitive and powerful way.
An overview of the Django-style syntax of the AnQi CMS template engine
The template engine of Anqi CMS is designed cleverly, its syntax style is similar to that of Django template engine in Python, and it also absorbs the concise characteristics of modern templates like Blade.This design aims to lower the learning threshold, allowing teams familiar with Web development to quickly get started and efficiently create and manage website content..htmlauto/templateThe core lies in the output of variables and the use of logic control tags under the directory.
Template data output, i.e., variables, are represented by double curly braces{{ 变量名 }}The format. This is consistent with the style of many modern template engines, intuitive and easy to understand. For example, if you want to display the name of the website on the page, you can simply write{{ system.SiteName }}. In the AnQi CMS, variable names usually follow the CamelCase naming convention, for examplearchive.Titleused to obtain the document title,system.SiteLogoUsed to obtain the website logo image address, such naming convention helps to maintain the consistency and readability of the code.
而对于模板中的逻辑控制,例如条件判断、循环遍历等,安企CMS则使用了单花括号与百分号的组合{% 标签 %}These tags always appear in pairs, with clear start and end tags, for example{% if 条件 %} ... {% endif %}Used for conditional judgment,{% for item in 列表 %} ... {% endfor %}Used for iteration. This structure clearly defines the scope of the logical code block, making template maintenance and expansion more convenient.
Flexible application of variables and data presentation
The variables in the Anqi CMS template are the carriers of dynamic page content.Through these variables, you can easily access system settings, article details, category information, user data, and more.{{ archive.Title }}Show the current article title, by{{ archive.CreatedTime }}Get the article publish timestamp and format it with the filter, or by{{ archive.Description }}Display the article summary.
For more complex data types, such as lists or arrays, you can directly access their properties. For example, when you get a list containing multiple friendship links,friendLinksyou can directly access them through{{ item.Link }}and{{ item.Title }}Translate each link's address and name. This dot notation (.) method of accessing attributes greatly simplifies the operation of data levels.
Powerful control of tags and page structure construction
Conditional judgment and loop traversal
The most basic logic control tag isifandfor.{% if 条件 %}Tags allow you to decide whether to render a specific content block based on the truth or falsity of a certain condition. For example,{% if item.Thumb %}Can determine whether the article has a thumbnail, thereby deciding whether to display the image tag.{% for item in 列表 %}Labels are used to iterate over arrays or lists, processing each element one by one. In loops, you can also useforloop.Counterto get the current loop index, or{% empty %}Tags are used to handle empty lists, all of these are very practical features.
Built-in feature tags
The auto CMS built-in rich tags cover many aspects of website operation. For example,{% system with name="SiteName" %}you can directly obtain the website name,{% contact with name="Cellphone" %}and then you can get the preset contact phone number.{% archiveList archives with type="page" limit="10" %}This is the core tag for fetching article lists and supporting pagination. It allows for flexible display of article content across different categories and sorting.{% pagination pages with show="5" %}Then used to render pagination navigation bar. These tags, through concise parameter configuration, greatly improve the efficiency of template development.
Auxiliary tags and filters
To further enhance the reusability and expressiveness of the template, Anqi CMS also providesinclude/extendsandmacroauxiliary tags.{% include "partial/header.html" %}Allow you to split the common part of the page (such as header, footer) into independent files and reference them where needed, to avoid code redundancy.{% extends 'base.html' %}Then template inheritance is implemented, you can define common structures in the base template and then overwrite specific content blocks in the sub-template to achieve unified website style and flexible changes of local content.{% macro 函数名(参数) %}It can define reusable code snippets, like functions, that accept parameters and return rendered content, further improving the modularization of the template.
Moreover, Filters are a powerful tool for handling variable output formats. Through the pipe character|, you can pass variables to one or more filters for processing. For example,{{ item.CreatedTime|stampToDate:"2006-01-02" }}The timestamp can be formatted into a readable date string,{{ content|safe }}It is used to declare that the content is safe HTML, preventing automatic escaping.{{ value|truncatechars:10 }}Can truncate strings to a specified length, which provides convenience for the refined display of content.
Agreements and **practice** in template creation
In AnQi CMS, it is crucial to follow some basic conventions for template development.Template files should use UTF-8 encoding to avoid garbled characters./public/static/Under the catalog. UtilizeincludeandextendsBuild maintainable template structures, separating common parts and variable areas, is the key to efficient development.At the same time, fully utilizing the various built-in tags and filters can reduce the need to write custom logic, making the template more focused on content display.
The template engine of Anqi CMS, with its intuitive syntax similar to Django, provides a powerful and flexible tool for website operators and developers.Whether it is the daily content release or the complex page layout adjustment, it can be completed with ease.
Common Questions and Answers (FAQ)
1. Is the template tag of AnQi CMS case-sensitive?
Yes, the template tags and variables of AnQi CMS are strictly case-sensitive. For example,{{ archive.Title }}ofTitleThe first letter must be capitalized to correctly retrieve the document title. Pay special attention to the case of variable names and tag parameters when writing template code to avoid unnecessary errors.
2. How to deal with garbled characters in template files?
Template files are usually encoded in incorrect encoding.Security CMS requires all template files to be encoded in UTF-8.If you are editing template files in a Windows environment, please make sure that your text editor (such as Notepad++, VS Code) saves the file in UTF-8 format.design-convention.mdDescription of file encoding in the document, and adjust your editing environment settings.
3. How to dynamically judge the current page type in the template (such as article detail page, category list page)?
The template engine of Anqi CMS automatically loads the corresponding context data when rendering different pages.You can determine the type of the current page by checking if a specific variable or tag exists.archiveThe object (or any custom document variable you define) is usually filled, you can check{% if archive %}to determine whether it is on the document detail page. Similarly, on the category list page,categoryan object orarchivesThe list will exist. Moreover, the Anqi CMS also provides suchtdktags, and the output content will also automatically adjust according to the current page type.