How to display variables in AnQiCMS templates?

Calendar 👁️ 69

In AnQiCMS templates, how to effectively display variable data is a core skill that every website operator and template creator needs to master.AnQiCMS uses a syntax similar to the Django template engine, making data calls and display both intuitive and flexible.This article will delve into various ways to display variables in AnQiCMS templates, helping you accurately present dynamic information such as background configuration data and article content on the website front end.

Core Concept: Double Curly Braces{{ }}With variable base

In the AnQiCMS template, the most basic way to display any dynamic data is to use double curly braces{{ 变量名 }}. This is an intuitive syntax, the template engine will automatically replace the variables within the double curly braces with actual data.

For example, if you want to display the name of the website in a template, it may be defined as a variableSiteNameYou can call it like this:<h1>{{ SiteName }}</h1>

If a variable is an object containing multiple properties (such as an article, a category), you can use a dot.to access its internal properties. For example, a document object usually has a titleTitleand contentContentproperties:<h2>{{ archive.Title }}</h2> <div>{{ archive.Content }}</div>

It should be noted that variable naming in AnQiCMS usually follows the camel case naming convention, that is, the first letter of each word is capitalized (for exampleSiteName,CreatedTime)

Dynamically retrieve data: The power of tags (Tags)

In addition to directly accessing some global variables, AnQiCMS provides rich \{% tag %}) to dynamically retrieve data from the database. These tags execute specific logic, and assign the retrieved data to variables in the template for further display.

1. Get global or site-level information:AnQiCMS provides some tags to retrieve the configuration information of the entire website or the current site, such assystem/contact/tdk(TDK refers to Title, Description, Keywords). These tags are usually specified throughnameParameters specify the fields to be retrieved:

  • Website system settings: {% system with name="SiteName" %}It will display the website name you have configured in the background “Global Settings”. Similarly,{% system with name="SiteLogo" %}It will return the image address of the website logo.
  • Contact Information: {% contact with name="Cellphone" %}The contact phone number you filled in the background "Contact Settings" will be displayed.
  • Page TDK: {% tdk with name="Title" %}Retrieve the current page title. When you need to combine the website title with the website name, you can use{% tdk with name="Title" siteName=true %}.

2. Get content details:When you are on the article detail page, category detail page, or single page, you can directly use the corresponding detail tag to obtain detailed information about the current content.These tags will automatically recognize the context of the current page.

  • Document details: {% archiveDetail with name="Title" %}It will display the title of the current article. Similarly,{% archiveDetail with name="Content" %}The article content will be displayed. You can also use parametersidortokento obtain information about a specified document.
  • Category details: {% categoryDetail with name="Title" %}to display the name of the current category,{% categoryDetail with name="Description" %}and display the description of the category.
  • Single page details: {% pageDetail with name="Content" %}Content for displaying "About Us" type of single-page.

3. Get content list:For scenarios that require displaying multiple data (such as article lists, product lists, category navigation), you need to use list tags and combine{% for %}Loop tags to iterate and display each item of data.

  • Document list: {% archiveList archives with type="list" categoryId="1" limit="10" %}Will get 10 articles under the specified category. In the loop, you can{{ item.Title }}access the title of each article.
  • Category list: {% categoryList categories with moduleId="1" parentId="0" %}Will retrieve all top-level categories under the article model. You can use it in a loop.{{ item.Title }}and{{ item.Link }}To build the navigation.

When you retrieve data using list tags, you usually organize the code like this:

{% archiveList articles with type="list" limit="5" %}
    {% for article in articles %}
        <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
        <p>{{ article.Description }}</p>
        <span>发布时间:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
    {% empty %}
        <p>暂时没有文章。</p>
    {% endfor %}
{% endarchiveList %}

HerearticlesIs the one you get througharchiveListThe article set obtained by the tag,articleIt is the variable for each article in the loop.

Fine control: Use filters (Filters) to optimize variable display.

It is often not enough to simply obtain raw data; we also need to format, truncate, or convert the data. AnQiCMS provides a "filter" mechanism, by adding a vertical bar after the variable name|With the filter name, you can finely control the display of variables.

1. Process HTML content:|safeWhen you enter rich text (such as article content) in the background editor that contains HTML tags, if you output it directly{{ archive.Content }}To ensure safety, the template engine may escape HTML tags and display them as<p>内容</p>instead of the rendered style. In this case, you need to use|safeA filter that tells the template engine that this content is safe and can be output directly as HTML:<div>{{ archive.Content|safe }}</div>

2. Format dates and times:stampToDateThe timestamp stored in AnQiCMS is usually a 10-digit number. To display it in a human-readable date format, you can usestampToDateFunction:<span>发布日期:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span> <span>更新时间:{{ stampToDate(article.UpdatedTime, "2006-01-02 15:04:05") }}</span>Please note the date format string2006-01-02 15:04:05Is a Go-specific reference time format, used to represent year, month, day, hour, minute, and second.

3. Extract text:|truncatecharsor|truncatewordsWhen you need to display an article summary or brief description, you can use the truncation filter to limit the number of characters or words, and it will automatically add an ellipsis at the end:{{ item.Description|truncatechars:100 }}(Truncated to the first 100 characters){{ item.Description|truncatewords:20 }}(Extract the first 20 words)

4. Other common filters:

  • |lower/|upper: Convert a string to lowercase/uppercase.
  • |add:数字: Add a specified number to the variable.
  • |replace:"旧词,新词": Replace specific content in the string.
  • |lengthGet the length of a string, array, or object collection.
  • |join:", "Join array elements into a string with commas and spaces.

Flexible variable definition:withandsetTag

Sometimes, you may need to define some temporary variables within the template to make the code clearer or to avoid repeated calculations. AnQiCMS provideswithandsetto achieve this purpose.

  • {% with ... %}:Used to define temporary, block-scoped variables in template blocks, often combined withincludetags to pass parameters to the imported template.
    
    {% with pageTitle="我的自定义页面标题", pageKeywords="关键字1,关键字2" %}
        {% include "partial/header.html" with title=pageTitle keywords=pageKeywords only %}
    {% endwith %}
    
  • {% set ... %}:Used to define within the current template

Related articles

What file extension should AnQiCMS template files use to ensure correct display?

When using AnQiCMS for website construction and content management, ensuring that the template files are displayed correctly is the foundation for the normal operation of the website.This is where, the naming convention of template files, especially the file extension, plays a crucial role. The template files of Anqi CMS have a clear and unified file extension requirement, which is to use **.html** as the file extension of the template files.These files are usually organized in the `/template` folder under the root directory of the website.Choose `.html`

2025-11-08

How to configure different content display templates for mobile and PC separately?

In website operation, providing a good browsing experience for different devices is crucial.AnQi CMS knows this, it provides a flexible template mechanism, allowing you to easily configure different content display templates for mobile and PC terminals, thereby meeting user needs and improving website performance.Below, let's take a detailed look at how to achieve this goal in Anqi CMS.### Understanding AnQi CMS Template Mode AnQi CMS provides three main website modes to meet the needs of content display on mobile and PC platforms in different scenarios: 1.

2025-11-08

How can I customize a template for a specific category or page to control the display style?

When operating a website, we often encounter situations where different types of content, even articles in the same category or different "About Us" pages on the same website, hope to have a unique display style to better attract readers and convey information.AnQiCMS (AnQiCMS) is well aware of this need and therefore provides a very flexible template customization mechanism, allowing us to easily tailor the display style for specific categories or pages.The Anqi CMS template system is based on the Django template engine syntax in Go language, which means that as long as you have a basic understanding of front-end development

2025-11-08

Does the background template editing function support real-time preview of content display effects?

AnQi CMS is favored by many operators for its efficient and flexible features in the field of website content management.When we talk about the visual presentation and user experience of a website, the customization capabilities of the template are undoubtedly the core.Among them, the real-time preview of the background template editing function and the display effect of the content is a focus for many users. ### The template editing capability of AnQi CMS: powerful and flexible AnQi CMS provides users with powerful and flexible backend template editing functions.The system uses a template engine syntax similar to Django, which is understandable for users familiar with front-end development or template languages

2025-11-08

How to use conditional judgment and loop control to dynamically adjust the display of content?

In Anqi CMS, we often encounter scenarios where we need to display different information based on specific conditions or repeat a series of contents.At this time, by flexibly using conditional judgment and loop control, the website content can become more vivid and intelligent.The AnQi CMS template feature provides powerful syntax support similar to the Django template engine, through these tags, we can easily achieve dynamic content adjustment without writing complex background code. ### Conditional judgment: Make the content 'speak to people' Conditional judgment is the basis for dynamic content display.Imagine

2025-11-08

Why is UTF8 encoding crucial for the display of content in AnQiCMS templates?

## Content display no longer troubles: Why is UTF-8 encoding so critical for AnQiCMS templates?In the process of building and operating a website with AnQiCMS, we often strive for the website's efficiency, stability, and diverse display of content.However, there is a seemingly basic but actually crucial link that, if not handled properly, will cause all our efforts to be wasted - that is the character encoding of the template file.Especially for AnQiCMS, UTF-8 encoding is not just a recommendation, it is the core foundation to ensure that website content is displayed correctly and clearly.

2025-11-08

Where should the mobile-specific template file be stored to be recognized by AnQiCMS?

Provide special optimization for the mobile experience of the website in AnQiCMS (AnQiCMS), which is a very practical requirement.Benefiting from the flexible template mechanism of AnQiCMS, we can easily achieve this goal.When it is necessary to provide a set of independent template files for mobile devices, the storage location and configuration method of these templates are crucial.

2025-11-08

How does AnQiCMS automatically apply the default display template for documents or categories through naming conventions?

In website operation, template management is the key to ensuring the professionalism and consistency of content display.For many operators, manually selecting or specifying a template for each new article or category is undoubtedly a repetitive and time-consuming task.AnQi CMS understands this pain point, therefore it introduces a set of intelligent naming conventions, allowing the system to automatically identify and apply default display templates, thereby greatly simplifying our daily operations.The cleverness of this feature lies in AnQiCMS's ability to intelligently associate content with preset design styles according to the specific naming rules of template files

2025-11-08