How to define temporary variables in AnQiCMS templates to control display according to requirements?

Calendar 👁️ 74

In website operation and content management, we often need to flexibly display content based on specific conditions, or temporarily process and store some data at some stage of the template.AnQiCMS provides a powerful and easy-to-use template engine, which draws on the syntax of Django templates, allowing us to conveniently define and use temporary variables, thereby achieving fine-grained control over page content.

AnQiCMS template files adopt.htmlSuffix, and supports Django template engine tag syntax. Variables are usually enclosed in double curly braces{{变量}}Print, while logical tags such as conditional judgment and loop control use single curly braces and percent signs.{% 标签 %}Define. Understanding these two basic syntaxes is the foundation for controlling display using temporary variables.

The variable definition method in AnQiCMS template

There are mainly two ways to define temporary variables in the AnQiCMS template:{% with %}Tags and{% set %}Tags. They each have their own characteristics and are suitable for different scenarios.

Use{% with ... %}Tags define temporary variables.

{% with ... %}Labels allow you to define one or more temporary variables within a specific code block. These variables are only available in thewithThe value is valid within the labeled area, and it will become invalid once it goes beyond this area. This feature of local scope allowswithThe tag is very suitable for use when temporary calculations or passing data to nested templates are needed, thus avoiding variable pollution of the global scope.

The basic syntax is:{% with 变量名="值" %}and it needs to be ended with{% endwith %}to end this code block.

For example, if you have a common page header templatepartial/header.html, you need to display different titles and keywords on different pages, you can use it like thiswithTags:

{# index.html 页面中 #}
{% with pageTitle="AnQiCMS首页 - 企业内容管理专家" pageKeywords="AnQiCMS, 企业建站, 内容管理" %}
    {% include "partial/header.html" with title=pageTitle keywords=pageKeywords %}
{% endwith %}

{# product.html 页面中 #}
{% with pageTitle="AnQiCMS产品详情 - 产品模型" pageKeywords="AnQiCMS产品, 内容模型, 解决方案" %}
    {% include "partial/header.html" with title=pageTitle keywords=pageKeywords %}
{% endwith %}

{# partial/header.html 模板内容 #}
<head>
    <title>{{ title }}</title>
    <meta name="keywords" content="{{ keywords }}">
    {# 其他页头内容 #}
</head>

In this example,pageTitleandpageKeywordsvariables in their respectivewithblocks are defined, and passed throughincludeThe tag passed toheader.htmlso thatheader.htmlThese temporary variables can be used to render page title and keywords.

Use{% set ... %}The tag defines variables

{% set ... %}Tags provide a more direct and flexible way to define variables. By usingsetThe defined variable can be used at any position in the current template (including all blocks after it) until the current template is rendered. This makessetAble to store values that need to be referenced multiple times in a template or processed in a complex way.

The basic syntax is:{% set 变量名 = 表达式 %}.

For example, you may need to truncate the title of the article obtained from the backend, or concatenate multiple strings and then store the result in a variable for multiple use:

{# 获取原始文章标题 #}
{% set originalTitle = archive.Title %}
{# 使用过滤器截断标题并赋值给一个新的变量 #}
{% set shortTitle = originalTitle|truncatechars:50 %}
{# 拼接字符串创建完整链接描述 #}
{% set fullLinkText = "点击查看更多关于"|add:shortTitle|add:"的详情" %}

<h1 title="{{ originalTitle }}">{{ shortTitle }}</h1>
<p><a href="{{ archive.Link }}">{{ fullLinkText }}</a></p>

In this example,shortTitleandfullLinkTextAll passsetTemporarily defined variables, which make the code more concise and the logic clearer.

How do temporary variables control display

The power of temporary variables lies not only in data storage, but also in their ability to combine with various logical tags of the AnQiCMS template engine, flexibly controlling the display and behavior of page elements.

1. Use conditional judgment ({% if ... %}) to control the display of content

The most common application is to combineiftags, comparing the truth value or value of variables to decide whether to display a certain content block.

Suppose we hope that the number of page views of the article reaches a certain amount

Related articles

How to automatically convert a plain text URL into a clickable hyperlink in content display?

It is crucial to provide users with a smooth and convenient browsing experience in website operation.When our articles, pages, or product descriptions contain URLs in plain text form, if users need to manually copy and paste to access them, it will undoubtedly greatly reduce their user experience.AnQi CMS is well-versed in this, providing a simple and efficient method to automatically convert these plain text URLs into clickable hyperlinks, thereby bringing the content to life.### Let plain text URLs automatically come alive: The hyperlink conversion technique in Anqi CMS Imagine that

2025-11-08

How to truncate the text or HTML content that is too long in the template and add an ellipsis to keep the page tidy?

It is crucial to maintain the neat and consistent layout of the page content in website operation.Especially when it is necessary to display abstracts of articles, product descriptions, or news titles, the length of the original content is often uneven, which is likely to cause page layout to be out of order and affect the reading experience of users.AnQiCMS provides very practical template filters that can help us easily truncate long text or HTML content and automatically add ellipses, keeping the page always elegant.### The Importance of Understanding Text Truncation Imagine, in a list of articles, some of the summaries are very short

2025-11-08

How to safely display user-submitted HTML content or JS code in a template to prevent XSS attacks?

In website operation, we often need to handle various types of content submitted by users, such as comments, messages, article submissions, etc.This content may contain HTML tags and even JavaScript code. If displayed directly on a webpage, it may introduce cross-site scripting attacks (XSS), posing a security risk to the website.AnQiCMS was designed with comprehensive consideration of content security and provides corresponding mechanisms at the template level to help us effectively prevent such risks.AnQi CMS uses a template engine syntax similar to Django

2025-11-08

Does AnQi CMS support direct arithmetic operations on strings or numbers within templates to affect display?

When building a website, we often need to handle various data, not just simply display it.For example, we may need to calculate the total price of goods, inventory surplus, or dynamically adjust the style of elements based on numerical size.These scenarios naturally lead to a core question: Does Anqi CMS support direct arithmetic operations on strings or numbers in templates, thereby flexibly affecting the display of content?The answer is affirmative. The AnQi CMS template engine cleverly adopts many advantages of the Django template, including direct support for arithmetic operations.

2025-11-08

How to display the current year or a custom formatted datetime in the template?

In website operation and content management, displaying accurate date and time is a basic and important requirement.Whether it is the copyright year in the footer, the publication or update date of the article, or the countdown of a specific event, flexible control of the date and time format can greatly enhance the user experience and professionalism of the website.In AnQiCMS, you can easily implement the display of the current year or a custom date and time format using several simple and practical methods.--- ### One: Display the current system year or date and time: Use `{% now %}`

2025-11-08

How to use the AnQiCMS random text generation feature to fill placeholder content during the development stage?

In the process of website project development, we often face a common challenge: how to effectively build and test the layout and style of a website in the absence of actual content?From design prototypes to frontend development, filling in real content is time-consuming and impractical, and simple placeholder text is difficult to simulate the visual effects of real content.

2025-11-08

How to solve the problem of page garbled display caused by incorrect encoding of AnQiCMS template files?

During the process of building a website with AnQiCMS, you may encounter situations where the page display appears garbled, which undoubtedly affects user experience and reduces the value of the carefully crafted page.Generally, such problems are caused by incorrect encoding settings in the template file.This article will detail how to troubleshoot and resolve the problem of garbled display caused by encoding issues in AnQiCMS template files, ensuring that your website content is clear and accurate.### Understanding the Root Cause of Garbled Characters Garbled characters, simply put, are the result of computers using the wrong encoding method to interpret data

2025-11-08

How to ensure that each site's content can be displayed independently in multi-site management?

In today's increasingly complex digital operation, many companies or individuals often need to manage multiple websites, which may be show sites of different brands, or independent content platforms for different markets or business lines.In response to such needs, AnQiCMS's multi-site management feature provides an efficient and powerful solution, allowing users to unify the operation and maintenance of multiple websites in a single system.However, while enjoying the convenience of management, ensuring that each site's content can be displayed independently without interference is one of the most concerns for operators.

2025-11-08