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

Calendar 👁️ 65

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.

Related articles

How to optimize page layout and content display by using the template inheritance (`extends`) tag?

In AnQiCMS, templates are the foundation for building the appearance and layout of a website.A well-designed template not only makes the website look professional and beautiful, but also greatly enhances the efficiency of content operation and the convenience of website maintenance.Among many powerful template tags, the `extends` (template inheritance) tag is undoubtedly one of the key tools for optimizing page layout and content display.It can help us build a unified and flexible website structure, making content operation and frontend development more effortless.### Understanding `extends`: The core of template inheritance `extends`

2025-11-08

How to define and call the `macro` tag in AnQiCMS template to display reusable code blocks?

AnQiCMS provides a flexible and powerful template system, making the display of website content efficient and beautiful.In template development, in order to enhance the reusability and maintainability of code, we often encounter the need to encapsulate a commonly used code snippet so that it can be called in different places.This is when the `macro` tag becomes the key tool to achieve this goal.It allows us to define reusable code blocks, like functions in programming languages.Why do we need the `macro` tag?

2025-11-08

How to avoid extra blank lines when template logical tags (such as if, for) are rendered on the page?

When developing templates in AnQiCMS, we often find that even though the template code itself looks neat, the final rendered HTML page may still contain some unexpected blank lines.These blank lines do not affect the page function, but may make the HTML source code look less tidy, and even in some extreme optimization scenarios, it may bring a slight increase in file size.For users who pursue code aesthetics and concise output, how to effectively avoid these redundant blank lines is a topic worth discussing.

2025-11-08

How to loop through a list of data in a template and display it (using for loop), supporting counting and reversing?

In Anqi CMS template, efficiently displaying list data is an indispensable part of website content operation.Whether it is to display the latest articles, product lists, category directories, or customized data sets, flexible looping through these data and fine-grained control can greatly enhance the performance and user experience of the website.The Anqi CMS provides a powerful and easy-to-use template engine, its `for` loop tag is rich in features, supporting not only basic iteration but also easily implementing counting, reversal, and more advanced operations.### Core Concept: `for`

2025-11-08

How to obtain and display the title and detailed content on the article detail page?

Manage website content in Anqi CMS, the article detail page is an important window for displaying core information to visitors.Whether it is corporate news, product introduction, or technical articles, clearly presenting the title and detailed content is the key to improving user experience and information communication efficiency.The Anqi CMS provides intuitive and powerful template tags, making content presentation easy and flexible. ### Understanding Anqi CMS Template Structure Anqi CMS template files are usually stored in the `/template` directory and follow a set of simple naming conventions.for the detail page of an article or product document

2025-11-08

How to correctly display images in the article content of AnQiCMS templates and support lazy loading of images?

In content operation, exquisite images can significantly enhance the attractiveness and reading experience of articles.However, image files are often large, and if not handled properly, they may slow down page loading speed, affect user experience, and even search engine rankings.AnQiCMS fully understands this, providing a flexible way to correctly display images in article content within templates, and supports lazy loading of images to balance aesthetics and performance.### Core: Display images in article content In the AnQiCMS template, to obtain the main content of the article, we mainly rely on `{%

2025-11-08

How to display the name, link, and hierarchy of the article category on the article detail page?

AnQi CMS is a flexible and efficient content management system that provides great freedom in content display.For a website, the article detail page not only needs to display the content of the article itself, but also the name, link, and even the hierarchical relationship of the category it belongs to, which are all important components for improving user experience, optimizing website structure, and promoting SEO.Clear categorization information can help users quickly understand the positioning of the article, facilitate their further browsing of related content, and also enable search engines to better crawl and understand the structure of the website.Next, we will discuss how to use the Anqi CMS article detail page

2025-11-08

How to retrieve and display the publish time, update time, view count, and comment count of an article?

## Mastering AnQi CMS: A Comprehensive Guide to Article Publishing and Interactive Data Display In website operation, clearly displaying the publication and update time of articles, popularity (page views), and reader feedback (number of comments) can not only effectively improve user experience but also enhance the authority and timeliness of the content.AnQiCMS (AnQiCMS) relies on its flexible template tag system to make the acquisition and display of this key information simple and efficient.This article will explain in detail how to implement these functions in Anqi CMS.### One

2025-11-08