In the template design of AnQi CMS, we often encounter scenarios where we need to temporarily store or pass data. To make the template code clearer, more readable, and more powerful, AnQi CMS provides a mechanism for defining and using custom variables, of which the most commonly used is{% with %}Tags and{% set %}Tags. They can help us flexibly handle data in templates, avoiding repeated calculations or passing complex objects.
Understanding the basics of variables in AnQiCMS templates
In AnQiCMS templates, data display is mainly achieved through double curly braces{{ 变量名 }}. For example,{{ archive.Title }}The content will display the current article's title. When we need to perform logical control (such as conditional judgments, loops) or define temporary variables within the template, we will use single curly braces and the percent sign.{% 标签名 %}The introduction of custom variables is based on this mechanism.
The `{% with %}` tag: magic of temporary variables.
{% with %}Tags are used to temporarily define one or more variables within a specific code block in a template. These variables are only valid within this{% with %}and{% endwith %}valid between, once out of this range, they will become invalid. This scope limitation, makes{% with %}Very suitable for local optimization or passing specific parameters to included template fragments.
Its basic usage is like this:
{% with variable_name="值", another_variable="另一个值" %}
<p>这个标题是:{{ variable_name }}</p>
<p>另一个变量是:{{ another_variable }}</p>
{% endwith %}
{# 在 {% endwith %} 之外,variable_name 和 another_variable 将不再可用 #}
In actual application,{% with %}One powerful aspect of tags is that they can be combined with{% include %}. Suppose we have a generic header (header) template filepartial/header.htmlWe hope it can display the title and keywords of the current page, but this information may vary from page to page. At this time, we can do it by introducingheader.htmlwhen{% with %}Pass these dynamic data:
{# 当前页面模板,例如 index.html #}
{% with page_title="首页", page_keywords="AnQiCMS, 模板, 变量" %}
{% include "partial/header.html" %}
{% endwith %}
{# partial/header.html 中的内容可能如下: #}
<head>
<title>{{ page_title }} - 站点名称</title>
<meta name="keywords" content="{{ page_keywords }}">
</head>
In addition,{% with %}It also supports usingonlykeywords to restrict the access of the included templates{% with %}Variables defined within the template cannot access the other variables of the parent template, which helps to reduce variable conflicts and improve the independence of the template:
{% with dynamic_image="/path/to/dynamic.jpg" only %}
{% include "partial/image_gallery.html" %}
{% endwith %}
{# 此时 image_gallery.html 只能访问 dynamic_image,不能访问其他变量 #}
The `{% set %}` tag: Flexible local variables
With{% with %}different,{% set %}tags are used to define variables in a larger scope within the current template. It does not{% with %}Such explicit code block limits, once defined, variables are available in{% set %}the template content after the tag until the template rendering ends. This makes{% set %}It is very convenient when you need to store calculation results, simplify complex expressions, or reuse a value in multiple places in templates.
{% set %}The basic syntax is very intuitive:
{% set variable_name = "要赋的值" %}
<p>我的名字是:{{ variable_name }}</p>
{% set calculated_value = 10 + 20 %}
<p>计算结果是:{{ calculated_value }}</p>
{# 你也可以将标签的输出赋值给变量,例如使用过滤器 #}
{% set truncated_description = archive.Description|truncatechars:100 %}
<p>{{ truncated_description }}</p>
For example, in a document list, we may need to dynamically adjust the display style based on the length of the article title. We can first use{% set %}The length of the storage title, then use this variable in conditional judgment:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
{% set title_length = item.Title|length %}
<li {% if title_length > 30 %}class="long-title"{% endif %}>
<a href="{{ item.Link }}">{{ item.Title }}</a>
<p>标题长度:{{ title_length }}</p>
</li>
{% endfor %}
{% endarchiveList %}
{% set %}It is also very useful when handling the filter chain.If you have a complex filter combination, you can store intermediate results to improve code readability or to further process the value at a later time.
When to use which tag?
Choose{% with %}Or{% set %}, mainly depending on the purpose and scope of the variable you define:
- Use
{% with %}: When you need to pass a temporary variable within a small block of a template, or to{% include %}a template fragment,{% with %}It is a better choice. It clarifies the lifetime of variables, which helps prevent variable pollution and improve the clarity of local code. - Use
{% set %}When you need a variable to be accessed or reused in multiple places within the current template.{% set %}It is more suitable. It is usually used to store computation results, simplify long expressions, or as a flag for the local state of the entire page.
In general,{% with %}and{% set %}They are valid tools defined in the AnQiCMS template for custom variables.Using them reasonably can greatly enhance the maintainability, flexibility, and readability of template code, making your AnQiCMS website content display more outstanding.
Common Questions (FAQ)
Q1:{% with %}and{% set %}What are the core differences between the variables defined?A: The core difference lies in the scope of variables.{% with %}The variables defined only in{% with %}and{% endwith %}code blocks are effective between them, which is block scope. And{% set %}Defined variables are available throughout the rest of the template after they are defined, which is the template scope.
Q2: Can I{% for %}use{% set %}define variables inside a loop?A: Of course, you can.{% for %}use{% set %}A variable defined, whose scope will start from the point of definition and end at the loop.Each time the loop iteration occurs, this variable can be reassigned or updated, which is very useful for cumulative calculations or processing intermediate data within the loop.
Q3: If I define a variable but do not use it in the template, will an error occur?A: Generally speaking, defining variables in AnQiCMS templates but not using them in the template will not cause runtime errors.The template engine will simply ignore variables that are not referenced.However, for the clarity and maintainability of the code, it is recommended to define only those variables that are actually needed.