As a senior AnQiCMS website operator, I fully understand the core role of templates in content presentation and user experience.Mastering AnQiCMS template syntax, especially distinguishing variable output and logical control tags, is the key to refined operations and achieving personalized content display.
Data display and logic control in AnQiCMS template
The design of AnQiCMS templates aims to provide strong flexibility, allowing content to be displayed in a diverse manner according to business needs. In the template files, we mainly encounter two types of special markers: double curly braces{{ }}and single curly braces percentage sign{% %}They each have different functions, the former is responsible for the direct output of data, and the latter is used to control the logic and structure of the template.
Variable output tag:{{ }}
double curly braces{{ }}In AnQiCMS template, it is the 'window' of data.The core function is to present dynamic data obtained from the backend program directly to the user.Whether it is the title of the article, the price of the product, the content of the comments submitted by users, or the global configuration information of the website, all data that needs to be displayed directly on the page will be output in this way.
For example, to display the title of the article on the article detail page, we will use{{ archive.Title }}; it may be necessary to display dynamically generated links{{ item.Link }}; or to use the website name set by the system{{ system.SiteName }}These tags essentially calculate the expression within the parentheses, then convert the result to a string and insert it into the corresponding position in the template.
Moreover, AnQiCMS also allows the use of "filters" for data preprocessing or formatting when outputting variables. For example,{{ stampToDate(item.CreatedTime, "2006-01-02") }}Can convert Unix timestamp to a human-readable date format; whereas{{ archiveContent|safe }}Then it indicates that the template engine should not escape the HTML content within it, ensuring that the HTML structure generated by the rich text editor can be rendered correctly.These filters greatly enhance the fine control capabilities of data display.
Logic control tags:{% %}
with{{ }}The tag outputs data differently, with single curly braces and percentages{% %}Tags focus on the 'behavior' and 'process' of templates. They do not directly generate visible content on the page, but are used to execute instructions, define code blocks, or control the rendering logic of the template.These tags are usually paired, containing a start tag and an end tag, for example{% if ... %}there must be{% endif %}Close it.
Logical control tags play a vital role in templates, their application covers all aspects of template structure:
- Conditional judgment: Pass
{% if 条件 %}/{% elif 其他条件 %}/{% else %}and{% endif %}The structure allows us to decide whether to display the content on the page based on specific conditions.This is very useful for implementing dynamic content display based on user status, data existence, or other business logic. - Loop through:
{% for item in 集合 %}and{% endfor %}Used to iterate over array, list, and other collection data to dynamically generate repeated HTML structures, such as article lists, navigation menu items, or product displays. Optional when the collection is empty.{% empty %}Tags can provide a backup content block. - Template combination and inheritance: AnQiCMS template supports modular development.
{% include "partial/header.html" %}Used to embed independent template fragments (such as headers, footers, sidebars) into the current template, greatly improving code reusability.{% extends "base.html" %}Enabled template inheritance, allowing us to define a basic layout (parent template), while child templates can overwrite or fill in specific areas defined in the parent template.{% block 名称 %}and{% endblock %}to rewrite or fill in specific areas defined in the parent template.{% macro ... %}Tags allow defining reusable code macros, similar to functions. - Variable declaration and assignment:
{% with var="值" %}and{% set var="值" %}Labels can be declared and assigned within a template. This is useful for temporarily storing calculation results, combining data, or passing parameters to other template fragments (such as withincludeTags used together are very useful. - Tags with specific functions: AnQiCMS is built-in with many functional tags, which also follow
{% %}the syntax. For example,{% archiveList archives with type="page" %}Used to query and retrieve a list of articles,{% pagination pages with show="5" %}Used to generate pagination navigation. Although these tags perform specific data query or structural generation tasks, their essence is to control the template process rather than output data directly.
Summary of the core differences
In simple terms,{{ }}and{% %}The core difference lies in their functional focus:
{{ }}Answer "What to display?" - it directly outputs the value of the variable or expression to the HTML stream.{% %}Answer 'How to organize and control the display?' - It handles the logical flow of templates, structure definition, and operation execution, but does not directly output content.
As a website operator, understanding the functions of these two tags can help us accurately understand and modify the AnQiCMS template, thereby more efficiently publishing and optimizing content, improving the overall performance and user experience of the website.
Frequently Asked Questions (FAQ)
1. Can I{{ }}Variable output label used inside{% %}Are logic control labels?
No.{{ }}The label is designed to parse and output the final value of a variable or expression, it does not have the ability to execute template logic. This means you cannot write code directly inside{{ }}itifJudgment,forLoop or any other logical control instruction. If you need to output different variables based on specific conditions, or to perform complex processing on variables before outputting, you should be in the{{ }}Use outside the tag first{% %}Use logic control tags to complete these operations (for example, assign the result to a new variable), and then{{ 新变量 }}to output the final result.
2. If I forget to close one in the template{% for %}or{% if %}what will happen to the logical control tags like?
If you forget to close any logical control tag that needs to be closed (for example{% for ... %}there is no corresponding{% endfor %}Or{% if ... %}there is no corresponding{% endif %})Provide the closing tag, the AnQiCMS template engine will not be able to correctly parse the template structure when trying to render the page, and will report a syntax error.This will cause the page to display incorrectly and related error information will be recorded in the system background or server logs.To ensure the correctness and maintainability of the template, be sure to use logical control tags in pairs and with correct nesting structure.
3. The rich text content obtained from the database (such as the main text of an article) contains HTML tags, and is used directly.{{ }}Why is the HTML tag escaped and displayed as plain text after output? How can I make it render correctly?
This is the default measure taken by the AnQiCMS template engine for website security (especially to prevent cross-site scripting attacks, XSS). To prevent malicious HTML or JavaScript code from being injected and executed, all through{{ }}The content will be automatically converted to HTML entities. If you are sure that the rich text content obtained from the database is safe and legal, and you want to render it as raw HTML, you can use|safeFilter. For example, to{{ archive.Content }}is modified to{{ archive.Content|safe }}As soon as possible. Please use with caution|safeFilter, apply only when you fully trust the source of the content, otherwise it may introduce security risks to the website