How to dynamically display data using Django-style tags and variables in AnQiCMS templates?
AnQiCMS provides a flexible and powerful template system, which adopts a syntax style similar to the Django template engine, making experienced developers able to quickly get started, and it is also very intuitive, even users unfamiliar with template language can dynamically present website content through simple learning.This system, with its concise labels and variable usage, makes the interaction between data and the front-end page efficient and easy to manage.
Make full use of the AnQiCMS template to dynamically display data, we need to understand its core syntax structure: variables, tags, and filters.
Understanding variables: the direct way to output data
In the AnQiCMS template, variables are the main way to directly output backend data to the frontend page. They are usually written using double curly braces{{ 变量名 }}Define. When the page is requested, AnQiCMS will parse these variables and replace them with the corresponding actual data.For example, if you want to display the title of an article, you can use it directly{{ archive.Title }}.
It is noteworthy that variable names usually follow the camel case naming convention, that is, the first letter of each word is capitalized, for examplearchive.Idrepresents the article ID,archive.CreatedTimeRepresents the creation time of the article. This naming convention makes the meaning of variables clear.
Using tags: to achieve logical control and data acquisition
Tags are the core of the AnQiCMS template system that implements logic control and executes specific functions. They are represented by single curly braces and the percent sign{% 标签名 %}The form appears, and most tags need a corresponding closing tag, such as{% if ... %}{% endif %}or{% for ... %}{% endfor %}.
Data acquisition tagAnQiCMS has built-in multiple data retrieval tags, allowing you to easily extract the required information from the database.
archiveDetailandarchiveListUsed to get the details of a single article or a list of articles. For example,{% archiveDetail with name="Title" %}It can get the title of the current page article, while{% archiveList archives with type="page" limit="10" %}It can get a paginated list of articles and assign it toarchivesVariables. These tags usually acceptid(ID),categoryId(Category ID),moduleId(Model ID),order(Sorting method) and other parameters to help you accurately filter the required data.categoryDetailandcategoryList: Used to get category details or category lists. You can usemoduleIdandparentIdparameters to build complex category navigation structures.systemandcontact: These tags are used to get the global settings of the website (such as the website nameSiteName, Record numberSiteIcp)and contact information (such as phone numberCellphone, emailEmail)- There are
pageDetail/pageListUsed for single page content,tagDetail/tagList/tagDataListUsed for tag-related data, as well aslinkListUsed for friend links, etc., all work with similar parameters and methods.
Control flow tagThese tags enable the template to have the logic capability of a programming language.
ifConditional judgment:{% if condition %}{% elif another_condition %}{% else %}{% endif %}The structure allows you to display different content based on different conditions. For example,{% if archive.Thumb %} <img src="{{ archive.Thumb }}" /> {% else %} <img src="/default-thumb.png" /> {% endif %}You can choose to display different images based on whether the article has a thumbnail.forLoop through:{% for item in collection %}{% empty %}{% endfor %}Used to iterate over lists or arrays. When the collection is empty,{% empty %}the content in the block will be executed. Inside the loop,itemThe variable represents the current element in the loop, you can also useforloop.Counterto get the current loop index.
Functional tags
stampToDateThis is a very practical feature that can format Unix timestamps into readable date and time strings. For example,{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}Display the article creation time in the format of "Year-Month-Day Hour:Minute".paginationis used to generate pagination navigation, usually witharchiveListused together to allow users to browse a large list of content.breadcrumb:Automatically generate breadcrumb navigation, enhancing the website's user experience and SEO friendliness.withandset:{% with key="value" %}{% endwith %}and{% set new_var = "value" %}Used to define temporary variables in templates, facilitating complex logic or data transmission.
Master the filter: a powerful tool for data beautification and transformation
Filters are a powerful tool for processing and formatting variable output in the AnQiCMS template system. They use the vertical bar symbol|After applying to the variable, such as{{ 变量 | 过滤器名:参数 }}.
safe:When you need to output a variable containing HTML content (such as the article bodyarchive.Content)To prevent HTML tags from being escaped as plain text, you must use|safea filter. For example:{{ archive.Content|safe }}.truncatechars/truncatewords:It is used to truncate a string to a specified length and automatically add an ellipsis.truncatecharsBy character count,truncatewordsTruncated by word count, with corresponding_htmlVersion used to safely handle HTML content.add/replace/split/join: Perform string concatenation, replacement, splitting into arrays, or concatenating arrays into strings, etc. For example,{{ "Hello AnQiCMS"|replace:"AnQi,Go" }}.upper/lower/capfirst/title: Convert the case of English strings.thumb: For image path variables,|thumbThe filter can quickly obtain its thumbnail version.render: After you enable the Markdown editor in the background, the content will be stored in Markdown format by default. Use|renderThe filter can be converted into HTML format and displayed on the front end.
Actual operation example: dynamically build the page.
Assuming we are building a homepage to display the latest list of articles, and we want each article to include a title, abstract, publication date, and thumbnail.
”`twig {# Get the website name for page title #}
Latest article
{# Use archiveList tag to get the article list, type as list, limit 10, and show flag attribute #} {% archiveList latest_archives with type="list" limit="10" showFlag=true %} {% for article in latest_archives %}{{ article.Title }}
<div class="article-body">
{# 检查是否有缩略图,如果有则显示 #}
{% if article.Thumb %}
<a href="{{ article.Link }}" class="article-thumb">
<img src="{{ article.Thumb }}"
Related articles
How to choose and apply the adaptive, code adaptation, PC+mobile terminal template mode supported by AnQiCMS?
How to ensure that the content is presented elegantly and efficiently on various devices when building and operating a website is a challenge that every operator has to face.AnQiCMS as a feature-rich enterprise-level content management system, fully considers this point, and provides three flexible template modes of adaptive, code adaptation, and independent sites for PC and mobile phones, allowing us to make **choices according to actual needs.Next, we will delve into the characteristics, application scenarios, and how to configure and use these three modes in AnQiCMS
2025-11-07What are the specific conventions for naming template files and directory structures when creating templates in AnQiCMS?
AnQiCMS is a content management system renowned for its efficiency and flexibility, also has a clear set of conventions in template design, aiming to help developers and operators build websites with clear structures and easy maintenance.Understanding these conventions is a key step to efficiently using AnQiCMS for website customization and content display. ### Core conventions of template files The template files of AnQiCMS use the `.html` extension as the suffix.This convention makes the file type clear at a glance and also helps the editor with syntax highlighting and intelligent hints
2025-11-07How does the modular design of AnQiCMS support personalized customization and secondary development requirements?
The choice of a content management system (CMS) focuses on flexibility and scalability, which is a core concern for many businesses and individual users.AnQiCMS (AnQiCMS) stands out with its unique modular design, demonstrating significant advantages in supporting personalized customization and secondary development, allowing users to create powerful and distinctive websites according to their own needs. ### The Foundation of Modular Design: The Elasticity of Core Architecture Anqi CMS is developed in Go language, which choice itself brings high performance and high concurrency advantages to the system. It is more important
2025-11-07How to ensure the stability and smoothness of the AnQiCMS website under high concurrent access volume?
When your AnQiCMS website starts to grow rapidly in traffic, even facing the challenge of high concurrency, how to ensure that it can still run stably and respond smoothly is a concern for every operator.As AnQiCMS was well thought out from the start, combined with its underlying technical advantages and some clever operational strategies, we can build a website as solid as a rock.One of the core strengths of AnQiCMS is that it is developed using the Go language.Go language is renowned for its excellent concurrent processing capabilities
2025-11-07How to precisely filter and display a document list with specific categories, models, or recommended attributes using the `archiveList` tag?
In AnQi CMS, the `archiveList` tag is the core tool for building dynamic content lists, allowing you to flexibly extract and display the content you want from the website's document library.Whether it is to display the latest articles under a specific category, or popular products under a certain content model, or special document topics with specific recommendation attributes, `archiveList` can help you accurately meet these needs through its rich parameter configuration.### Accurately locate content: Classification, Model and Recommendation Attributes First
2025-11-07How to retrieve the title, content, image, and custom fields of a specific document using the `archiveDetail` tag?
How to use the `archiveDetail` tag in AnQiCMS to finely control the display of document content?AnQiCMS as an efficient and flexible content management system, one of its core advantages lies in its powerful content display capabilities.For each independent article, product detail, or any other 'document' type of content on the website, we hope to fine-tune their display methods on the front page.This is the place where the `archiveDetail` tag really shines.
2025-11-07How to implement hierarchical display and nested calling of the `categoryList` tag?
Building a clear and organized website navigation in AnQiCMS is crucial for improving user experience and website accessibility, especially when dealing with multi-level categories.The `categoryList` tag was created for this purpose, providing powerful features to help us flexibly display the hierarchical structure of categories and make fine-grained nested calls.
2025-11-07How to use the `prevArchive` and `nextArchive` tags to navigate to adjacent documents in the article detail page?
In today's content-driven world, the user experience of the website article detail page is particularly important.When a reader is immersed in an excellent content, if they can navigate smoothly to related or adjacent articles, not only can it extend their stay on the website and increase the page views, but it can also indirectly optimize the search engine crawling and ranking through the internal link structure.AnQi CMS is an efficient and SEO-friendly content management system that fully considers these details and provides simple and intuitive template tags to help us easily achieve this function. Today
2025-11-07