As an experienced website operations expert, I know that understanding the meaning and usage scenarios of template variables is crucial for efficient website operation in a content management system.AnQiCMS (AnQiCMS) offers a flexible and powerful template engine, providing great convenience for content display.{{item.Title}}and{{archive.Title}}These two seemingly similar variables cause confusion.Today, we will delve into the meanings, usage contexts, and how to skillfully use them to build dynamic and accurate content displays.

The foundation of the Anqi CMS template engine

Before delving deeper into these two variables, let's briefly review the template mechanism of AnQi CMS. The template syntax of AnQi CMS is similar to Django, which uses double curly braces{{ 变量 }}Output a variable value, using single curly braces plus percent sign{% 标签 %}To handle logic control, for example, loops (for), conditional judgments (if)et al. This design makes the template structure clear, easy to understand and maintain. It is worth mentioning that according to the update log of AnqiCMS,archiveThe tag is inv2.1.1version has replaced the originalarticle/producttag, which indicatesarchiveIt has become the core concept representing 'document' or 'content item' in the system.

{{item.Title}}: Each shining star in the list

When you need to display a series of contents on a website, such as a list of blog articles, product categories, friend links, or sidebar recommendations,{{item.Title}}it will become your powerful assistant.

itemVariables usually appear in the template'sloop (for) structureinside. It represents the single content item in the current loop iteration of.Imagine you are usingarchiveListtags to get the latest list of articles:

{% archiveList latestArticles with type="list" limit="5" %}
    {% for item in latestArticles %}
        <div class="article-preview">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>{{ item.Description|truncatechars:100 }}</p>
            <span>发布于 {{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
        </div>
    {% endfor %}
{% endarchiveList %}

In this example,latestArticlesis a collection of article lists. Whenforyou iterate through this collection in a loop, each iteration will take the currentArticle objectAssign toitemVariable. Therefore,{{item.Title}}It will dynamically output the title of the article reached by the current loop,{{item.Link}}Then output its link,{{item.Description}}Output its description.

Not limited toarchiveList, other tags used to obtain list data, such ascategoryList(Category list),tagList(Tag list),pageList(Single page list) andnavList(Navigation list) and theirforwill be used inside the loopitemrepresents each independent list element. It acts as a temporary container, carrying the complete information of the current element at every moment of the loop.

{{archive.Title}}: Identity identification of the core content of the page.

withitem.TitleThe dynamic, iterative characteristics are different,{{archive.Title}}Carrying a more core and unique responsibility - it represents the currentThe main content (document) carried by the pageTitle.

This variable is mainly used inDocument detail page(For example, accessingarticle/123orproduct/abcsuch a page is effective in the context. On these pages,archivethe variables have been assigned the complete data of the current document by the AnQi CMS system. Therefore,{{archive.Title}}This will directly output the title of the article or product you are currently visiting.

Consider a template structure for an article detail page:

{# 假设这是文章详情页的模板(例如 /template/default/article/detail.html)#}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>{{ archive.Title }} - {{ system.SiteName }}</title>
    <meta name="keywords" content="{{ archive.Keywords }}">
    <meta name="description" content="{{ archive.Description }}">
</head>
<body>
    <header>
        <h1>{{ archive.Title }}</h1> {# 页面主标题 #}
        <p>分类:<a href="{{ archive.Category.Link }}">{{ archive.Category.Title }}</a></p>
        <p>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}</p>
    </header>
    <main>
        <div class="article-content">
            {{ archive.Content|safe }} {# 输出文章正文,使用safe避免HTML转义 #}
        </div>
    </main>
    <footer>
        <p>&copy; {{ now "2006" }} {{ system.SiteName }}</p>
    </footer>
</body>
</html>

Here, {{archive.Title}}It directly corresponds to the article title of the current page. It is usually used to set the page<title>tags (crucial for SEO), the main title within the<h1>page, and filling in metadata (such askeywordsanddescriptionIt is the most direct statement of "What is" on the current page.

Context is king: the key to distinguishing between them.

UnderstandingitemandarchiveThe key is to grasp their context.Context (Context).

  • item.TitleIsLocal variable, its lifecycle and scope are limited to theforloop internal. Every time the loop comes to a new data item,itemit will be updated,TitleThe value will also change. It is used to handle the 'one-to-many' relationship, that is, displaying multiple same