How to define and use variables in AnQiCMS to display data flexibly in templates?

Calendar 👁️ 61

AnQiCMS (AnQiCMS) is a highly efficient and flexible content management system, whose powerful template functions are the key to building personalized websites.In templates, defining and using variables flexibly allows us to easily handle and display various data, whether it is article titles, category information, or custom field content, all can be dynamically called through variables, making the website full of vitality.

Next, we will delve into how to define, retrieve, and use variables in AnQiCMS templates, helping everyone build and maintain websites more efficiently.

The Basics of Variables: Understanding and Declaration

In AnQiCMS template syntax, variables are placeholders used to store and display data.They allow our templates to receive data from the backend and render it to the page as needed.

1. Variable Output Format

AnQiCMS templates use double curly braces{{ 变量名 }}Output the value of the variable. When you need to display a specific data on the page, just place the variable name inside double curly braces. For example,{{ siteName }}The name of the website may be displayed.

When naming variables, it is usually followed by camel case naming method (CamelCase), which means the first letter of each word is capitalized, for examplearchive.Id/archive.Title.

2. Declare variables:{% with %}with{% set %}

In certain scenarios, we need to define our own variables within the template or store the output results of complex tags for repeated use. AnQiCMS provides two main ways to declare variables:

  • {% with ... %}Tags: withLabels allow you to define one or more variables within a specific code block. These variables are only valid in{% with %}and{% endwith %}between. This is very suitable forincludePass arguments with labels or use temporary variables within a local scope.

    {% with pageTitle="最新文章列表" %}
        <h1>{{ pageTitle }}</h1>
        {# pageTitle变量在此处有效 #}
    {% endwith %}
    {# pageTitle变量在此处已失效 #}
    

    You can also define multiple variables at the same time, separated by spaces:

    {% with title="产品介绍" keywords="产品,详情" %}
        <title>{{ title }}</title>
        <meta name="keywords" content="{{ keywords }}">
    {% endwith %}
    
  • {% set ... %}Tags: setThe tag is used to declare a variable in the current template file, its scope starts from the declaration point and ends at the end of the template file.This is very useful for sharing data across different parts of the template.

    {% set welcomeMessage = "欢迎来到我们的网站!" %}
    <header>
        {{ welcomeMessage }}
    </header>
    <main>
        <p>{{ welcomeMessage }}</p>
    </main>
    

    Tip: {% with %}It is more suitable to define local, temporary variables,{% set %}It is more suitable to define variables that are needed throughout the template. A reasonable choice can avoid name conflicts and improve code readability.

II. The source of variables: Where do the data come from

Variables in AnQiCMS are not created out of thin air, they are mainly obtained through built-in template tags to access backend data, or directly read from URL parameters.

1. Tag empowerment: Get data from built-in tags

AnQiCMS is built-in with rich tags to retrieve various data of the website.These tags usually handle data queries and logic internally and assign the results to a variable name you specify for use in the template.

  • Get single data:Many tags are used to get data for a single entity, such assystem(System settings),contact(Contact information),tdk(Page TDK),archiveDetail(Article Details),categoryDetail(Category Details) etc.

    These tags pass through变量名称 with name="字段名称"in the form, directly assigning the value of a specific field to变量名称.

    {# 获取网站名称,并赋值给siteName变量 #}
    {% system siteName with name="SiteName" %}
    <p>网站名称:{{ siteName }}</p>
    
    {# 直接获取当前文章的标题 #}
    <h1>{% archiveDetail with name="Title" %}</h1>
    
  • Get list data:Some tags are used to obtain data collections (such as article lists, category lists, navigation lists), which will assign an array or slice to a specified variable name. At this point, you need to use{% for %}Loop tags to iterate through this variable, display each item one by one.

    {# 获取文章列表,并赋值给archives变量 #}
    {% archiveList archives with type="list" limit="5" %}
        <ul>
        {% for item in archives %}
            <li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
        {% endfor %}
        </ul>
    {% endarchiveList %}
    

    In this example,archivesis a list variable containing multiple articles.itemisfora temporary variable for each article in the loop.

  • Get custom parameters:archiveParamsYou can customize additional fields for articles or product models on the backend,archiveParamsLabel retrieval. This label will assign the data of custom fields as a list or map (map) to a variable, and you can iterate and display it as needed.

    {# 获取当前文章的所有自定义参数 #}
    {% archiveParams params %}
        <div class="custom-fields">
        {% for field in params %}
            <p>{{ field.Name }}:{{ field.Value }}</p>
        {% endfor %}
        </div>
    {% endarchiveParams %}
    

2. URL parameters and global data

AnQiCMS template engine can also access some global data or retrieve parameters from the URL. For example, on the search page, you may need to retrieve the search keywords from the URLq. It is mentioned in the documentarchiveListThe tag is inqIf the parameter is not specified, it will automatically read the URL in itqThe query parameter implies that it is possible tourlParams.qaccess the URL parameters in this form.

{# 假设URL是 /search?q=安企CMS,这里可以获取搜索关键词 #}
<input type="text" name="q" value="{{ urlParams.q }}" placeholder="请输入搜索关键词">

The depth of variables: data processing and display

After obtaining the variables, we can further process and display them in various ways to make the page content more expressive.

1. Access variable properties: dot operator.

When a variable stores an object or structure (such asitema variable represents an article object), you can use the dot operator.Access its properties.

`twig {# item is an article object, accessed via dot notation its

Related articles

How to format a timestamp in AnQiCMS template and display it in a specified date format?

In AnQiCMS template, processing timestamps and displaying them in a specific date format is a common requirement in content operation.No matter whether you need to display the publication time of the article or the update date of the product, understanding how to flexibly format time will greatly enhance the expressiveness of the website content.AnQiCMS provides simple and powerful template tags, making this process easy and convenient.

2025-11-08

How does the AnQiCMS template loop through arrays or objects to display list data?

In the Anqi CMS template system, the core of looping through arrays or objects and displaying list data lies in understanding its Django-like template syntax, especially the use of the `for` loop tag.This mechanism makes the presentation of dynamic content intuitive and efficient, whether it is for article lists, category navigation, or product displays, it can adapt flexibly. ### Core Mechanism: The Use of `for` Loops Data traversal in Anqi CMS templates mainly relies on `{% for ...in ...

2025-11-08

How to implement conditional logic in AnQiCMS to control the display of content?

In website operation, displaying different content based on different conditions is the key to improving user experience and achieving refined operation.AnQiCMS provides powerful and flexible template tags and logical judgment functions, allowing us to easily implement conditional display of content, thereby meeting various business needs.This article will delve into how to make use of the template engine features in AnQiCMS, controlling the display of website content precisely through conditional logic judgments.

2025-11-08

How to set and display the global information of AnQiCMS, such as Logo, filing number, and copyright?

AnQiCMS provides an intuitive and powerful way to manage and display the global information of the website, such as the website logo, filing number, and copyright statement.This information is not only an important part of the website's brand image, but also concerns the legal and compliant operation of the website.The detailed introduction on how to set and utilize these global information will follow. ### Set global information in the background All the global information of the website is concentrated in the "Global Function Settings" of AnQiCMS backend.

2025-11-08

How to use the template inheritance and reference feature of AnQiCMS to build reusable display modules?

When using AnQiCMS to build a website, we often encounter such situations: each page of the website has common header navigation, footer information, or some modules (such as sidebars, article recommendation lists) need to be displayed in multiple places, but the content or style may be slightly different.If you copy and paste this code every time, not only is it inefficient, but also once it needs to be modified, you have to adjust it one by one on all related pages, which is very easy to make mistakes and difficult to maintain.

2025-11-08

How to display different content and manage interfaces for multiple sites in the AnQiCMS website?

AnQiCMS, with its powerful multi-site management functions, provides an efficient and flexible solution for operators with multiple brands, sub-sites, or content branches.It allows you to easily create, manage, and display multiple independently operated websites under the same core system, greatly enhancing the efficiency of content operation.

2025-11-08

How to configure and display the contact information of the AnQiCMS website, such as phone number, address, and social media?

The contact information of the website is crucial for establishing a connection and providing support to users.AnQiCMS (AnQiCMS) provides an intuitive and flexible way to manage and display this important information, whether it's phone numbers, addresses, or various social media platform links, all can be easily achieved. ### Configure the website contact information in the background To configure the website contact information, you can first log in to the Anqi CMS backend management interface.In the left navigation bar, find and click "Background Settings", then select "Contact Information Settings".Here is a collection of all the contact information you may need to display

2025-11-08

How to display the details and list of a custom single page in AnQiCMS?

In AnQiCMS, the Single Page (Page) feature provides the website with the powerful ability to display independent and fixed content, such as 'About Us', 'Contact Information', 'Company Profile', and other pages.These pages usually do not belong to a specific category, but exist independently, carrying important corporate information or brand stories.Efficiently create, manage, and flexibly display the details and lists of these single pages on the website, which is an indispensable part of content operation.

2025-11-08