How to define and use variables in AnQiCMS templates to display dynamic content?

Calendar 👁️ 77

When building a website, we all hope that the content can change flexibly according to different pages and different data, rather than unchanging static text.AnQiCMS with its high-performance architecture based on the Go language and flexible Django-like template engine, provides us with powerful dynamic content display capabilities.The core of all this, cannot be separated from the definition and use of 'variables'.

Master how to define and use variables in AnQiCMS templates, just like you've unlocked the key to dynamic vitality on your website.This is not just a technical operation, but also a key step to achieve content marketing, SEO optimization, and user experience enhancement.

Core: Basic definition and usage of variables

AnQiCMS's template syntax is concise and intuitive, it follows a style similar to the Django template engine. The most direct way is to use double curly braces{{ 变量名 }}To display dynamic content. When the system renders the page, it will look for the variable names within these braces and replace them with the actual data.

Variable naming usually follows camelCase, for examplearchive.Idorarchive.Title. Here,archiveusually represents the articles, products and other content entities loaded on the current page, andIdandTitleare the properties of the entity.

Get data from system built-in tags

AnQiCMS has preset a large number of template tags for us, which implicitly inject data from the background configuration or database into the template environment when called, allowing us to use it in the form of variables.

  1. Global site and contact information:If you want to display the company name, phone number, filing number, and other global information at any location on the website, you can usesystemandcontacttags. For example, to display the website name:{% system siteName with name="SiteName" %}{{ siteName }}. Here,systemThe label obtained the system configuration named “SiteName” and assigned it tositeNamevariable, then we use{{ siteName }}Display it. Similarly, to display the contact number, you can do it like this:{% contact cellphone with name="Cellphone" %}{{ cellphone }}.

  2. Page-specific information (TDK):The page title (Title), keywords (Keywords), and description (Description) are crucial for SEO.tdkTags can dynamically retrieve this information. For example, in<title>The tag displays the page title:<title>{% tdk with name="Title" siteName=true %}</title>. Here,siteName=trueIt means that the website name will be automatically added to the page title, enhancing brand recognition.

  3. Content detail page:When we visit a specific article or product page, the main content of the page is the most typical dynamic information.archiveDetailThe tag is used to retrieve detailed data of the current document, which can be accessed directly byarchive.属性名in the form of, for example{{ archive.Title }}It will display the title of the article,{{ archive.Content }}The article content will be displayed. For example, on an article detail page, we can display the content like this:

    <h1>{{ archive.Title }}</h1>
    <div>
        发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}
        阅读量:{{ archive.Views }}
    </div>
    <div class="article-content">
        {{ archive.Content|safe }}
    </div>
    

    Herearchive.Title/archive.CreatedTimeandarchive.ViewsThey are all directly usable variables.archive.Contentthe following|safeIt is a filter that tells the template engine that this content is safe HTML, does not need to be escaped, thereby ensuring that formatted content in rich text editors is displayed correctly.

Define and use variables in a loop

Websites often need to display list data, such as article lists, product lists, and so on.forLoop tags are a powerful tool for handling such dynamic content, they assign the data of the current item to a temporary variable during each iteration.

For example, display a list of articles:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{ item.Link }}">
            <h3>{{ item.Title }}</h3>
            <p>{{ item.Description|truncatechars:100 }}</p>
            <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
        </a>
    </li>
    {% empty %}
    <li>目前还没有任何文章。</li>
    {% endfor %}
{% endarchiveList %}

In this example,archivesIsarchiveListThe article collection obtained by the tag,{% for item in archives %}The statement makesitemThe current article object in each loop. We can access the properties of each article throughitem.Link/item.Titleand so on to access the properties of each article.item.Description|truncatechars:100Demonstrates how to usetruncatecharsFilter to extract description content, prevent it from being too long.

Advanced: Create and operate variables within the template.

In addition to obtaining data from built-in tags, we can also declare and operate variables directly in the template, which brings greater flexibility to template design.

UsewithTags define temporary variables.

{% with ... %}Tags allow you to define temporary variables within a local scope, which are only valid between the start and end of the tag. This is useful when you need towithspecify certain blocks or sections.includePassing parameters to template fragments is very convenient.

{% with headerTitle="我的博客首页" headerKeywords="技术,分享,CMS" %}
    <head>
        <title>{{ headerTitle }}</title>
        <meta name="keywords" content="{{ headerKeywords }}">
    </head>
{% endwith %}

here,headerTitleandheaderKeywordsonlywithAvailable within tags. If we pass them to a commonheader.htmlTemplate, it can be written like this:{% include "partial/header.html" with title=headerTitle keywords=headerKeywords %}

UsesetTags declare more flexible variables

{% set ... %}The tag provides broader variable declaration capabilities. It defines variables that are available in the current template file, not just limited to a block, which is very useful for performing some calculations or storing intermediate results.

`twig {% set greeting = "Welcome to the AnQiCMS world!" %}

{{ greeting }}

{#

Related articles

How to organize directory structure of AnQi CMS template files to manage content display?

For users of AnQiCMS, understanding the organization of template files is the key to efficiently managing website content display.A clear and orderly template directory structure, not only allows you to easily customize the appearance of the website, but also greatly improves the flexibility and efficiency of content operation.The template files of Anqi CMS use `.html` as the suffix and are stored in the `/template` folder under the root directory of the website.And all the style sheets, JavaScript scripts, images, and other static resources used in all templates have their own home

2025-11-07

How to configure Anqi CMS to support adaptive, code adaptation, or PC+Mobile independent site display mode?

In the current internet environment, it has become a norm for users to browse websites on various devices. How to ensure that your website presents a**good effect on different devices is one of the keys to the success of website operation.AnQiCMS (AnQiCMS) is well-versed in this, providing flexible and diverse display mode configurations, allowing you to easily switch between adaptive websites, code adaptation, or PC+Mobile independent sites according to your actual needs.This article will detail how to configure these display modes in Anqi CMS, ensuring that your content shines on any screen.###

2025-11-07

What template engine syntax does AnQi CMS support to control content display?

As users of AnQi CMS, we all hope to efficiently and flexibly control the way website content is presented.AnQi CMS performs very well in this aspect, it provides a set of intuitive and powerful template engine syntax, allowing us to easily achieve a variety of content display needs. ### Basic: Anqi CMS Template Engine: Django style, easy to learn and use Anqi CMS template engine adopts a marking method similar to Django syntax, which allows friends with experience in other template engines to quickly get started.Its design philosophy is to strive for simplicity and efficiency

2025-11-07

How does AnQi CMS ensure the compliance and security of displayed content?

In website operation, the compliance and security of content is a focus for every webmaster and operator.It not only concerns the reputation and trust of the website, but may also touch upon the legal and regulatory line.As a system dedicated to providing users with an efficient and secure content management experience, AnQiCMS (AnQiCMS) integrated these core demands into its security mechanism from the outset, ensuring the compliance and security of the content we display in multiple dimensions.<h3>Build a Secure Foundation: From System Bottom to Content Interaction</h3> The security of AnQi CMS primarily originates from its solid technical foundation

2025-11-07

How to flexibly control the display logic of conditional judgment and loop control tags?

Build dynamic and interactive websites in AnQi CMS, it is inseparable from precise control of the content display logic.This involves conditional judgment (`if`) and loop control (`for`) tags playing a core role.They are like a powerful 'combination punch', making the template no longer a static layout, but one that can intelligently respond and flexibly present content based on data changes.The template syntax of AnQi CMS borrows from the Django template engine, its concise and efficient marking method allows even beginners in template development to quickly get started.

2025-11-07

How can you customize templates for specific documents or categories to achieve personalized display?

In Anqi CMS, when you want a specific article, product, category, or standalone page to have a unique display effect, the system provides a flexible template customization feature that allows you to easily achieve personalized content display.This is greatly beneficial for creating brand characteristics, launching special event pages, optimizing the user experience of specific content, and even improving SEO performance.Why do we need to customize templates for specific content?Imagine that on your website there is an "About Us" page about the history of the company, you may want it to be dignified and elegant

2025-11-07

How does Anqi CMS automatically extract the first image of the document content as a thumbnail for display?

In website content operation, carefully selecting and setting thumbnails for each article or product page is a time-consuming and important task.A captivating thumbnail can significantly increase click-through rate, optimize user experience, and benefit SEO performance.However, facing an overwhelming amount of content, manually uploading and adjusting thumbnails will undoubtedly become a huge burden.AnQi CMS deeply understands this, and for this reason, it provides a smart and flexible solution that automatically converts the first image of the document content into a thumbnail, greatly simplifying the operation process.## Smart Extraction

2025-11-07

How to add recommended attributes (such as headlines, slides) to control the priority of display on the homepage?

In website operation, how to efficiently display important content, ensure that they can be found first when users visit the homepage, is the key to improving user experience and conversion rate.The Anqi CMS provides a flexible "recommended attribute" feature, allowing us to easily control the display priority of documents, whether as the headline news of the website or prominently displayed in the carousel, it can achieve precise control.

2025-11-07