安企CMS(AnQiCMS)as an efficient and customizable content management system, its template engine design philosophy is to simplify the complexity of content presentation, allowing website operators to focus on content organization and optimization.In the daily operation of websites, we often need to display content based on different conditions or traverse list data for dynamic rendering.if-elseand loop traversal (forTags. Mastering the use of these tags is the key to creating flexible and versatile web pages that meet user needs.

The template engine syntax of Anqi CMS is similar to Django and Blade, which allows operators familiar with other mainstream template engines to get started quickly.Through these tags, we can accurately control the display logic of content, ensuring that users receive **informational experience when visiting the website.

Flexible use of conditional judgment (if-else)

In website content display, conditional judgment is an indispensable part. Whether it is to display different content based on user permissions, to judge whether the data exists to avoid page errors, or to adjust the page layout based on specific business logic,if-elseLabels can provide strong support.

Conditional tags in the Anqi CMS are used to{% if 条件 %}at the beginning, followed by{% endif %}End. Its basic form is to judge whether a certain condition is true, if it is true then executeifthe content in the block.

For example, when we want to display specific information based on the document ID:

{% if archive.Id == 10 %}
    这是文档ID为10的文档的专属内容。
{% endif %}

English version: except for the simpleifwe can also use{% elif 条件 %}and{% else %}to build more complex logic.elifallow us to set multiple mutually exclusive conditions,elseThen it is the default handling when all conditions are not met.

An common scenario is to judge based on the size of the number:

{% if simple.number < 42 %}
    这个数字小于42。
{% elif simple.number > 42 %}
    这个数字大于42。
{% else %}
    这个数字就是42。
{% endif %}

Conditional expressions support various operators, including comparison operators (==equals,!=Not equal to,>Greater than,<Less than,>=greater than or equal to,<=less than or equal to), logical operators (andlogical AND,orlogical OR,notLogical NOT), as well as member detection operator (inContains). For example, we can determine whether a variable is empty, or whether it is in some set:

{% if !simple %}
    'simple'变量不存在或为空。
{% elif "Text" in complex.post %}
    'complex.post'中包含"Text"这个字符串。
{% endif %}

Through these flexible combinations, we can create various refined content display rules to ensure that the website presents the expected effects in different states.

Efficiently implement loop iteration (for)

In website operation, we often need to display list data, such as article lists, product lists, navigation menus, or image groups, etc.forThe loop iteration label is exactly designed to meet such needs, it allows us to iterate over array, slice (slice) and other collection types of data, and process each element.

forThe basic syntax of the loop is{% for item in collection %}," and is{% endfor %}End.itemthe temporary variable name of the current element in each loop,collectionwhich is the collection we want to iterate over.

For example, iterating over a collection namedarchivesThe list of articles and display their title and link:

{% for item in archives %}
    <li>
        <a href="{{ item.Link }}">{{ item.Title }}</a>
    </li>
{% endfor %}

During the loop, sometimes we need to know the current loop count, or apply different styles at special positions (such as the first item or the last item). Safe CMS provides built-inforloopA variable that contains some useful information:

  • forloop.Counter: The current loop number, starting from 1.
  • forloop.Revcounter: The reverse sequence number of the current loop, indicating the number of remaining items.

We can utilizeforloop.CounterTo add a number or special style to list items:

{% for item in archives %}
    <li class="{% if forloop.Counter == 1 %}active{% endif %}">
        <span>第{{ forloop.Counter }}篇,剩余{{ forloop.Revcounter }}篇</span>
        <a href="{{ item.Link }}">{{ item.Title }}</a>
    </li>
{% endfor %}

In order to meet different data display requirements,forThe loop also supportsreversedandsortedmodifier.reversedYou can reverse the traversal order,sortedand you can sort the set:

{# 倒序遍历 #}
{% for item in archives reversed %}
    ...
{% endfor %}

{# 排序后遍历 #}
{% for item in archives sorted %}
    ...
{% endfor %}

{# 倒序并排序遍历 #}
{% for item in archives reversed sorted %}
    ...
{% endfor %}

When the set to be traversed may be empty, use{% empty %}Tags can elegantly handle this situation, avoiding blank pages or errors:

{% for item in archives %}
    <li>
        <a href="{{ item.Link }}">{{ item.Title }}</a>
    </li>
{% empty %}
    <li>当前没有任何内容可以显示。</li>
{% endfor %}

In addition,cycleLabels are also very useful in loops, as they can alternate predefined values with each iteration and are often used to implement effects like alternating row colors.

{% for item in archives %}
    <li class="{% cycle 'even' 'odd' %}">
        {{ item.Title }}
    </li>
{% endfor %}

PassforThese features of loops allow us to easily build clear, rich, and dynamically responsive list pages.

Summary and practical suggestions

Conditional judgment and loop traversal are the most basic and important components in the development of Anqi CMS templates.As a website operator, being proficient in them can not only help us better understand and modify existing templates, but also quickly implement customized content display when needed.

In actual operation, please pay strict attention to the syntax of template tags, for example, all tags must appear in pairs. At the same time, to maintain the neatness and readability of the template code, it is recommended to organize the nesting structure of conditions and loops reasonably and make use of{%- ... -%}This syntax is used to eliminate unnecessary blank lines, making the final rendered HTML code more compact.By continuously practicing and trying, you will be able to fully utilize the potential of the Anqi CMS template engine to provide users with an excellent website experience.


Frequently Asked Questions (FAQ)

Q1: In the security CMS template, how to judge whether a variable is empty or does not exist?

A1: You can use{% if variable %}to judge whether a variable exists and is not empty. Ifvariablehas a value ofnil、empty string、0、falseor an empty set,ifconditions will be judged as false. If you want to explicitly judge that a variable does not exist, you can use{% if not variable %}. For example:{% if category.Title %}will check if the category title exists and is not empty;{% if not archives %}It will determinearchivesThe list is empty.

Q2: How can I implement alternating row colors or styles for list items in a loop?

A2: You can use{% cycle 'value1' 'value2' %}Use labels to implement alternating row colors.cycleThe label will output its parameter value in order each time the loop runs. For example, to implement different colors for odd and even rows, you can<li>use the label:

{% for item in articles %}
    <li class="{% cycle 'bg-light' 'bg-dark' %}">
        {{ item.Title }}
    </li>
{% endfor %}

Q3:ifThe tag supports which complex condition expressions, such as checking if multiple conditions are met at the same time?

A3:ifThe tag supports logical operatorsand(Logical AND),or(Logical OR) andnot(Logical NOT), and parentheses to combine complex conditions. For example:

{% if user.IsLoggedIn and user.IsVip %}
    欢迎VIP会员!
{% elif user.IsLoggedIn and not user.IsVip %}
    普通会员你好,升级VIP享受更多特权!
{% else %}
    请先登录。
{% endif %}

This combination method allows you to precisely control the display logic of content based on the state of multiple variables.