As a website operator who is deeply familiar with the template mechanism of AnQiCMS, I know that template language is the cornerstone of efficient presentation and management of website content. In AnQiCMS, the template system adopts syntax similar to Django template engine, with the core being two main syntax structures: double curly braces used for outputting variables{{ ... }}And the single curly bracket percentage symbol used to control logic and execute specific operations{% ... %}Understanding and mastering these structures is the key to building efficient and dynamic website templates
double curly braces{{ ... }}The way of presenting data
In AnQiCMS template, double curly braces{{ ... }}The primary function is to output the value of the variable.When you need to display dynamic data in a template, such as article titles, category names, website settings information, or any field stored in a database, this syntax will be used.The system will parse the variables within the braces and directly present them on the webpage.
Variable naming usually follows camel case naming conventions, for example{{archive.Title}}Used to display the title of the article,{{system.SiteName}}Used to display the website name.These variables can be simple data types, such as strings, numbers, or objects containing multiple properties..) to access its internal properties, for example{{item.Id}}It will output the ID of the current item in the loop.
In addition to directly outputting variables, double curly braces also carry some important functions. For example, AnQiCMS integratesstampToDateThis function is used to format timestamps. Its calling format also uses double curly braces, such as{{stampToDate(item.CreatedTime, "2006-01-02")}}This makes the data processing logic able to be directly integrated into the output statement. In addition, the filter (filters) is also through the pipeline symbol (|) is used to combine with variables within double curly braces, for example{{obj|filter__name:param}}It allows you to process or format the output variable further, such as truncating strings, case conversion, etc.
single curly braces percentage{% ... %}: The control center for logical AND operation
Different from the double curly braces focused on data presentation, the single curly brace percent sign{% ... %}It is used to define the logical structure in the template, perform control flow operations, or call specific functional tags.These tags do not output content directly, but control the rendering process of the template, or retrieve and organize data for output by double curly braces.
The single curly brace percent sign tag usually needs to appear in pairs, with a start tag and an end tag, such as conditional judgment{% if ... %}{% endif %}or loop traversed{% for ... %}{% endfor %}This structure ensures the completeness and clarity of logic blocks. AnQiCMS provides a rich set of built-in tags covering various needs of website operation:
- Control flow tag:
{% if condition %}Used for conditional judgment,{% for item in list %}Used for traversing arrays or lists,{% with ... %}{% endwith %}and{% set ... %}Used to define and assign variables in templates, for use within the local scope. - Functional tagsThese tags are used to retrieve specific types of data or perform specific operations from the AnQiCMS system. For example,
{% archiveList archives with type="page" %}{% endarchiveList %}used to retrieve and organize the list of article data;{% categoryDetail with name="Title" id="1" %}Used to obtain detailed information of a specified category;{% include "partial/header.html" %}Used to include other template files, achieving modularization of templates;{% extends 'base.html' %}It is used for template inheritance, building reusable page skeletons.
By these tags, template developers can flexibly control the display conditions of page content, data iteration methods, and the organization of page layout, thereby building highly dynamic and maintainable website pages.
variable and tag collaboration
double curly braces in the actual development of AnQiCMS templates{{ ... }}and single curly braces percentage sign{% ... %}It does not exist independently, but works closely together to render the page content.{% ... %}Tags are responsible for the 'behind-the-scenes' work such as retrieving data from the backend, defining loops and conditions, and including files, and{{ ... }}Then move these data that have been processed or organized by tags to the foreground, and present them directly to the user.
For example, a typical article list page will first use{% archiveList %}Label to get article data and then through{% for %}Label to traverse these data and use it in the loop{{ item.Title }}/{{ item.Link }}Use double curly braces to output the title and link of each article.This clear division of labor makes the AnQiCMS template powerful and easy to understand and maintain, effectively supporting the creation, publication, and optimization needs of website content.
Frequently Asked Questions (FAQ)
1. Why do I use{{ myVariable }}Is the content of the output variable empty?
There are several possible reasons. First, checkmyVariableThe spelling is correct, including the case, because AnQiCMS template variable names are case-sensitive.Next, confirm that the variable has been correctly defined or retrieved from background data before it is called.{{archive.Title}}While the current page context does not havearchiveAn object, it will be empty. Finally, some variables may need specific tags to be retrieved, such as{% archiveList %}Such tags will create aarchivesList, then you can traverse ititemand access{{item.Title}}.
2.{{ stampToDate(...) }}and{% system with name="SiteName" %}They are all used for output, what are the differences?
Although both can generate output in the template, their mechanisms and uses are different.{{ stampToDate(...) }}Is aBuilt-in function call.It receives parameters (such as timestamps and format strings), performs internal logical calculations, and then outputs the calculated result directly as a variable value.The core is to execute a function and return the result.{% system with name="SiteName" %}Is aFunctional tagsIt mainly controls or retrieves specific types of data from the system. In this example,systemthe tag is used to obtain the system settings of the website,with name="SiteName"It is a parameter of the tag indicating that it retrieves the system setting item named "SiteName".After the label is executed, it will "provide" a value for output. If you do not specify a variable name, it will output directly, or you can specify a variable to receive the value it provides.In short, the former executes a function and outputs the result, while the latter calls a tag to get or process data, and then outputs the data.
3.{% with ... %}{% endwith %}and{% set ... %}Can all variables be defined in the template, which one should I use?
Both of these tags are used to create or assign variables in the template, but they differ in scope and syntax.
{% with ... %}{% endwith %}A tag defines a variable block, where the variables defined inside are onlywithvalid within the block. It is usually used toincludeLabel passing local variables, or temporarily defining some variables in a specific template code.{% set new_var = "value" %}The tag can be defined at any position in the current template, and its scope is usually from the definition point to the end of the template file.
When choosing to use it, if your variable only needs to be used within a smaller, bounded code block, or if you need to pass the variable toincludea template,{% with %}It is a more suitable choice. If your variable needs to be used in multiple places within the current template and has a broader scope, then{% set %}it will be more convenient.