How to declare a temporary variable in AnQiCMS template and use it to display content, improving template flexibility?

In Anqi CMS template creation, we often need to display various dynamic content.It is crucial to be proficient in using temporary variables to make the template more flexible and the code more concise.The AnQi CMS template engine provides powerful functionality for declaring temporary variables, which helps us better organize and process data, thus improving the efficiency and maintainability of content display.

Understanding the value of temporary variables

Imagine that you need to display specially processed data at multiple locations on the page, or some data is repeatedly used in conditional judgments, or the data obtained from a label needs to be further processed before it can be presented.If you call the label or write a complex expression repeatedly, the template code will become long and difficult to read.At this point, temporary variables can be put to use. By storing intermediate results or pre-processed data in temporary variables, we can not only make the code clearer, but also improve the execution efficiency of the template, because the data only needs to be processed once.

The AnQi CMS template engine provides two main ways to declare temporary variables:{% with %}Tags and{% set %}Tags. They have different focuses and are suitable for different scenarios.

Use{% with %}Declare local variables with tags

{% with %}Tags are used to declare temporary variables within a specific code block, with a scope limited to{% with %}and{% endwith %}This means that the variables you define within this block will not affect the variables with the same name outside the block, nor will they pollute the global variables.This is very useful for scenarios where you need to isolate variables and avoid naming conflicts.

You can declare one or more variables at once, separated by spaces, using变量名="值"The form. For example, if you want to use a custom title and keywords in a local area:

{% with pageTitle="AnQiCMS 模板指南" pageKeywords="临时变量,模板,AnQiCMS" %}
    <header>
        <h1>{{ pageTitle }}</h1>
        <meta name="keywords" content="{{ pageKeywords }}">
    </header>
{% endwith %}
{# 在这里,pageTitle 和 pageKeywords 变量就无法使用了 #}

{% with %}A common use case for the tag is to include other template files:{% include %}Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can... Sometimes. Sometimes, you want to pass some specific data to the template that is being included, but you do not want the included template to directly access all the variables of the current template. In this case, you can...includeAfter the label usingwithKeyword, and optionally addonlyKeyword to limit variable passing:

{# 假设 partial/header.html 需要一个 title 变量 #}
{% include "partial/header.html" with title="我的自定义页面标题" only %}

Thus,partial/header.htmlThe template can only access totitleThis variable, which will not inherit the other context data of the main template, thus improving the modularity and maintainability of the module.

Use{% set %}Label declares a universal variable

with{% with %}The tag is different,{% set %}Variables declared by tags usually have a scope that is the entire template file or the block where they are located{% block %}This makes{% set %}it more suitable for sharing data between different parts of the template.

{% set %}The use of tags is very intuitive, you can assign a value directly to a variable, or assign the result of an expression or a filter to a variable. For example, we often need to get the first image from the document's image list as a thumbnail, or format the timestamp and display it:

{# 假设 archive.Images 是一个图片URL数组 #}
{% set firstImage = archive.Images[0] %}
{% if firstImage %}
    <img src="{{ firstImage }}" alt="{{ archive.Title }}">
{% endif %}

{# 格式化文档创建时间 #}
{% set formattedTime = stampToDate(archive.CreatedTime, "2006年01月02日 15:04") %}
<p>发布时间:{{ formattedTime }}</p>

{% set %}The power of tags lies in their ability to be combined with various tags and filters provided by Anqi CMS. For example, when you need to extract fromcategoryDetailorarchiveDetailWhen retrieving a specific field value from a label and performing further processing:

{% categoryDetail currentCategory with name="Id" %}
{% set categoryId = currentCategory %} {# 将标签的输出结果赋给变量 #}

{# 现在可以使用 categoryId 变量进行其他操作,比如获取该分类下的文档列表 #}
{% archiveList articles with categoryId=categoryId limit="5" %}
    {# ... 循环显示文章 ... #}
{% endarchiveList %}

Or, you may need to render rich text content obtained from custom parameters from the background to ensure the correct display of HTML structure:

{% archiveParams params with sorted=false %}
    {% set introContent = params.introduction.Value %}
    {% if introContent %}
        <div class="intro-block">{{ introContent|render|safe }}</div>
    {% endif %}
{% endarchiveParams %}

HereintroContent|render|safecombination, first throughrenderThe filter will convert any existing Markdown format to HTML, then throughsafeThe filter ensures that HTML code is correctly parsed and displayed by the browser, rather than being output as escaped plain text.

Flexible application of **practice

  1. Define the scope clearly:Understand{% with %}(Local) and{% set %}(Global/Block) scope difference, choose the most suitable tag. For variables used only within a small block, prefer{% with %}Shared variables that need to be used throughout the template or in a main area{% set %}.
  2. Data preprocessing:Store filtered, truncated, or calculated data in variables to avoid repeated logic during display, making the code in the display part more concise.
  3. to enhance readability:Break down complex expressions into multiple temporary variables, which can significantly improve the readability of template code.
  4. Avoid overusing:Although temporary variables are very useful, overuse may also lead to overly complex template logic, making it difficult to maintain.In the template, the principle of maintaining data display and a small amount of logic should be followed, and more complex business logic should be placed on the backend.

By flexible application{% with %}and{% set %}These two ways of declaring temporary variables allow you to create more flexible, efficient, and easy-to-maintain Anqi CMS templates, thus better controlling the display of website content.


Frequently Asked Questions (FAQ)

Q1:{% with %}and{% set %}What is the core difference in scope of tags?A1:{% with %}Variables declared in tags are only in their{% with %}and{% endwith %}Valid inside the block, it cannot be accessed outside of it. It is mainly used to create temporary, local contexts and{% set %}The variable declared by the tag has a scope within the current template file or the block{% block %}It is valid within the block and can be used at any position after declaration until the template parsing is complete.

Q2: Can the output result of the AnQiCMS tag or the processing result of the filter be stored in a temporary variable?A2: Yes. Use{% set %}Labels can conveniently use AnQiCMS tags (such as{% system %}or{% categoryDetail %}) output results, or after various filters (such as|stampToDate/|truncatecharsProcessed data, assigned to a temporary variable. This helps in further processing or reuse in templates.

Q3: How to determine if a temporary variable exists or is empty in a template and provide a default value?A3: You can use{% if variableName %}To determine if a variable exists and is not empty. At the same time, the template engine of Anqi CMS provides|defaultand|default_if_nonefilter.{{ variableName|default:"默认值" }}you canvariableNamedisplay 'default value' when it does not exist or is an empty string, zero value; and{{ variableName|default_if_none:"默认值" }}It is specifically used to determine whether a variable isnil(null pointer), if it is, it will display the default value, which is particularly useful for handling possible returnsnilof the backend data.