As an experienced CMS website operations personnel of an enterprise, I deeply understand the core status of content in the digital age.An efficient content management system, especially its template function, is the cornerstone to ensure that the website content can be presented flexibly and accurately to the readers.AnQi CMS performs very well in this aspect, it draws on the mature Django template engine syntax, allowing content creators and template developers to build pages in an intuitive and powerful way.
AnQi CMS template engine overview of Django-style syntax
The AnQi CMS template engine is cleverly designed, its syntax style is similar to that of the Django template engine in Python, and it also absorbs the concise characteristics of modern templates like Blade.This design aims to reduce the threshold of learning, allowing teams familiar with web development to quickly get started and efficiently create and manage website content.The template file is usually with.htmlas suffix, stored in/templatedirectory, its core lies in the output of variables and the application of logical control tags.
The data output in the template, which is a variable, is represented by double curly braces{{ 变量名 }}This style is consistent with 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 simply write{{ system.SiteName }}. In Anqi CMS, variable names usually follow CamelCase, for examplearchive.TitleUsed to get the document title,system.SiteLogoUsed to obtain the website logo image address, such naming conventions help maintain code consistency and readability.
And for the logical control in the template, such as conditional judgment, loop traversal, etc., Anqi CMS uses the combination of single curly braces and percentages.{% 标签 %}These tags always appear in pairs, with clear start and end tags, such as{% if 条件 %} ... {% endif %}Used for conditional judgment,{% for item in 列表 %} ... {% endfor %}Used for loop traversal. This structure clearly defines the scope of the logic code block, making template maintenance and expansion more convenient.
Flexible application of variables and data presentation
The variables in Anqi CMS templates are carriers of dynamic page content.Through these variables, you can easily access system settings, article details, category information, user data, and more.For example, on the article detail page, you can{{ archive.Title }}Show the title of the current article through{{ archive.CreatedTime }}Get the timestamp of the article published and format it with a filter, or through{{ archive.Description }}Display the article summary.
For more complex data types, such as lists or arrays, you can also directly access their properties. For example, when you get a list containing multiple friendship linksfriendLinksyou can directly access through{{ item.Link }}and{{ item.Title }}Output the address and name of each link. The dot notation method of accessing properties simplifies the operation of data hierarchies..Strong control of tags and page structure construction.
Strong control of tags and page structure construction.
In addition to variables, the tag system of Anqi CMS is even more powerful in its template engine.They are not only used to control the logic flow of the page, but also to call various built-in functions, even to achieve the reuse and inheritance of templates.
Conditional judgment and loop traversal
The most basic logical control tag isifandfor.{% if 条件 %}The tag allows 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 if the article has a thumbnail, thereby deciding whether to display the image tag.{% for item in 列表 %}The tag is used to iterate over arrays or lists, processing each element one by one. In a loop, you can also useforloop.Counterto get the current loop index, or{% empty %}Tags to handle the case of an empty list, these are very practical features.
Built-in feature tags
The Anqi CMS is built-in with rich tags covering many aspects of website operation. For example,{% system with name="SiteName" %}you can directly obtain the name of the website,{% contact with name="Cellphone" %}and then you can get the preset contact phone number.{% archiveList archives with type="page" limit="10" %}It is the core tag for getting the article list and supporting pagination. It can flexibly display articles of different categories and sorting.{% pagination pages with show="5" %}It is used to render pagination navigation bars. These tags greatly improve the efficiency of template development through concise parameter configuration.
Auxiliary tags and filters
To further enhance the reusability and presentation of the template, Anqi CMS also providesinclude/extendsandmacroand other auxiliary tags.{% include "partial/header.html" %}Allow you to split the common parts of the page (such as the header and footer) into independent files and reference them where needed, to avoid code redundancy.{% extends 'base.html' %}Then template inheritance was implemented, you can define a general structure in the base template, and then overwrite specific content blocks in the child template to achieve unified website style and flexible changes of local content.{% macro 函数名(参数) %}You can define reusable code snippets that accept parameters and return rendered content, thereby improving the modularization of templates.
Moreover, filters are powerful tools for handling variable output formats. Through the pipe character|, you can pass a variable 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 }}which 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 refinement of content display.
Template production conventions and **practice.
It is crucial to follow some basic conventions when developing templates in AnQi CMS.The template file should use UTF-8 encoding to avoid garbled characters.The template directory structure is clear, static resources should be stored in/public/static/Under the catalog. Utilize.includeandextendsBuild a maintainable template structure, separate the common part and the variable area, which is the key to efficient development.At the same time, fully utilizing various built-in tags and filters can reduce the writing of custom logic, allowing the template to focus more on content display.
The Anqi CMS template engine, with its intuitive syntax similar to Django, provides a powerful and flexible tool for website operators and developers.Whether it is the daily content publishing or the adjustment of complex page layouts, it can be done with ease.
Frequently Asked Questions (FAQ)
1. Do AnQi CMS template tags distinguish between uppercase and lowercase?
Yes, AnQi CMS template tags and variables are strictly case-sensitive. For example,{{ archive.Title }}ofTitleIt must start with a capital letter to correctly retrieve the document title. Be sure to pay 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?
The template file is garbled usually due to incorrect file encoding.The AnqiCMS requires that all template files must use UTF-8 encoding.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. Check.design-convention.mdDescription of file encoding in the document, and adjust your editing environment settings.
How to dynamically determine the current page type in the template (such as article detail page, category list page)?
The Anqi CMS template engine 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.For example, on the article detail page,archiveThe object (or any custom document variable you define) is usually filled, you can check{% if archive %}to determine if it is on the document detail page. Similarly, on the category list page,categoryor objectarchivesThe list will exist. Moreover, Anqin CMS also provides suchtdktags, and the output content will also adjust automatically according to the current page type.