As an experienced AnQiCMS website operations personnel, I am well aware of the core role of templates in content presentation and user experience.Mastering AnQiCMS template syntax, especially distinguishing between variable output and logical control tags, is the key to refined operation 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 tags: double curly braces{{ }}With single curly braces and percentage signs{% %}They each have different functions, the former is responsible for direct data output, and the latter is used to control the logic and structure of the template.

Variable output tag:{{ }}

Double curly braces{{ }}The 'window' of data in AnQiCMS template.The core function is to directly present dynamic data obtained from the backend program from the database or other data sources 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, any 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 get the website name set by the system, which will be used{{ system.SiteName }}These tags essentially evaluate the expression within the brackets, convert the result to a string, and insert it into the corresponding position in the template.

Moreover, AnQiCMS also allows the use of "filters" (Filters) for data preprocessing or formatting when variables are output. For example,{{ stampToDate(item.CreatedTime, "2006-01-02") }}Can convert Unix timestamps to human-readable date format;{{ archiveContent|safe }}The value 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 ability of data display.

Logical control tags:{% %}

With{{ }}Tags directly output data differently, single curly braces percentage signs{% %}Tags focus on the "behavior" and "process" of the template.They do not produce visible content directly on the page, but are used to execute commands, define code blocks, or control the rendering logic of templates.{% if ... %}must have{% endif %}to close.

Logic control tags play a crucial role in templates, their application covers all aspects of template structure:.

  • Conditional judgment: Through{% if 条件 %}/{% elif 其他条件 %}/{% else %}and{% endif %}Structure, we can 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.
  • to iterate:{% for item in 集合 %}and{% endfor %}Used to iterate over arrays, lists, 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 an alternative content block.
  • Template combination and inheritanceAnQiCMS 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" %}Realized template inheritance, allowing us to define a basic layout (parent template), while child templates can{% block 名称 %}and{% endblock %}overwrite or fill in specific areas defined in the parent template.{% macro ... %}Tags allow the definition of reusable code macros, similar to functions.
  • Variable declaration and assignment:{% with var="值" %}and{% set var="值" %}Labels can be declared and assigned variables within a template. This is useful for temporarily storing computation results, combining data, or passing parameters to other template fragments (e.g., withincludeLabel combination is very useful.
  • Specific function tags: AnQiCMS has built-in many functional tags, which also follow{% %}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 structure generation tasks, their essence is to control the template flow rather than directly output data.

Core Difference Summary

In simple terms,{{ }}and{% %}The core difference lies in their focus of function:

  • {{ }}Answer 'What to display?' - it directly outputs the value of the variable or expression into the HTML stream.
  • {% %}Answer the question 'How to organize and control the display?' — It handles the logical flow, structure definition, and operation execution of the template, but does not directly output content.

As website operators, clearly understanding the functions of these two tags can help us more accurately interpret and modify the AnQiCMS template, thereby more efficiently publishing and optimizing content, and enhancing the overall performance and user experience of the website.


Common Questions and Answers (FAQ)

1. Can I use{{ }}variable output tags inside{% %}logical control tags?

No.{{ }}The design purpose of the tag is to parse and output the final value of variables or expressions, it does not have the ability to execute template logic. This means you cannot directly write{{ }}insideifjudgment,forLoop or any other logic control instruction. If you need to output different variables based on specific conditions or to perform complex processing on variables before outputting, you should{{ }}Tags outside first use{% %}Complete these operations (such as, assigning the result to a new variable) and then through{{ 新变量 }}Output the final result.

2. What if I forget to close one in the template?{% for %}or{% if %}What will happen if I forget to close a logical control tag (such as)

If you forget to close any logical control tag that requires it (for example){% for ... %}without a corresponding{% endfor %}, or{% if ... %}without a corresponding{% endif %}Provide the closing tag, otherwise AnQiCMS template engine will not be able to correctly parse the template structure during page rendering 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, please make sure to use the logical control tags in pairs and with proper nesting.

3. The rich text content (such as article content) retrieved from the database contains HTML tags, and it is used directly without{{ }}Why is the HTML tag escaped to plain text when displayed? How can I make it render correctly?

This is a measure taken by the AnQiCMS template engine for website security (especially to prevent cross-site scripting attacks, XSS). To avoid malicious HTML or JavaScript code from being injected and executed, all through{{ }}The content output will be automatically escaped as 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 }}Change to{{ archive.Content|safe }}[en] Please be cautious when using it. It is essential to be careful.|safe[en] Filter, only apply it when you fully trust the source of the content, otherwise it may introduce security risks to the website.