How to use variables and control tags in AnQiCMS templates?

Calendar 👁️ 72

AnQi CMS, an enterprise-level content management system built on the Go language, with its efficient, secure, and flexible features, provides strong support for content operators and enterprise users.In AnQiCMS, content is not just static text and images, through its carefully designed template engine, we can transform data into vivid and personalized website pages.Understanding how to use variables and control tags in the AnQiCMS template is a key step in unleashing its powerful customization capabilities.

AnQiCMS's template engine adopts a syntax similar to Django or Blade, which means developers familiar with these front-end template languages will feel very comfortable.It makes dynamic content rendering easy and intuitive.Template files are usually named with.htmlStored neatly as a suffix/templateIn the directory, while the accompanying styles, scripts, and images are stored independently./public/static/In the directory, maintaining a good organizational structure.

Variables: the magic that brings content to life.

In AnQiCMS templates, variables are the main means to display dynamic data.Imagine if the title, article content, and product prices of a website all had to be manually modified, it would be so cumbersome.The introduction of variables is to solve this problem.

When you need to display a dynamic piece of information on a page, such as the title of the current article, the name of the website, or the contact phone number, you just need to enclose the variable name in double curly braces, like this:{{ 变量名称 }}. AnQiCMS variable naming follows camel case, that is, the first letter of each word is capitalized, for examplearchive.Titlemeans the title of the article,system.SiteNameIt represents the name of the website. This clear naming convention allows developers to quickly understand the content pointed to by the variable.

For example, if we want to display the title and publication time of a blog post, it might be written like this:

<h1>{{ archive.Title }}</h1>
<p>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</p>

here,archive.TitleWe can directly use the title field of the current article object.archive.CreatedTimeThe timestamp is retrieved using the built-in AnQiCMSstampToDateFunction (in AnQiCMS, it is treated as a tag function), format it into the familiar "year-month-day" form. AnQiCMS has built-in tags to retrieve different types of data, such assystemTags are used to retrieve global settings (such asSiteName/SiteLogo)contactTags are used to retrieve contact information (such asCellphone/Email)archiveDetailTags are specifically used to retrieve article details, they can all be accessed throughnameparameters to specify the specific fields to be retrieved.

In addition to directly outputting variables, AnQiCMS also provides powerful "filters" (Filters) that allow us to further process the output of variables. Filters are applied using the pipe character|Connected after the variable, for example{{ obj|filter__name:param }}. A common use case is to handle rich text content, in order to prevent XSS attacks, the template engine defaults to escaping HTML tags.If we need to display the HTML content generated by the editor, we must use|safeFilter to declare content as safe, allowing the browser to parse normally, for example{{ archive.Content|safe }}In addition, there is also|truncatechars:9Used to truncate the string and add an ellipsis,|lowerUsed to convert strings to lowercase, as well as|add:2Used for adding numbers, etc., these filters greatly enhance the flexibility of variable output.

Control tags: the skeleton for building page logic

Just having dynamic data is not enough, a fully functional website also needs to display different content based on different conditions, or traverse list data.This is where the control tags come into play. In AnQiCMS templates, control tags are defined using single curly braces and percent signs, for example{% 标签 %}and most need corresponding end tags{% end标签 %}to specify the scope.

1. Conditional judgment: if/elif/else/endif

ifTags are the basis for implementing conditional logic. They can determine whether to render a section of content based on the truth value of an expression. For example, if an article has a thumbnail, we display it:

{% if archive.Thumb %}
    <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
{% else %}
    <img src="{% system with name='DefaultThumb' %}" alt="默认缩略图">
{% endif %}

here,{% if archive.Thumb %}Will checkarchive.ThumbDoes exist or has a value. If true, the article thumbnail is displayed; otherwise,{% else %}a system default thumbnail is displayed. In addition,{% elif %}Can be used to handle multiple parallel conditions, making logical judgment more refined.

2. Loop traversal: for/empty/endfor

The website is filled with list data, such as article lists, product lists, navigation menus, and so on.forThe label is born to iterate over these data. It can access each item in the array, Slice, and other collections one by one, and make each item available within the loop.

Assuming we want to display a list of the latest articles:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <li>
            <a href="{{ item.Link }}">{{ item.Title }}</a>
            <span>阅读量:{{ item.Views }}</span>
        </li>
    {% empty %}
        <p>当前没有任何文章发布。</p>
    {% endfor %}
{% endarchiveList %}

In this example,{% archiveList archives with type="list" limit="10" %}Is a control tag used to get a list of articles, which assigns the data of 10 articles obtained toarchivesVariable. Next,{% for item in archives %}The loop will iterate overarchivesEach article initem, then in<li>Display its link, title and reading count in the tag

It is worth mentioning,forLoop also supports some convenient properties and modifiers:

  • forloop.CounterGet the current loop index (starting from 1).
  • forloop.RevcounterGet the remaining number of times in the loop.
  • reversedReverse traverse the list.
  • sortedSort and traverse the list.
  • emptyWhen the loop collection is empty,{% empty %}and{% endfor %}the content between them will be rendered, very suitable for displaying a "no content" prompt.
  • cycleWhen alternating multiple values in the loop, for example{% cycle "even" "odd" %}You can output 'even' and 'odd' alternately in each loop, which is often used to add different CSS classes to list items.

3. Variable assignment:with/set

Temporarily defining some variables in the template can effectively improve the readability and reusability of the code.{% with %}Labels allow you to declare one or more variables within a block scope and{% endwith %}Use colon between

{% with pageTitle="安企CMS官网", pageKeywords="CMS,建站系统,Go语言" %}
    <title>{{ pageTitle }}</title>
    <meta name="keywords" content="{{ pageKeywords }}">
{% endwith %}

And{% set %}You can declare a variable at any position in the current template, its scope is usually wider, until the current template rendering ends.

4. Modularization and Inheritance:include/extends/block/macro

To enhance the reusability and maintainability of the template, AnQiCMS introduced the module

Related articles

What are the specifications for the template file suffix and storage directory of AnQiCMS?

AnQiCMS as an efficient and customizable enterprise-level content management system, follows a clear and flexible set of rules in the organization and use of template files.For website operators and developers, a deep understanding of these regulations not only ensures the correct presentation of front-end content on the website but also provides great convenience in content layout and personalized customization.Today, let's unveil the mystery of AnQiCMS template files together and see what suffixes and directory secrets they have.First, AnQiCMS

2025-11-07

Where can I view the update log of AnQiCMS? What new features have been added in the latest version?

As an experienced website operations expert, I know that keeping the system updated is the key to maintaining the competitiveness of the website and ensuring its security and functionality.For an enterprise-level content management system like AnQiCMS (AnQiCMS) that is dedicated to providing efficient, customizable, and easily scalable solutions, understanding its update log and latest features is a 'compulsory course' for our operation staff.Today, let's deeply analyze the update of AnQiCMS.--- ### Learn More About AnQi CMS: Where is the update log, and what useful features does the latest version bring

2025-11-07

How does AnQiCMS ensure content safety and compliance?

Good, as an experienced website operations expert, I am well aware of the importance of content security and compliance for any online platform.AnQiCMS (AnQi CMS) has put a lot of consideration into this and provides a series of powerful and practical features to ensure that your digital assets are properly protected and comply with relevant regulations.Next, let me give you a detailed explanation of how AnQiCMS has built this solid defense line. --- ## AnQiCMS: Content security and compliance, how to safeguard your digital assets?In today's rapidly changing internet environment

2025-11-07

How does AnQiCMS's static cache and TDK configuration improve website loading speed?

Good, as an experienced website operation expert, I am very willing to deeply analyze how AnQiCMS's static cache and TDK configuration work together to inject speed and vitality into your website.How to improve website loading speed with AnQiCMS static cache and TDK configuration?In today's digital age, website loading speed has become a core indicator of user experience and search engine friendliness.

2025-11-07

What template types does AnQiCMS support (adaptive, code adaptation, PC + mobile)?

## AnQiCMS Template World: Flexible for various screens, making your website omnipresent In the wave of digitalization today, whether your website can seamlessly present on various devices is directly related to user experience and brand image.As an experienced website operations expert, I am well aware of the importance of a flexible and efficient content management system (CMS) to a company.

2025-11-07

How to include header, footer, and other common code snippets in AnQiCMS templates?

## AnQiCMS template development: clever integration of common code snippets, making your website maintenance more efficient When operating a website, we often find that many pages share the same basic structure, such as the website header, footer, sidebar navigation, or a general ad slot.If you have to rewrite this code for each page repeatedly, it is not only inefficient but also, once you need to make changes, you have to adjust them one by one, which is undoubtedly a nightmare for website maintenance.It is good that AnQiCMS is an enterprise-level content management system developed in Go language

2025-11-07

What are the naming conventions for the document detail page and list page template files in AnQiCMS?

As a senior website operation expert, I am well aware that the naming rules of a content management system template are not only technical details, but also the foundation of content operation efficiency and website maintainability.AnQiCMS (AnQiCMS) is designed with flexibility and efficiency, and also provides clear and practical naming conventions in template management, allowing developers and operators to operate with ease.Today, let's delve into the naming rules of AnQiCMS's document detail page and list page template filenames to help you better master this powerful tool.

2025-11-07

What are the functions of the 38 commonly used tags in AnQiCMS templates?

As an experienced website operations expert, I have a deep understanding of the powerful functions of AnQiCMS (AnQiCMS) and its flexible template system.It is not just a content management system, but also a tool that empowers enterprises and content operation teams, especially in terms of content display and management. Its core lies in a set of elegantly designed and feature-rich template tags system.Today, let's delve deeply into the 38 commonly used tags in the Anqi CMS template, see what roles they play, and how they help us easily build and manage all kinds of websites.

2025-11-07