When using Anq CMS to build a website, templates are the key to dynamic content display. We often see two special syntax structures in template files:{{变量}}and{% 标签 %}.Although they are all related to the display and processing of data, they play different roles and are used in different ways.Understanding the difference between them is the first step to efficiently using the Anqi CMS template.
{{变量}}: Placeholder for dynamic content output
Imagine{{变量}}It is like a fill-in-the-blank question on a web page.Its main function is to directly output the data passed from the backend to the web page.When you want to display dynamic information such as article titles, website names, and user comments on a page, this syntax is used.
For example, if you want to display the current article title on a page, you might see something like this:{{archive.Title}}. Here,archiveIt may represent the current article object, andTitlewhich is the title attribute of the article object. Similarly, to display the website filing number, you can directly use{{system.SiteIcp}}. Through the dot (.), we can delve into the properties of the object layer by layer.
It is worth mentioning that variables can also be processed through filters before output, such as truncating text, converting case, and formatting dates, etc. For example,{{ item.Title|truncatechars:10 }}It will truncate the first 10 characters of the article title and output. And like{{stampToDate(item.CreatedTime, "2006-01-02")}}This style, although it looks like a function call, it is still based on a variable, and it formats the value (timestamp) through a built-in function, finally outputting the processed variable content.These filters make the output of variables more flexible and diverse, meeting various display requirements.
{% 标签 %}: Control logic and instructions to obtain complex data
Compared with that,{% 标签 %}It is more like a set of instructions or a functional module.It does not directly output data, but is used to control the logical structure of templates, execute specific operations, or retrieve complex data sets.The core lies in 'control' and 'function'.
{% 标签 %}Is very widely used, for example:
- logical judgment: When you need to decide whether to display a certain content based on a condition, you can use
{% if 条件 %}...{% endif %}For example,{% if archive.Id == 10 %}这是文档ID为10的文档{% endif %}specific content will be displayed when the document ID is 10. - Loop throughWhen you need to iterate over a list or array and render each item repeatedly, you will use
{% for item in list %}...{% endfor %}For example,{% for item in archives %}You can loop through each article in the article list. - Data acquisition and processing: Anqi CMS provides a rich set of built-in tags to retrieve specific types of data sets. For example,
{% navList navs %}tags to retrieve the navigation list of the website,{% archiveList archives with type="page" %}It is used to obtain a paginated list of articles. These tags are usually needed to be used in conjunction{% for %}in a loop to iterate and use{{变量}}to output the specific content. - Other auxiliary functions: Some tags are used for the organization structure of templates, for example
{% include "partial/header.html" %}Used to introduce other template file fragments,{% extends 'base.html' %}For template inheritance, etc.
These tags often appear in pairs, for example{% for %}Always with{% endfor %}End,{% if %}Always with{% endif %}End, clearly defining their scope.
The core difference: content and instructions
Fundamentally,{{变量}}and{% 标签 %}The core difference lies in the roles they play:
{{变量}}The task isDisplay data. It is a pure output mechanism, displaying the values provided by the backend directly on the page, or after simple filtering.{% 标签 %}The task isExecute commands, control logic, or retrieve complex dataIt is a functional structure used to build the logical skeleton of a page, handle data flow, rather than directly display a specific value.
In simple terms,{{变量}}is the concrete embodiment of content, while{% 标签 %}Is a tool and set of rules for handling, organizing, and determining when and where content appears.Mastering the essence of these two grammars allows you to effortlessly control the Anqi CMS template system, whether it is to display simple content or build complex dynamic pages, you can do it with ease, making website operation twice as efficient.
Frequently Asked Questions (FAQ)
Question:{{变量|过滤器}}and{% 标签 参数 %}Can handle data, what are the differences?
Answer: {{变量|过滤器}}Mainly forAlready existsFormat or convert a single variable value, its role is to beautify and adjust the display of data.For example, shorten a long article abstract or convert timestamps to readable date formats.The processing range is usually limited to the value of the variable itself.
And{% 标签 参数 %}Has a more extensive function, it may be used forObtainData (for examplearchiveListGet the list of articles, perform complex logical judgments (for example)ifsentences), or generate structured content (for example)paginationGenerate pagination navigation).The function is to organize and control the display of content, sometimes it also includes internal data processing logic, but its core is 'function' or 'control', rather than just the 'formatting' of a single variable.
Question: All tags are required{% end标签 %}?
Answer:Tags are not required for all. Usually, used to control logical flow (such asif/for) or handle complex data sets (such asnavList/archiveListThe tag needs an ending tag to define its scope. But some tags, such as{% system %}or{% contact %}(when they directly output field values), or like{% lorem %}This tag for generating random content does not require an end tag. Whether an end tag is needed is usually specified in the AnQiCMS tag usage documentation.
Ask: Can I customize new{{变量}}or{% 标签 %}?
Answer:As a regular user, you usually cannot customize the new core directly in the template file{{变量}}or{% 标签 %}syntax itself.{{变量}}The value is usually provided by the backend code, while{% 标签 %}It is a system built-in control structure or functional module.
However, Anqi CMS provides the function of "custom content model fields" and "custom setting parameters", and you can use these fields defined in the background to expand{{变量}}The content, such as adding a custom field for the "author biography" in the template, and then passing through{{archive.AuthorIntro}}Call. Or use existing tags and filters to create a more personalized display, such as using first{% archiveList %}Get the list of articles, then use{% for %}Loop through and pass through{{ item.Title|upper }}To display uppercase titles and achieve customization effects.