How to define variables and display their values in AnQiCMS templates?

Calendar 👁️ 62

In AnQiCMS templates, we often need to handle various data, whether it is the name of the website, the title of the article, or the product details, these dynamic contents need to be carried and displayed through variables.Understanding how to define and display variables in templates is the core of AnQiCMS template development.

AnQiCMS uses a syntax similar to the Django template engine, making variable usage intuitive and easy to learn. In the template file (usually.htmlFormat, and placed in/templateDirectory), we mainly use two symbols to operate content: double curly braces{{ }}Used to output the value of variables, while single curly braces and percentage signs{% %}It is used to define logical structures, such as conditional judgments or loops, and is also sometimes used to call specific data tags and define variables.

Display of basic variables

The most direct way to display a variable is to enclose the variable name in double curly braces. For example, if you want to display the current article title on a page and the backend has already passed the article data to the template and named itarchiveAn object, you can display it like this easily:

<h1>{{ archive.Title }}</h1>

HerearchiveIt is an object,TitleThen it is the attribute of this object. Through.The dot operator, we can delve into the properties of the object layer by layer. AnQiCMS usually follows the camel case naming convention for variable names, that is, the first letter of each word is capitalized, for examplearchive.Id/archive.Link.

Define a new variable in the template

Sometimes, we may need to create some temporary variables inside the template for subsequent calculations, combinations, or use in local code blocks. AnQiCMS providessetandwithDefine variables in two ways.

setThe tag is used to define a new variable and assign a value in the current template file. The scope of this variable will last until the end of the current template file.

{% set myTitle = "欢迎来到安企CMS的世界" %}
<p>{{ myTitle }}</p>

AndwithLabels are often used to define a temporary, scope-limited variable, it is usually associated withincludeTags are used to combine, to pass specific data to imported sub-templates, or to declare local variables within a code block, and to{% endwith %}end their scope.

{% with headerTitle = "安企CMS官方网站" %}
    <h1>{{ headerTitle }}</h1>
{% endwith %}
{# 在这里,headerTitle 变量将不再可用 #}

Use built-in tags to retrieve and display data

AnQiCMS is built-in with many powerful tags, which can directly obtain configuration data, content data, and so on, and provide two commonly used ways to display variable values: direct output or define as a variable first and then output.

For example, to get the website name, we can usesystemtag. If you just want to display the website name directly at a certain position, you can write it like this:

<p>我们的网站名称是:{% system with name="SiteName" %}</p>

If you want to assign the website name to a new variable so that it can be used flexibly in multiple locations in the template or for further processing, you can specify the variable name after the tag:

{% system siteBrandName with name="SiteName" %}
<p>网站品牌:{{ siteBrandName }}</p>
<meta property="og:site_name" content="{{ siteBrandName }}">

There are similar tags such ascontactUsed to obtain contact information, such as{% contact cellPhone with name="Cellphone" %}{{ cellPhone }})、tdkUsed to obtain page SEO-related TDK information, such asarchiveDetailTo obtain article details, such ascategoryDetailAnd to obtain category details, such aspageDetail(Get single page details) and so on. They all follow this pattern ofwith name="字段名称"to specify which field to retrieve.

to process list data:forLoop

In websites, we often need to display lists of articles, product lists, and other collection data. AnQiCMS'sforThe loop tag can iterate over these data collections and assign the current item to a temporary variable in each iteration for us to display its properties.

For example, to display the list of the latest ten articles, you can cooperate witharchiveListTags:

{% archiveList articles with type="list" limit="10" %}
    <ul>
        {% for item in articles %}
            <li>
                <a href="{{ item.Link }}">
                    <h3>{{ item.Title }}</h3>
                    <p>{{ item.Description|truncatechars:100 }}</p>
                    <small>发布日期: {{ stampToDate(item.CreatedTime, "2006-01-02") }}</small>
                </a>
            </li>
        {% empty %}
            <li>暂无文章</li>
        {% endfor %}
    </ul>
{% endarchiveList %}

In this example,for item in articlesIt will assign the current article data toitemvariable. We can then useitem.Link/item.TitleIn order to access and display the various properties of the article.forloop.CounterThese built-in variables can also help us obtain information such as the current index of the loop.

Conditional display:ifStatement.

Sometimes, we may need to decide based on specific conditions whether to display a variable or code block.ifThe statement allows us to make logical judgments.

For example, if an article does not have a thumbnail, we will not display the image area:

{% if item.Thumb %}
    <img src="{{ item.Thumb }}" alt="{{ item.Title }}">
{% else %}
    <img src="{% system with name="SiteLogo" %}" alt="默认图片">
{% endif %}

Use filters to beautify and process variables

AnQiCMS provides a rich set of filters (Filters) to process variables, such as formatting, truncation, escaping, etc. When using filters, just add them to the variable name with|(Pipe symbol) and filter name is enough.

  • |safe:When the variable content contains HTML code and you do not want it to be escaped and displayed, use|safeThe filter ensures that HTML code is correctly parsed by the browser. This is particularly important for displaying rich text editor content.
    
    <div>文章内容:{{ archive.Content|safe }}</div>
    
  • |stampToDate:AnQiCMS timestamps are usually stored in the form of 10-digit numbers,stampToDatethe function can format it into the date and time format we need. “`twigUpdate time: {{ stampToDate(archive.UpdatedTime, “2006年0

Related articles

How to iterate over an array or slice in AnQiCMS template and loop through its elements?

In the AnQi CMS template world, dynamically displaying content is the key to building an active and feature-rich website.Whether it is an article list, product display, navigation menu, or image gallery, we need a mechanism to traverse the data set and display each element on the page.Fortunately, the template engine of Anqi CMS provides an intuitive and easy-to-use loop structure, allowing us to easily achieve this goal.

2025-11-09

How to implement scheduled publishing of articles in AnQiCMS to ensure that content is visible on the front page at specified times?

As a content operator, we know that in the age of information explosion, the timing of content release often determines its dissemination effect.In order to coordinate with market activities, maintain a stable update frequency, or publish important announcements at specific times, a function that can accurately control the publication time is indispensable.AnQiCMS (AnQi Content Management System) is well-versed in this, providing us with a simple and powerful scheduling mechanism to ensure that every meticulously prepared article is presented to the reader at the most appropriate time.Use AnQiCMS for timed publishing

2025-11-09

How to customize parameters in the AnQiCMS backend and display them in the front-end template?

During the operation of a website, we often encounter such needs: standard content fields (such as title, content, publication time) can no longer meet the needs of personalized display.For example, a product detail page may need to display "production dateAnQiCMS as a flexible enterprise-level content management system fully considers these customized needs.

2025-11-09

How to build and display the breadcrumb navigation of the current page using the `breadcrumb` tag in AnQiCMS?

In website operation, breadcrumb navigation (Breadcrumb Navigation) is an important element to enhance user experience and website structure clarity.It not only helps visitors quickly understand the position of the current page in the website hierarchy, but also effectively assists search engines in understanding the relationship between website content, and has a positive effect on SEO optimization.For AnQiCMS users, building and displaying breadcrumb navigation is a very convenient operation, thanks to the powerful template tags built into AnQiCMS.### Understand AnQiCMS's

2025-11-09

How to set up an independent template for a single page in AnQiCMS and display its content correctly?

In AnQiCMS, single pages (such as "About Us", "Contact Information", or specific product landing pages) are an important part of our website content.They usually carry unique information and need to have a different layout and visual style from other content on the website (such as article lists or product details).Fortunately, AnQiCMS provides极高的flexibility, allowing you to easily set up a dedicated independent template for each single page, thereby creating a unique user experience.### AnQiCMS template base

2025-11-09

How does the AnQiCMS website achieve fast content display under high concurrency through static caching and Go language features?

In today's rapidly changing digital world, the speed of website access and stability under high concurrency have become key factors in user experience and search engine rankings.For a content-rich corporate website or self-media platform, ensuring that content is presented quickly while efficiently utilizing server resources is a major challenge for operators.AnQiCMS (AnQiCMS) with its powerful core based on the Go language and the exquisite static caching mechanism, provides an excellent solution to this problem.### Go Language Empowerment

2025-11-09

How to convert the case of a string and display it in AnQiCMS template?

In website content operation, we often encounter the need to format text.In order to unify the brand image, improve the readability of the page, or optimize the search engine friendliness, converting strings to uppercase and lowercase is a fundamental and practical skill.AnQiCMS as an enterprise-level content management system developed based on the Go language, with its flexible template engine syntax (similar to Django templates), provides us with a rich set of tools to accurately control the display of content.Today, let's delve deeply into how to use AnQiCMS

2025-11-09

Does AnQiCMS support displaying collected articles directly on the website?

## AnQiCMS content collection feature: let the article go online directly, or go further?In the era of information explosion, continuously delivering high-quality content to websites is a major challenge faced by many operators.The efficiency of content production is directly related to the activity of the website, user stickiness, and even search engine rankings.

2025-11-09