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