How to use Django template engine syntax in AnQiCMS template to control the content display logic?

Calendar 👁️ 65

AnQiCMS is an efficient content management system developed based on the Go language, its template system uses syntax similar to Django's template engine, which is undoubtedly an advantage for friends familiar with web development to quickly get started and implement powerful content display logic.It allows us to flexibly control the presentation of content at the template level without writing complex backend code.

To understand how to use this GoLang implemented Django template engine syntax in the AnQiCMS template to control the display logic of content, we can break it down into several core parts: displaying variables, logical judgments, data traversal, code reuse, and data formatting and processing.

Variable display: the direct presentation of content

The most basic template operation is to display data. In AnQiCMS templates, we use double curly braces{{ 变量名 }}Output the value of the variable. These variables are usually data passed from the backend to the template, such as article titles, category names, system configurations, and so on.

For example, if we want to display the title of the current page's article, we can write it like this:<h1>{{ archive.Title }}</h1>Herearchive.TitleThis is the title attribute passed from the backend article object. This intuitive syntax allows us to easily embed data into the HTML structure.

Control flow: Implementing logical judgment

The display of website content often needs to be judged according to specific conditions.For example, images are displayed only when the article has a thumbnail, or different content is displayed based on the user's login status.The AnQiCMS template providesif/elif(else if) andelseTags to handle such logic.

The basic structure is as follows:

{% if 条件 %}
    <!-- 条件为真时显示的内容 -->
{% elif 其他条件 %}
    <!-- 第一个条件为假,此条件为真时显示的内容 -->
{% else %}
    <!-- 所有条件都为假时显示的内容 -->
{% endif %}

For example, we want to judge if there is a thumbnail on the article detail page and display it.

{% if archive.Thumb %}
    <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
{% else %}
    <p>暂无缩略图</p>
{% endif %}

This structure allows us to make flexible judgments based on the existence or absence of data, the size of the numbers, and boolean states, thus presenting differentiated content.

Traverse data: Display list content

There are a large number of list contents on the website, such as article lists, product lists, navigation menus, etc., which require the template system to traverse the data set. AnQiCMS template usesforLoop tags to handle such requirements.

forThe basic syntax of a loop is{% for item in 集合 %} ... {% endfor %}. Inside the loop,itemVariables will represent each element in the collection sequentially, and we can access their data by.item.属性.

A common use case is to display the latest article list:

{% archiveList articles with type="list" limit="5" %}
    <ul>
        {% for article in articles %}
            <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
        {% empty %}
            <li>暂无文章</li>
        {% endfor %}
    </ul>
{% endarchiveList %}

here,{% archiveList ... %}It is a special tag provided by AnQiCMS for retrieving article lists.{% empty %}The tag is inforThe loop executes when the data collection is empty, which is very suitable for handling user prompts when there is no data. In addition,forThe loop also providesforloop.Counter(current loop count) andforloop.Revcounter(remaining loop count) and other variables, which make it easier for us to have finer control, such as adding special styles to the first article.

Template reuse: improves development efficiency

When building a website, many page elements are common, such as headers, footers, sidebars, etc. The AnQiCMS template providesincludeandextendsTwo mechanisms are used to achieve template reuse, greatly improving development efficiency and the convenience of later maintenance.

  • includeTags:Used to embed a template file into another template file.This applies to reusable components on the page, such as header navigation, sidebar ads, copyright information, and so on.For example, introduce the header and footer in the main template file:

    {% include "partial/header.html" %}
    <div class="main-content">
        <!-- 页面主体内容 -->
    </div>
    {% include "partial/footer.html" %}
    

    includeIt can also be done throughwithPassing additional variables and usingonlyLimit the range of variables, increase flexibility.

  • extendsTags:Used to implement template inheritance. We can define a basic layout (base.html), which contains the overall structure, common styles, and scripts of the website, and usesblockThe tag defines the area that can be rewritten (overwritten) by the sub-template. For example, define a content block inbase.html:

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <title>{% block title %}默认标题{% endblock %}</title>
        <!-- 其他公共head内容 -->
    </head>
    <body>
        <header><!-- 页眉内容 --></header>
        <main>{% block content %}{% endblock %}</main>
        <footer><!-- 页脚内容 --></footer>
    </body>
    </html>
    

    The sub-template only needs to declare{% extends "base.html" %}Then proceed{% block block_name %}Rewrite the corresponding area:

    {% extends "base.html" %}
    {% block title %}我的文章 - AnQiCMS{% endblock %}
    {% block content %}
        <h1>欢迎阅读我的文章</h1>
        <!-- 具体文章内容 -->
    {% endblock %}
    

    This way ensures consistency in the overall style of the website, while allowing each page to have its unique content.

Filter: Format and process data.

Filters are an important tool in template languages for processing variable outputs. They allow us to modify, format, or convert the data before displaying it. Filters use the pipe symbol|After connecting to a variable, multiple filters can be chained.

For example,{{ 变量名|过滤器名称:参数 }}.

  • safe:Used to output unescaped HTML content. It is very critical when displaying the main text of an article (usually containing HTML format), to prevent HTML tags from being displayed as plain text.<div>{{ archive.Content|safe }}</div>
  • stampToDate:Convert the timestamp to a readable date format. This is a unique feature of AnQiCMS as it handles timestamps directly.<span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}</span>Here"2006-01-02 15:04"It is a date and time formatting reference value unique to the Go language.
  • truncatechars:N:Truncate a string to a length of N characters and add an ellipsis at the end. Often used to display article summaries.{{ article.Description|truncatechars:100 }}
  • add:Perform addition operations or concatenation on numbers or strings.{{ item.Views|add:" 次浏览" }}
  • lower/upper:Convert a string to lowercase/uppercase.

The filter greatly enhances the ability to process data in templates, making the front-end display more flexible and accurate.

AnQiCMS dedicated tag: deeply integrated with content data

In addition to the general syntax of Django template engine implemented in GoLang, AnQiCMS also provides many dedicated content tags that are closely integrated with the CMS backend data, used to directly obtain various types of content data, further simplifying template development.

  • archiveList/archiveDetail:Used to obtain a list of articles or the details of a single article. It can be filtered according to various conditions such as category ID, module ID, recommendation attributes, sorting method, etc.{% archiveList latestArticles with categoryId="1" limit="10" order="CreatedTime desc" %}
  • categoryList/categoryDetail:Used to get the list or details of a single category, and it also supports filtering by module ID and parent ID.{% categoryList subCategories with parentId=currentCategory.Id %}
  • pageList/pageDetail:Used to get the list or details of a single page.
  • navList:Used to get the website navigation menu.
  • system/contact/tdk:Used to get the system configuration, contact information, and page SEO (TDK) information.

These built-in tags allow us to gain an in-depth understanding of the database structure without the need, just through simple parameter configuration, we can call the required data in the template, greatly improving the efficiency of content operation.

In summary, the AnQiCMS template system, implemented using the Django template engine syntax in GoLang, provides website operators with powerful and flexible content display logic control capabilities. From basic variable output, to complex conditional judgments and data loops, to efficient code reuse mechanisms, as well as practical data formatting filters and dedicated tags deeply integrated with the CMS, all of which make the presentation of front-end content possible

Related articles

Do I want to use a unique display template for specific articles or categories, does AnQiCMS support this?

In website operation, we often encounter such needs: some articles or categories, due to the particularity of their content or marketing needs, we hope to give them a completely different display style from the rest of the website.This personalized display requirement is particularly important in today's increasingly rich content.So, does AnQiCMS support using a unique display template for specific articles or categories?The answer is affirmative, and it provides powerful and flexible features to support you in achieving this goal.Why do you need a dedicated template?\n\nImagine

2025-11-08

How to understand the template file structure of AnQiCMS and effectively manage the display layout of the website?

Manage the display layout of the website in Anqi CMS, the core lies in understanding its flexible template file structure and powerful tag system.This can not only give your website a unique visual style, but also greatly improve the efficiency of content operation and user experience. ### One, Anqi CMS template overview: the foundation for building a website Imagine that your website is a house, and the Anqi CMS template system is the design图纸 and construction tools for this house.It is based on the efficient backend of Go language, but it adopts syntax similar to the Django template engine in front-end templates

2025-11-08

What template types does AnQiCMS support to adapt to content display on different devices?

Whether website content can be seamlessly and beautifully displayed on various devices in today's multi-screen era is directly related to user experience and brand image.AnQiCMS as an enterprise-level system focusing on efficient content management, fully understands the importance of this demand, and therefore provides a flexible and diverse range of solutions in template support to ensure that your website content can be presented in a **state** on desktop computers, tablets, or mobile phones.To meet the needs from simple and efficient to highly customized, AnQiCMS meticulously designed three core website modes to handle the display of different devices

2025-11-08

How can I customize the overall interface and user experience of the AnQiCMS website?

A powerful and flexible set of tools is provided by AnQi CMS, allowing users to deeply customize the overall interface and user experience of the website, thereby creating a digital platform that is both beautiful and efficient.A customized website can not only better convey the brand image but also improve visitors' satisfaction and conversion rate through optimized user experience.This will elaborate in detail on how to achieve this goal by using the various functions of AnQiCMS.Flexible content presentation: The foundation of the interface. The website interface first presents the content.AnQiCMS provides high flexibility in content management

2025-11-08

How can you list the latest articles or products under a specified category in AnQiCMS?

Manage website content in AnQiCMS, often need to display relevant information according to different themes or sections.For example, you may want to display the latest articles under the "Company News" category in some area of the homepage, or show the latest products under the "Hot New Products" category in the sidebar of the product page.AnQiCMS provides flexible and powerful template tags, allowing you to easily implement these content call requirements.Understanding the content organization of AnQiCMS is the key to efficiently utilizing its features.The system distinguishes different types of content through the "content model", such as article models and product models

2025-11-08

What sorting and filtering options does AnQiCMS provide to control the display order of list content?

How to efficiently display and organize content in website operation directly affects the user experience and the efficiency of information transmission.For users who use AnQiCMS, the system provides various flexible sorting and filtering options to help us accurately control the display order of list content. Whether it is articles, products, or other custom content types, they can be presented to visitors in the most business-friendly way possible.The AnQi CMS has given the control of content display order to the user, which is mainly realized through a powerful template tag system and backend management settings.Through these features

2025-11-08

How to display a list of related articles based on the keywords or custom associations of an article?

In website content operation, providing users with a list of relevant articles can not only effectively extend the visitor's stay time on the website and reduce the bounce rate, but also through guiding users to discover more interesting content, optimize the internal traffic path, and have a positive impact on SEO performance.AnQiCMS (AnQiCMS) understands this need and provides flexible and powerful functions, allowing us to easily display lists of related articles based on keywords or custom associations.###

2025-11-08

How to call and display the custom fields of an article on the article detail page?

In Anqi CMS, the content display of the article detail page is highly flexible, especially for fields customized through the content model.These custom fields can help us manage and display different types of information more precisely, such as product prices, authors' origins, and property layouts, etc.How can you accurately call and display the content of these custom fields on the article detail page?This article will introduce this process in detail. ### 1. Custom field creation and filling: laying the content foundation In AnQi CMS

2025-11-08