How to correctly refer to and display variables in AnQiCMS templates, and use conditional judgments to control content display?

In AnQiCMS templates, flexibly referencing variables and using conditional judgments to control content display is the key to building dynamic and feature-rich websites.AnQiCMS uses a template engine syntax similar to Django, making template development both intuitive and powerful.This article will delve into how to effectively manage variables and logic in AnQiCMS templates, helping you better master content presentation.


一、AnQiCMS Template Basic: Variable Reference and Display

In AnQiCMS template, data flow is carried by variables. Understanding how to correctly refer to and display these variables is the first step in template development.

1. Template Engine Syntax Overview

AnQiCMS templates follow specific syntax rules:

  • Variable References: Use double curly braces{{ 变量名 }}to output the value of a variable.
  • Logical control:Use single curly braces and percentage signs{% 标签名 %}to handle logical operations such as conditional judgment, loops, function calls, etc. These logical tags usually need to appear in pairs, for example{% if 条件 %}...{% endif %}.

2. Variable Naming and Access

AnQiCMS 中的变量通常采用驼峰命名法(CamelCase),即每个单词的首字母大写。当你需要访问一个对象的属性时,使用点号.Establish connection.

  • Reference simple variable: If you have configured the website name in the background "Global Function Settings" and named itSiteName, you can reference it in the template like this:

    <h1>欢迎来到 {{ SiteName }}!</h1>
    

    or use the system tag to explicitly call and assign a value to a custom variable name:

    {% system siteNameVar with name="SiteName" %}
    <h1>欢迎来到 {{ siteNameVar }}!</h1>
    
  • reference object properties When traversing the document list, each document is an object, and you can useitem.Title/item.LinkAccess its properties in English. For example, display article titles and links in an article list:

    {% archiveList archives with type="list" limit="5" %}
        {% for item in archives %}
            <p><a href="{{ item.Link }}">{{ item.Title }}</a></p>
        {% endfor %}
    {% endarchiveList %}
    
  • Use filters to process variables: EnglishQiCMS provides rich filters to format or convert variables. Filters are separated by a vertical bar|Connected to variable names, they can be used sequentially. For example, if your article contentitem.ContentContaining HTML tags, to safely display this content without being directly parsed by the browser as malicious script, you can usesafeFilter:

    <div>{{ item.Content|safe }}</div>
    

    For example, formatting timestampsitem.CreatedTimeFor a readable date:

    <p>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
    

    For string truncation, you can usetruncatechars:

    <p>简介:{{ item.Description|truncatechars:50 }}</p>
    

3. Custom variables

Sometimes you need to temporarily create or assign a variable in the template, AnQiCMS provideswithandsetLabel to achieve this.

  • {% with %}tags: Used to define one or more variables within a scope, usually withincludeused in conjunction with the tag, to pass variables to the included template fragments.
    
    {% with greeting="你好" name="世界" %}
        <p>{{ greeting }}, {{ name }}!</p>
    {% endwith %}
    
  • {% set %}tagsEnglish for: Used to declare a variable within the current template scope, its scope lasts until the template ends or is covered by a newsettag.
    
    {% set articleCount = 0 %}
    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
            {% set articleCount = forloop.Counter %}
            <p>{{ articleCount }}. {{ item.Title }}</p>
        {% endfor %}
    {% endarchiveList %}
    <p>共有 {{ articleCount }} 篇文章。</p>
    

二、Flexible content presentation control: conditional judgment (If statement)

条件判断是模板逻辑的核心,它允许你根据不同的条件显示或隐藏特定的内容块。AnQiCMS 的 Englishif语句语法直观且功能完善。English

1.{% if %}/{% elif %}/{% else %}structure

The most common conditional judgment structures includeif(if),elif(else if),else(else) andendif(end if).

{% if 用户已登录 %}
    <p>欢迎回来,{{ 用户名 }}!</p>
{% elif 用户正在注册 %}
    <p>欢迎注册新账号!</p>
{% else %}
    <p>请登录或注册。</p>
{% endif %}

2. Common Condition Judgment

  • Equal and Not Equal: Use==(Equal) And!=(Not Equal).
    
    {% if category.Id == 1 %}
        <p>这是新闻分类的特别内容。</p>
    {% endif %}
    
  • Size Comparison: Use>,<,>=,<=.
    
    {% if archive.Views > 1000 %}
        <span class="hot-badge">热门</span>
    {% endif %}
    
  • Logical operation: Useand(And),or(Or),not(Not).
    
    {% if user.IsVIP and user.ExpireTime > currentTime %}
        <p>您是尊贵的 VIP 会员。</p>
    {% elif not user.IsVIP %}
        <p>成为 VIP 享受更多特权。</p>
    {% endif %}
    
  • Determine if a variable exists or is not empty:Directly use the variable name as a condition, when the variable isnil, empty string, zero value, etc., it will be judged asfalse.
    
    {% if archive.Thumb %}
        <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}">
    {% else %}
        <img src="/static/images/default_thumb.jpg" alt="默认缩略图">
    {% endif %}
    

3. Practical application scenarios

Conditional judgment is widely used in various scenarios, such as:

  • Display different navigation menus based on user role.
  • Display an image or placeholder based on whether the article has a thumbnail.
  • On the list page, based on the article'sFlagProperties (such as "recommended
  • In multi-site management, based onsiteIdload different content or layouts.

三、Traverse data sets: Loop (For statement)

Loop is a powerful tool for processing data lists, whether it is an article list, category list, or navigation menu.forThe statements can help you efficiently render.

1.{% for %}/{% endfor %}structure

forLoops are used to iterate over each element in an array, slice (slice), or other iterable objects.

{% categoryList categories with moduleId="1" parentId="0" %}
    <ul>
        {% for category in categories %}
            <li><a href="{{ category.Link }}">{{ category.Title }}</a></li>
        {% endfor %}
    </ul>
{% endcategoryList %}

2.forloopObjects and auxiliary functions

InforInside the loop, you can access a specialforloopobject that provides information about the current loop state.

  • forloop.Counter: the iteration count of the current loop (starting from 1).
  • forloop.Revcounter:Current remaining iteration count (counting backwards).
    
    {% archiveList archives with type="list" limit="3" %}
        {% for item in archives %}
            <p>{{ forloop.Counter }}. {{ item.Title }} (还剩 {{ forloop.Revcounter }} 篇)</p>
        {% endfor %}
    {% endarchiveList %}
    

3