AnQiCMS (AnQiCMS) is a highly efficient and flexible content management system, whose powerful template functions are the key to building personalized websites.In templates, defining and using variables flexibly allows us to easily handle and display various data, whether it is article titles, category information, or custom field content, all can be dynamically called through variables, making the website full of vitality.
Next, we will delve into how to define, retrieve, and use variables in AnQiCMS templates, helping everyone build and maintain websites more efficiently.
The Basics of Variables: Understanding and Declaration
In AnQiCMS template syntax, variables are placeholders used to store and display data.They allow our templates to receive data from the backend and render it to the page as needed.
1. Variable Output Format
AnQiCMS templates use double curly braces{{ 变量名 }}Output the value of the variable. When you need to display a specific data on the page, just place the variable name inside double curly braces. For example,{{ siteName }}The name of the website may be displayed.
When naming variables, it is usually followed by camel case naming method (CamelCase), which means the first letter of each word is capitalized, for examplearchive.Id/archive.Title.
2. Declare variables:{% with %}with{% set %}
In certain scenarios, we need to define our own variables within the template or store the output results of complex tags for repeated use. AnQiCMS provides two main ways to declare variables:
{% with ... %}Tags:withLabels allow you to define one or more variables within a specific code block. These variables are only valid in{% with %}and{% endwith %}between. This is very suitable forincludePass arguments with labels or use temporary variables within a local scope.{% with pageTitle="最新文章列表" %} <h1>{{ pageTitle }}</h1> {# pageTitle变量在此处有效 #} {% endwith %} {# pageTitle变量在此处已失效 #}You can also define multiple variables at the same time, separated by spaces:
{% with title="产品介绍" keywords="产品,详情" %} <title>{{ title }}</title> <meta name="keywords" content="{{ keywords }}"> {% endwith %}{% set ... %}Tags:setThe tag is used to declare a variable in the current template file, its scope starts from the declaration point and ends at the end of the template file.This is very useful for sharing data across different parts of the template.{% set welcomeMessage = "欢迎来到我们的网站!" %} <header> {{ welcomeMessage }} </header> <main> <p>{{ welcomeMessage }}</p> </main>Tip:
{% with %}It is more suitable to define local, temporary variables,{% set %}It is more suitable to define variables that are needed throughout the template. A reasonable choice can avoid name conflicts and improve code readability.
II. The source of variables: Where do the data come from
Variables in AnQiCMS are not created out of thin air, they are mainly obtained through built-in template tags to access backend data, or directly read from URL parameters.
1. Tag empowerment: Get data from built-in tags
AnQiCMS is built-in with rich tags to retrieve various data of the website.These tags usually handle data queries and logic internally and assign the results to a variable name you specify for use in the template.
Get single data:Many tags are used to get data for a single entity, such as
system(System settings),contact(Contact information),tdk(Page TDK),archiveDetail(Article Details),categoryDetail(Category Details) etc.These tags pass through
变量名称 with name="字段名称"in the form, directly assigning the value of a specific field to变量名称.{# 获取网站名称,并赋值给siteName变量 #} {% system siteName with name="SiteName" %} <p>网站名称:{{ siteName }}</p> {# 直接获取当前文章的标题 #} <h1>{% archiveDetail with name="Title" %}</h1>Get list data:Some tags are used to obtain data collections (such as article lists, category lists, navigation lists), which will assign an array or slice to a specified variable name. At this point, you need to use
{% for %}Loop tags to iterate through this variable, display each item one by one.{# 获取文章列表,并赋值给archives变量 #} {% archiveList archives with type="list" limit="5" %} <ul> {% for item in archives %} <li><a href="{{ item.Link }}">{{ item.Title }}</a></li> {% endfor %} </ul> {% endarchiveList %}In this example,
archivesis a list variable containing multiple articles.itemisfora temporary variable for each article in the loop.Get custom parameters:
archiveParamsYou can customize additional fields for articles or product models on the backend,archiveParamsLabel retrieval. This label will assign the data of custom fields as a list or map (map) to a variable, and you can iterate and display it as needed.{# 获取当前文章的所有自定义参数 #} {% archiveParams params %} <div class="custom-fields"> {% for field in params %} <p>{{ field.Name }}:{{ field.Value }}</p> {% endfor %} </div> {% endarchiveParams %}
2. URL parameters and global data
AnQiCMS template engine can also access some global data or retrieve parameters from the URL. For example, on the search page, you may need to retrieve the search keywords from the URLq. It is mentioned in the documentarchiveListThe tag is inqIf the parameter is not specified, it will automatically read the URL in itqThe query parameter implies that it is possible tourlParams.qaccess the URL parameters in this form.
{# 假设URL是 /search?q=安企CMS,这里可以获取搜索关键词 #}
<input type="text" name="q" value="{{ urlParams.q }}" placeholder="请输入搜索关键词">
The depth of variables: data processing and display
After obtaining the variables, we can further process and display them in various ways to make the page content more expressive.
1. Access variable properties: dot operator.
When a variable stores an object or structure (such asitema variable represents an article object), you can use the dot operator.Access its properties.
`twig {# item is an article object, accessed via dot notation its