How to distinguish between variable output (`{{ }}`) and logical control tags (`{% %}`) in AnQi CMS template?

Calendar 👁️ 56

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

Related articles

Where should the static resources such as images, JS, and CSS used in the template be stored统一放在哪里?

Hello!As an experienced security CMS operator, I fully understand the importance of static resource management in website construction and content management.High-quality static resource management not only improves website performance, but also optimizes maintenance efficiency.According to the Anqi CMS documentation and our practical experience, here is a detailed explanation of where the static resources such as images, JS, and CSS used in the template should be stored.

2025-11-06

Which folder under the website root directory must all template files be stored in?

As an experienced website operator familiar with the operation mechanism of AnQiCMS, I deeply understand that the template structure of a content management system (CMS) is crucial for the display, maintenance, and optimization of a website.A clear and standardized template file storage rule is the foundation for ensuring the efficient operation of the website and the smooth release of content.In the Anqi CMS system, all template files used to build the website front-end interface have their specific storage location.

2025-11-06

What suffix should be used for AnQiCMS template files?

As a senior CMS website operator, I am well aware that the correct use and management of template files are crucial for the stable operation and content presentation of the website.A clear template structure not only facilitates content creation and publishing, but also ensures the maintenance efficiency and scalability of the website.What suffix should be used for the Anqi CMS template file, this is a fundamental and key question, we can find the clear answer from the system design conventions.

2025-11-06

Which function upgrades in the AnQiCMS update log have the greatest impact on website operation?

As a senior security CMS website operator, I fully understand the importance of each system update to daily work efficiency and the overall performance of the website.AnQi CMS is committed to providing efficient solutions for small and medium-sized enterprises and content operation teams, and the functional upgrades in its update log often directly relate to all aspects of our content creation, publication, optimization, and even user maintenance.The detailed explanation of which function upgrades in the AnQiCMS update log have the greatest impact on website operation.

2025-11-06

Are conditional judgment and loop control tags in the template required to have closing tags?

As an experienced CMS website operation person, I am well aware of the importance of template design for website content display and user experience.During the process of content creation, editing, and publishing, we often deal with templates, and understanding the correct usage of template tags, especially the closure requirements of conditional judgment and loop control tags, is the basis for ensuring the correct rendering of content and the stable operation of the website.About whether the end tags are required for conditional judgment and loop control tags in the template, the template engine of AnQi CMS follows clear conventions.Make the agreement document according to the template we provide, **condition judgment

2025-11-06

What is the naming rule for template variables?(For example:`archive.Id`)

As a senior security CMS website operator, I know that a clear and consistent template variable naming rule is crucial for the efficient operation and content management of the website.This not only improves the efficiency of template development and maintenance, but also helps operation personnel quickly locate and understand the data structure when adjusting content.In Anqi CMS, the naming of template variables follows a set of clear conventions aimed at ensuring the readability and maintainability of the system.

2025-11-06

What encoding format should the template file be saved in to avoid page garbled characters?

Hello!As an experienced CMS website operation person, I know that every detail in content publishing and website maintenance may affect user experience.The page garbled characters are one of the common and annoying problems, which are directly related to the correct presentation of content.According to the Anqi CMS documentation, there is a clear and simple solution to this problem.

2025-11-06

What types of template modes does Anqi CMS support?

As a website operator who deeply understands the operation of AnQiCMS (AnQiCMS) and has a profound understanding of content operation, I am very happy to give you a detailed explanation of the template mode supported by AnQiCMS.In AnQiCMS design philosophy, flexibility and adaptability are among its core advantages, which naturally also manifest in its powerful template system.AnQiCMS knows that different websites and operational strategies have diverse needs for front-end display.

2025-11-06