AnQi CMS, an enterprise-level content management system built based on Go language, which provides strong support for content operators and enterprise users with its high efficiency, security, and flexibility.In AnQiCMS, content is not just static text and images, but through its carefully designed template engine, we can transform data into vivid and personalized web pages.Understanding how to use variables and control tags in AnQiCMS templates is a key step to unleashing its powerful customization capabilities.
AnQiCMS's template engine uses a tagging method similar to Django or Blade, which means developers familiar with these frontend template languages will feel very familiar.It renders dynamic content in a simple and intuitive manner..htmlStored neatly as a suffix/templateDirectory, while the accompanying styles, scripts, and images, etc., static resources are independently stored./public/static/Directory, maintaining a good organizational structure.
Variable: the magic that brings content to life.
In AnQiCMS template, variables are the main means of displaying dynamic data.Imagine if the title, article content, and product prices of a website all had to be manually modified, it would be so cumbersome.The introduction of variables is to solve this problem.
When you need to display a dynamic piece of information on the page, such as the title of the current article, the name of the website, or the contact phone number, you just need to enclose the variable name with double curly braces, like this: {{ 变量名称 }}. AnQiCMS's variable naming follows the camel case rule, that is, the first letter of each word is capitalized, for examplearchive.Titlerepresents the title of the article,system.SiteNameThis represents the name of the website. This clear naming style allows developers to quickly understand the content pointed to by the variable.
For example, if we want to display the title and publication time of a blog post, it might be written like this:
<h1>{{ archive.Title }}</h1>
<p>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</p>
Here,archive.TitleDirectly use the title field of the current article object.archive.CreatedTimeThe value obtained is a timestamp, and we cleverly use the built-in AnQiCMSstampToDateThe function (in AnQiCMS, it is considered a tag function), format it into the familiar "year-month-day" form. AnQiCMS has built-in rich tags to obtain different types of data, such assystemThe tag is used to retrieve global settings (such asSiteName/SiteLogo)contactThe tag is used to retrieve contact information (such asCellphone/Email)archiveDetailThe tag is specifically used to retrieve article details, and they can all be accessed throughnamethe parameter to specify the specific field to be retrieved.
In addition to directly outputting variables, AnQiCMS also provides powerful "Filters", allowing us to further process the output of variables. Filters are applied through the pipeline operator|for example{{ obj|filter__name:param }}.An English translation of the value is: An English translation of the value is: A common application scenario is to handle rich text content, and to prevent XSS attacks, the template engine defaults to escaping HTML tags.|safeThe filter to declare content as safe, allowing the browser to parse normally, such as{{ archive.Content|safe }}. In addition, there is|truncatechars:9Used to truncate strings and add an ellipsis,|lowerused to convert strings to lowercase,|add:2Used for addition of numbers and so on, these filters greatly enhance the flexibility of variable output.
Control tags: the skeleton of building page logic
Just having dynamic data is not enough, a fully functional website also needs to display different content based on different conditions, or iterate through list data.This is where the control tag takes effect.{% 标签 %}And most of them need corresponding end tags{% end标签 %}To define the scope clearly.
1. Condition judgment:if/elif/else/endif
ifTags are the foundation for implementing conditional logic. They can determine whether to render a piece of content based on the truth or falsity of an expression. For example, if an article has a thumbnail, we display it:
{% if archive.Thumb %}
<img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
{% else %}
<img src="{% system with name='DefaultThumb' %}" alt="默认缩略图">
{% endif %}
Here,{% if archive.Thumb %}will checkarchive.ThumbIs there or has a value. If true, the thumbnail of the article is displayed; otherwise,{% else %}a system default thumbnail will be displayed. In addition,{% elif %}Can be used to handle multiple parallel conditions, making logical judgments more refined.
2. Loop traversal:for/empty/endfor
The website is full of list data, such as article lists, product lists, navigation menus, etc.forTags are created to traverse these data. It can visit each item in arrays, slices, and other collections one by one, and make each item available within the loop body.
假设我们要展示一个最新文章列表:English
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<li>
<a href="{{ item.Link }}">{{ item.Title }}</a>
<span>阅读量:{{ item.Views }}</span>
</li>
{% empty %}
<p>当前没有任何文章发布。</p>
{% endfor %}
{% endarchiveList %}
In this example,{% archiveList archives with type="list" limit="10" %}是一个用于获取文章列表的控制标签,它将获取到的10篇文章数据赋值给Englisharchivesthe variable. Then,{% for item in archives %}The loop will iterate over thearchivesEach article is processed, and the current article is nameditem, then in<li>The link, title, and reading count are displayed in the tag.
It is worth mentioning that,forThe loop also supports some convenient properties and modifiers:
forloop.Counter:Get the current loop index (starting from 1).forloop.Revcounter:Get the remaining number of times in the current loop.reversed:Reverse iterate over the list.sorted:Sort and iterate over the list.empty:When the loop collection is empty,{% empty %}and{% endfor %}the content between the brackets will be rendered, very suitable for displaying 'no content' prompts.cycle:Alternating multiple values are output in the loop, for example{% cycle "even" "odd" %}The value can be alternately outputted as 'even' and 'odd' in each loop, often used to add different CSS classes to list items.
3. Variable assignment:with/set
Define some variables temporarily in the template, which can effectively improve the readability and reusability of the code.{% with %}Labels allow you to declare one or more variables within a block scope and{% endwith %}Between them use:
{% with pageTitle="安企CMS官网", pageKeywords="CMS,建站系统,Go语言" %}
<title>{{ pageTitle }}</title>
<meta name="keywords" content="{{ pageKeywords }}">
{% endwith %}
while{% set %}You can declare a variable at any position in the current template, whose scope is usually broader, until the current template rendering ends.
4. Modularization and Inheritance:include/extends/block/macro
To improve the reusability and maintainability of the template, AnQiCMS introduces the module