Hello!As an experienced CMS website operation person, I am very happy to give you a detailed explanation of how to define and assign temporary variables in the CMS template of AnQi.This is crucial for the flexibility, readability, and maintainability of the template, helping us to organize and display website content more efficiently.
In the template system of AnQi CMS, we often encounter scenarios where we need to store data, calculate results, or prepare content for specific components within a local scope. To meet these needs, AnQi CMS provides a simple yet powerful variable definition mechanism, mainly realized through two tags: withTags andsetLabel.
Use flexiblywithLabel defines a local variable
withTags are powerful tools used in AnQi CMS templates to pass temporary variables within specific code blocks or to included templates. Their main feature is the scope restriction in{% with %}and{% endwith %}Between them, this makes variable management clearer, enabling effective avoidance of naming conflicts, especially suitable for preparing data for complex components or specific display areas.
When you need to temporarily define one or more variables in a part of the template, you can usewithLabel.For example, suppose you are designing a detail page of an article and need to define a title and some keywords for a specific sidebar module on the page, but these variables should not affect other parts of the page.
{% with sidebarTitle="热门推荐" sidebarKeywords="SEO优化,内容营销" %}
<aside class="sidebar-module">
<h2>{{ sidebarTitle }}</h2>
<p>相关关键词:{{ sidebarKeywords }}</p>
{# 这里可以放置使用 sidebarTitle 和 sidebarKeywords 的内容 #}
</aside>
{% endwith %}
In this example,sidebarTitleandsidebarKeywordsThese variables are only{% with %}and{% endwith %}within the tag wrapper<aside>The element is valid internally. Once you leave this area, these two variables will no longer be accessible, ensuring the locality and non-interference of variables.
withThe other common and very practical scenario of the label is to include the template (throughincludeLabel the template fragment passed in. This makes the template fragment more generic, allowing different content to be displayed based on the parameters passed in. For example, you have a generic header templatepartials/header.htmlwhere it may be necessary to display the page title and highlighted navigation items. In the main template, you can introduce and pass variables like this:
{% include "partials/header.html" with pageTitle="关于我们" activeNavItem="about" %}
Andpartials/header.htmlin the template, you can use them directly{{ pageTitle }}and{{ activeNavItem }}Render the corresponding content to achieve the reusability and dynamicity of the header template. You can pass multiple variables at once, just separate them with spaces.withafterward with spaces.key="value"and it's done.
UsesetLabel is defined as a variable in the current template
WithwithThe tag focuses on different local scope and parameter passing,setLabels are used to define variables within the current template file, and their scope extends from the point of definition to the end of the current template or to the end of any nestedblockLabels inside, can be inherited and used by its sub-blocks. This makessetThe tag is very suitable for storing some intermediate calculation results, temporary storage of values, or defining common variables for the entire template or most of its content.
Define a variable and assign it a value, the syntax is very intuitive:)
{% set totalViews = archive.Views + 100 %}
<p>文章总阅读量:{{ totalViews }}</p>
In this example, we define a variable namedtotalViewsthe variable, and set its value toarchive.Views(assuming this is the number of page views fetched from the backend) plus a fixed number.totalViewsThe variable can be called at any subsequent position within the current template.
setThe power of labels also lies in their ability to store more complex results, such as the output of other template tags or data processed by filters. For example, if you need to format a timestamp and store the result for multiple uses, you can do it like this:
{% set publishTime = stampToDate(archive.CreatedTime, "2006年01月02日 15:04") %}
<p>发布于:{{ publishTime }}</p>
<p>本文更新于:{{ publishTime }}</p>
Thus, you only need to calculate the formatting time once, and you can refer to it multiple times in the template.publishTimeThis avoids duplicate code, improves the efficiency and maintainability of the template. In addition,setThe variable defined by the label is usually available in the child template that inherits the parent template after it is defined in the parent template, which provides great convenience for constructing hierarchical template structures.
In summary,withTags andsetLabels are the effective way to define temporary variables in the Anq CMS template, each with its focus:withSuitable for local scope and passing parameters toincludethe template,setIt is more suitable to store and reuse variables in the current template and its sub-blocks.Understand and make good use of these two tags, which will help you build more flexible, efficient, and easy-to-maintain security CMS website templates.
Common Questions (FAQ)
问:I can use a variable defined in a parent template inseta child template that inherits it?答:Yes, in most cases, you can use a variable in the parent template that{% set %}The defined variables can be used directly in child templates that inherit the parent template.This means that any value calculated or defined in the parent template can be conveniently accessed and rendered in the child template, thus achieving data sharing and transmission.
问:如果我同时使用withandset标签定义了同名的变量,哪个会生效?Answer: In{% with %}标签所包裹的代码块内部,withLabels defined with the same name will take precedence because they have a more local scope. Once it leaves{% with %}Code block, if it exists with the samesetvariable, thensetThe variable will be re-enabled. It is recommended to use descriptive variable names to avoid potential confusion.
Q: Can I usesetorwithto store other template tags (such asarchiveDetail) for the output results?答:Certainly. This issetandwitha very useful aspect of tags. You can assign the result of any template tag or filter to a variable. For example,{% set articleTitle = archiveDetail with name="Title" %}The article title will be stored inarticleTitlethe variable, making it convenient to use in templates later. This greatly enhances the flexibility and maintainability of the template.