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