As an experienced CMS website operation personnel in security, I know that the flexibility of the template is crucial for efficient operation.尤其是condition judgment, it allows us to intelligently control the display and hiding of content based on different data states and business logic, thereby providing users with more precise and personalized browsing experiences.The template engine of AnQi CMS adopts the syntax style of Django, making the implementation of conditional judgments both powerful and easy to understand.
Basic structure of conditional judgment in AnQi CMS template
In the AnQi CMS template, conditional judgment is mainly achieved throughif/elif[else if abbreviation] andelsetags and is implemented withendifLabel end. This structure is very similar to conditional statements in many programming languages, allowing operation personnel familiar with programming logic to quickly get started.
An basic conditional judgment statement structure is as follows:
{% if 条件 %}
<!-- 当条件为真时显示的内容 -->
{% endif %}
If you need to handle multiple situations, you can introduceelifandelse:
{% if 条件1 %}
<!-- 当条件1为真时显示的内容 -->
{% elif 条件2 %}
<!-- 当条件1为假且条件2为真时显示的内容 -->
{% else %}
<!-- 当所有条件都为假时显示的内容 -->
{% endif %}
Allifstatements must end with{% endif %}tags are correctly closed.
Elements that constitute conditional expression
The core of conditional judgment lies in the construction of its expression. The Anqi CMS template supports various types of expressions to help us precisely define judgment logic.
FirstlyComparison operators. We can use common comparison operators to determine the relationship of values:
- Is equal to:
==(For example}]}archive.Id == 10) - Is not equal to:
!=(For example}]}item.Id != 1) - Is greater than:
>(For example}]}simple.number > 42) - Is less than:
<(For example}]}simple.number < 42) - greater than or equal to:
>=(For example}]}simple.number >= 42) - less than or equal to:
<=(For example}]}simple.number <= 42)
ThenLogical operatorsThey allow us to combine multiple conditions to form more complex judgments:
- logical and:
&&orand(For example}]}true && (true && (1 == 1 || false))) - logical or:
||oror(For example}]}true || false) - Logical NOT:
!ornot(For example}]}!true,not complex.post)
In addition, there areMember operator inUsed to check if a value exists in a collection (such as an array, a map):
- For example
5 in simple.intmapTo judge5Whether it existssimple.intmapin this collection.
auto CMS template also performs translation on some valuesimplicit true or false judgmentsFor example, non-empty strings, non-zero numbers, non-empty lists or objects are usually consideredtrue; while empty strings, zero,nilThis is an empty value or an empty list is consideredfalse. This means we can directly use{% if 变量名 %}to judge whether a variable has a value or is true.
actual application of conditional judgment in templates
Condition judgment is widely used in daily website operations, and the following are some common examples:
Display or hide content based on data status:We can decide whether to display relevant information based on the review status of the content, whether there are subcategories, or a specific attribute. For example, in the comment list, we can determine if a comment is being reviewed:
{% if item.Status != 1 %}
<span>此评论正在审核中</span>
{% else %}
<span>{{item.Content}}</span>
{% endif %}
Or determine whether a category has subcategories to decide whether to display the subcategory list or the document list under the category:
{% if item.HasChildren %}
{% categoryList subCategories with parentId=item.Id %}
<!-- 显示子分类 -->
{% endcategoryList %}
{% else %}
{% archiveList products with type="list" categoryId=item.Id limit="8" %}
<!-- 显示该分类下的产品 -->
{% endarchiveList %}
Handle special items in the list:When traversing a list, we often need to perform special processing on the first, last, or items that meet specific conditions. Byforloop.Counter(current loop index, starting from 1) andforloop.RevcounterThe remaining loop count and loop variables can be easily implemented:
{% for item in archives %}
<li class="{% if forloop.Counter == 1 %}active{% endif %}">
<a href="{{item.Link}}">{{item.Title}}</a>
{% if forloop.Counter == 1 %}
<span>这是列表的第一项!</span>
{% endif %}
</li>
{% empty %}
<li>没有内容</li>
{% endfor %}
Handle optional data and placeholders:Sometimes, images, descriptions, or other fields may not be available for all content. Using conditional judgments can prevent empty spaces or incorrect links from appearing on the page.
{% if item.Thumb %}
<img src="{{item.Thumb}}" alt="{{item.Title}}">
{% else %}
<img src="/static/images/default-thumb.png" alt="默认缩略图">
{% endif %}
Again, in SEO optimization, the canonical URL may only exist on specific pages:
{%- tdk canonical with name="CanonicalUrl" %}
{%- if canonical %}
<link rel="canonical" href="{{canonical}}" />
{%- endif %}
Condition judgment based on custom fields:If the content model defines custom fields, we can also judge based on the values of these fields. For example, a custom field namedis_featureda boolean type:
{% archiveDetail archive with name="is_featured" %}
{% if archive.is_featured %}
<span class="featured-badge">推荐</span>
{% endif %}
Or exclude certain fields when outputting custom fields in a loop:
{% categoryDetail extras with name="Extra" %}
{% for field in extras %}
{% if field.Name != 'author' and field.Name != 'price' %}
<div>{{field.Name}}:{{field.Value}}</div>
{% endif %}
{% endfor %}
Nested and combined use
The conditional judgment statements in the AnQi CMS template can be nested at any level and can be used with other template tags (such asforLoop seamlessly integrated.This powerful combination capability allows us to organize complex page logic in a highly structured way.
{% navList navList %}
{%- for item in navList %}
<li>
<a href="{{ item.Link }}">{{item.Title}}</a>
{%- if item.NavList %}
<ul class="sub-menu">
{%- for inner in item.NavList %}
<li>
<a href="{{ inner.Link }}">{{inner.Title}}</a>
{% archiveList products with type="list" categoryId=inner.PageId limit="8" %}
{% if products %}
<ul class="product-list">
{% for product in products %}
<li><a href="{{product.Link}}">{{product.Title}}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endarchiveList %}
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
{% endnavList %}
This example shows how tofornested within a loopifmake a judgment, andifJudge if there is another nested inside.forloop andifJudge to implement complex navigation structures.
Details and precautions.
- Label closure:.Ensure that each one is done properly.
{% if %}Each corresponds to one{% endif %}Otherwise, the template cannot be parsed correctly. - Case sensitive:Label names, variable names, and parameters in the template