How to use double curly braces to define variables and single curly braces with percentages to define tags in AnQiCMS templates?

Calendar 👁️ 57

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.

Related articles

What are the basic conventions and file directory structure for AnQiCMS template creation?

As a senior practitioner who deeply understands the operation of AnQiCMS, I know the core position of templates in website construction.A well-structured template that follows conventions can not only present content efficiently but is also the foundation of website SEO and user experience.Below, I will elaborate on the basic conventions and file directory structure of AnQi CMS template creation, hoping to provide strong support for your content creation and management on AnQiCMS.

2025-11-06

How to use AnQiCMS link push feature to notify search engines to crawl pages?

As a senior security CMS website operator, I am well aware of the importance of search engine crawling for the visibility of website content.Safe CMS provides a convenient link push function to help your website content be discovered and indexed by major search engines more quickly.This feature will greatly enhance the SEO performance of your website, ensuring that high-quality content can reach potential readers in a timely manner.The detailed operation guide on how to effectively use the Anqi CMS link push function to notify search engines to crawl the page.

2025-11-06

What are the practical operations of AnQiCMS image resource management function?

As a senior AnQi CMS website operation staff, I am fully aware that the management of high-quality image resources is crucial for the attractiveness of website content and user experience.The Anqi CMS not only provides basic storage and classification functions for image resource management, but also integrates multiple practical operations, aiming to help operators efficiently handle and optimize the visual content of the website.

2025-11-06

How to create and edit a single page in AnQiCMS's page management feature?

As a website content expert who deeply understands the operation of AnQiCMS, I know the importance of high-quality, well-organized page content for attracting and retaining users.In Anqi CMS, the single-page management feature provides a powerful and intuitive platform for creating and maintaining static content that is independent of the regular articles or product streams, such as "About Us", "Contact Information", "Service Introduction", and so on.These pages often carry the core information of the website, which is an important window for users to understand the company and trust the brand.

2025-11-06

How to get system configuration information (such as website name, Logo) in AnQiCMS template?

As an expert in the long-term operation of AnQiCMS content, I fully understand the importance of dynamically obtaining system configuration information in the template.Efficiently display the website name, logo, and other core elements, which not only ensures brand consistency but also can flexibly adapt to changes in operational strategies.AnQiCMS's powerful template engine provides intuitive and feature-rich tags, making it easy to obtain these system-level configurations in front-end templates.

2025-11-06

How to get contact information in AnQiCMS template (such as phone, email)?

As a website operator who has been deeply involved in AnQiCMS for many years, I know the importance of clearly and effectively displaying key information, especially contact information, on the website front-end template.This is not only about user experience, but also the key to building trust and promoting communication.AnQiCMS was designed with this in mind from the beginning, providing intuitive and flexible template tags that allow developers to easily access and display this information.Next, I will elaborate on how to obtain and display contact information in the AnQiCMS template, such as phone and email.

2025-11-06

How to call the TDK (Title, Keywords, Description) information of the page in AnQiCMS template?

As a website operator who is well-versed in the operation of AnQiCMS, I deeply understand the importance of TDK (Title, Keywords, Description) information for the performance of the website in search engines and the willingness of users to click.AnQiCMS was designed with SEO-friendliness in mind from the beginning, providing us with flexible and powerful mechanisms to manage these key metadata.Properly calling TDK in the template is the basis for ensuring that website content is efficiently indexed, improves click-through rates, and enhances user experience.

2025-11-06

How to get and display the navigation list and its sub-menu in AnQiCMS template?

## AnQiCMS Navigation List and Submenu Display in Templates As an experienced AnQiCMS website operations personnel, I am well aware of the importance of a clear and efficient website navigation for user experience and content access.AnQi CMS provides a powerful and flexible navigation list tag `navList` for template developers, making it easy to obtain and display the main navigation, footer navigation, and even more complex multi-level navigation structures with submenus on the website front-end.

2025-11-06