In the template development of AnQi CMS, mastering how to define and display temporary variables within templates is a very practical skill.It can help us handle data more flexibly, optimize template structures, and write simpler and more efficient code.Temporary variables are like the 'small tags' you use when creating a page, which can temporarily store some data for later use.
Why do we need temporary variables?
Imagine a scenario like this: you might need to get a value from some label, then perform a series of operations on this value (such as string slicing, formatting time), and finally use this processed result multiple times in different locations on the page.If there are no temporary variables, you may need to repeat these processes at each usage, which not only makes the code verbose but also reduces maintainability.The introduction of temporary variables is precisely to solve such problems, making data processing and display more centralized and efficient.
The template engine of AnQi CMS supports syntax similar to Django template engine, providing an intuitive way to define and use these temporary variables.
How to define a temporary variable in the template?
There are mainly two ways to define temporary variables in the AnQi CMS template:setTags andwithLabel.
1. UsesetLabel definition of variables
setLabels are the most direct and commonly used method, allowing you to declare a new variable at any position in the template and assign a value to it.This variable is available in the template file in which it is defined, including within the block it is located.
Basic syntax:
{% set 变量名 = 值 %}
Here, the 'value' can be a direct string, number, boolean, or even another variable, the output of a label, or data processed through a filter.
Example:We need to get the title of the current document and store it in a variable namedpageTitle:
{# 获取当前文档的标题,并存储到 pageTitle 变量 #}
{% set pageTitle = archive.Title %}
{# 进一步处理标题,例如截取前10个字符,存储到 shortTitle #}
{% set shortTitle = pageTitle|truncatechars:10 %}
<title>{{ shortTitle }} - {% system with name="SiteName" %}</title>
<h1>{{ pageTitle }}</h1>
In this example, we firstarchive.TitleAssign to.pageTitle, and then perform a cutting operation onpageTitle, and assign the result toshortTitlelike this,pageTitleandshortTitleIt can be reused anywhere in the subsequent template.
2. UsewithLabel definition of variables
withLabels are used to define one or more temporary variables within a specific code block.setTags are different,withThe variable defined by the label is only valid{% with %}and{% endwith %}Between valid, its scope is localized. This is very useful for scenarios where temporary variables are needed but it is not desired to pollute the global variable list, especially when used with the tag.includeusage.
Basic syntax:
{% with 变量名1=值1 变量名2=值2 %}
{# 在这里使用这些变量 #}
{% endwith %}
Example:Assuming you need to introduce a public page header templateheader.htmland you want to pass some specific variables to this page header:
{# 在 with 块内部定义两个变量 title 和 keywords #}
{% with title="安企CMS模板技巧" keywords="临时变量,模板开发,AnQiCMS" %}
<head>
<title>{{ title }}</title>
<meta name="keywords" content="{{ keywords }}">
</head>
{# 或者将这些变量传递给一个 include 模板 #}
{% include "partial/header.html" with title=title keywords=keywords %}
{% endwith %}
{# 在 with 块外部,title 和 keywords 变量不再可用 #}
PasswithLabels, we can clearly define the scope of variables, avoid name conflicts, and improve the readability and modularity of templates. When used withincludelabels, withThe label can effectively manage the data passed from the parent template to the child template.
How to display temporary variables?
Once a temporary variable is defined, displaying them is very simple. The Anqi CMS template engine uses double curly braces to output variable values.{{ 变量名 }}The syntax to output the value of a variable is.
Example:Continuing with the above example, displayingpageTitleandshortTitle:
{% set pageTitle = archive.Title %}
{% set shortTitle = pageTitle|truncatechars:10 %}
<!-- 在页面的任何地方显示 -->
<p>完整标题:{{ pageTitle }}</p>
<p>精简标题:{{ shortTitle }}</p>
If your temporary variable is a complex data structure, such as an object or an array (a struct or slice in Go language), you can use a dot..Access its properties or elements.
Example:Suppose you define a temporary variableuserInfoIt containsNameandEmailProperties:
{% set userInfo = {Name: "张三", Email: "[email protected]"} %}
<p>用户名:{{ userInfo.Name }}</p>
<p>联系邮箱:{{ userInfo.Email }}</p>
This is very useful for scenarios where you need to retrieve and store object data from tags and then access its internal properties.
Combine filter to handle data
The power of temporary variables also lies in their seamless integration with filters (Filters).The filter can perform various data processing on variables and assign the processing results to new temporary variables.
Example:Get the website name from system settings and convert it to lowercase:
{% set siteName = system.SiteName %}
{% set lowerCaseSiteName = siteName|lower %}
<p>原始网站名:{{ siteName }}</p>
<p>小写网站名:{{ lowerCaseSiteName }}</p>
You can even use filters directly when defining variables:
{% set formattedTime = stampToDate(archive.CreatedTime, "2006年01月02日 15:04") %}
<p>发布时间:{{ formattedTime }}</p>
Summary
In the template of AnQi CMS, defining and displaying temporary variables is the key to improving the flexibility and code organization of the template. Whether usingsetglobal definition, or making use ofwithPassing localization can help you better manage data streams, combined with powerful filters, it can easily achieve various complex data processing and display requirements.Reasonably utilize these techniques, and your security CMS website template will be more efficient, readable, and easy to maintain.
Common Questions (FAQ)
1.setTags andwithWhat is the difference between labels when defining temporary variables?
setThe variables defined by the tag are available throughout the current template file and any included sub-templates (local to the scope of the current template file).withThe variable defined by the label is only valid{% with %}and{% endwith %}Blocks between are valid, the scope is local.withTags are commonly used toincludeTags pass parameters to limit the scope of variables, to avoid naming conflicts.
2. What types of data can a defined temporary variable store?Temporary variables can store a variety of data types, including but not limited to:
- Basic types:String (string), integer (integer), float (float), boolean (boolean).
- Compound types:Arrays/slices, objects/structs, and maps. You can access them using a dot.
.or square brackets[]to access these compound type data members.
3. Can I use a filter on the defined temporary variables?{% set processed_text = my_variable|truncatechars:10|lower %}. This makes the data processing flow clearer and more controllable.