English CMS (EnglishCMS) is powerful and intuitive in content presentation with its efficient architecture based on the Go language and flexible template mechanism.In website content operation, we often need to display dynamic information, such as article titles, product prices, contact information, etc., which cannot be separated from defining and using variables in templates.Understanding how to flexibly use variables in the AnQiCMS template is the key to building and maintaining an efficient, personalized website.
The template engine of AnQi CMS draws on Django's syntax, making the definition and reference of variables very easy to understand.基本上,任何需要动态显示的数据,无论是来自系统配置、数据库内容,还是我们临时计算的值,都可以通过变量的形式在模板中进行操作。{{ 变量名 }}Appears in the form, while performing logical judgment or loop operations, single curly braces and percentage signs will be used{% 标签名 %}.
Understand the source of variables in the template
In AnQiCMS template, the sources of variables mainly have three kinds:
Variables provided automatically by the system: In accessing a specific page, the system will automatically pass some core data to the template, which can be used directly in the template. For example, in the article detail page,
archiveThe variable will contain all the information of the current article (such asarchive.Title/archive.Content);On the category list page,categorythe variable represents the data of the current category. In addition, likepagesSuch variables will carry pagination information, no need for us to manually obtain it.Variables obtained and defined through built-in tags: 这是我们最常用来获取和显示特定数据的方式。AnQiCMS提供了丰富的内置标签(例如
system/contact/archiveList/categoryListThese tags can not only query data on demand but also assign the query results to a custom variable name we define, for further use in the template.For example, if you want to display the website name in the footer, you can directly call the system settings label:
<div>网站名称:{% system with name="SiteName" %}</div>If you need to use this website name multiple times or want to process it further, it is best to assign it to a variable:
{% system siteNameVar with name="SiteName" %} <div>网站名称:{{ siteNameVar }}</div>This is,
siteNameVarThis includes the website name, and we can refer to it multiple times in the subsequent template code. In cases where we need to obtain list data, such as an article list, we will define a variable in this wayarchivesStore the query results:{% archiveList archives with type="list" categoryId="1" limit="10" %} {% for item in archives %} <p><a href="{{ item.Link }}">{{ item.Title }}</a></p> {% endfor %} {% endarchiveList %}Here,
archiveshas become an array containing multiple articles, we go through:forloop through it, and through:item.Linkanditem.Titleaccess the link and title of each article.Custom variables defined within the template: Sometimes, we need to define some temporary variables in the template to store calculation results, phrases, or pass data to other modules. AnQiCMS provides two commonly used methods:
{% with ... %}{% endwith %}This kind of variable defined only in thewithtag's internal, usually used toincludepass parameters to the tag, or define temporary variables in a local code block.{% with greeting="Hello AnQiCMS User!" %} <p>{{ greeting }}</p> {% endwith %} {# 在这里,greeting 变量将不再有效 #}{% set ... %}:setThe variable defined by the label has a wider effective range within the current template file, starting from the definition position to the end of the template.{% set pageTitle = "关于我们" %} <title>{{ pageTitle }}</title> {# 稍后在页面其他地方依然可以使用 pageTitle #} <p>欢迎来到{{ pageTitle }}页面。</p>These two methods have their own focus,
withEmphasizing scope isolation,set更适合在整个模板中共享数据。
访问变量的属性和调用方法
一旦变量被定义,我们就可以使用点(.)symbol to access its internal properties or fields. For example, ifarchiveis a variable of the article object, then{{ archive.Title }}will display the article title,{{ archive.CreatedTime }}then it will display the creation time.
For variables of list or array type, we usually combine{% for ... in ... %}{% endfor %}loop labels to iterate over each element and access the properties of each element within the loop:
{% archiveList latestNews with type="list" limit="5" %}
<ul>
{% for newsItem in latestNews %}
<li><a href="{{ newsItem.Link }}">{{ newsItem.Title }}</a></li>
{% endfor %}
</ul>
{% endarchiveList %}
In this example,latestNewsis a list variable for articles, in the loop,newsItemrepresents each article object in the list.
AnQiCMS's template supports directly calling public methods defined in Go language structs. For example, if the article objectitemThere is a namedGetThumb()The method to get the thumbnail, we can directly call it in the template like this:
<img src="{{ item.GetThumb() }}" alt="{{ item.Title }}">
Use filters to process variables
AnQiCMS template engine is built-in with a rich set of filters (Filters), which allow us to perform various operations on variable values, such as formatting, truncating, escaping, etc. Filters are used through the pipe character|Connected to the variable.
Some commonly used filters include:
|safe: When the variable content includes HTML code (such as article details)Contentfield) use|safeIt can prevent the template engine from escaping it, allowing HTML code to render normally.<div>{% archiveDetail articleContent with name="Content" %}{{ articleContent|safe }}</div>|stampToDate:"格式"It is used to format timestamps into readable date or time strings.<p>发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</p>|truncatechars:数字:Truncate the string to a specified length and add an ellipsis at the end.<p>{{ archive.Description|truncatechars:100 }}</p>|default:"默认值":If the variable has no value or is empty, display a default value.
The filter greatly enhances the flexibility of data processing in templates, allowing us to present more diverse content on the front-end without modifying the back-end logic.<p>作者:{{ archive.Author|default:"佚名" }}</p>
Summary
Define and use variables in AnQiCMS is a core step to move from static pages to dynamic content display.Whether it is to quickly obtain data through the variables built into the system, or to customize queries using built-in tags, or to define temporary variables directly in the template, it all provides us with great flexibility.Combine dot notation to access properties, iterate over data, and apply filters for formatting, and we can build feature-rich, excellent user experience websites.Master these variable usage skills, and it will make your content operation work more at ease.
Common Questions (FAQ)
How to view the data and structure contained in a variable?
If you are uncertain about the internal structure or data contained in a variable during template development,{{ 变量名 | dump }}Filter. It will directly print the detailed structure of the variable on the page