In website operation, displaying different content based on different conditions is the key to improving user experience and achieving refined operation.AnQiCMS (AnQiCMS) provides powerful and flexible template tags and logical judgment functions, allowing us to easily implement conditional display of content, thereby meeting diverse business needs.
This article will delve into how to make full use of the template engine features of AnQiCMS, and control the display of website content accurately through conditional logic judgments.
Flexible and versatile conditional judgment basis:ifTag
AnQiCMS template engine supports syntax similar to Django'sif/elif/elseandendifLabel, this is the core of realizing conditional judgment. Through these labels, we can decide whether to display a piece of content based on the truth or falsity of various expressions.
The basic judgment structure is very intuitive:
{% if 条件 %}
<!-- 满足条件时显示的内容 -->
{% endif %}
When the condition involves multiple possibilities, we can introduceelif(else if) andelseto handle:
{% if 条件1 %}
<!-- 满足条件1时显示的内容 -->
{% elif 条件2 %}
<!-- 满足条件2时显示的内容 -->
{% else %}
<!-- 以上条件都不满足时显示的内容 -->
{% endif %}
When constructing conditions, we can use common comparison operators such as==equal,!=Not equal to,>Greater than,<Less than,>=Greater than or equal to,<=less than or equal to), as well as logical operators such asandand,oror,notnon) to combine more complex judgments.
Master content attributes to achieve fine-grained control
AnQiCMS provides a rich set of built-in variables and tags, carrying various attributes of page, document, category, and other content.By using these properties, we can construct various meaningful conditional judgments.
1. Display specific content based on recommended content attributes.
When editing documents in the background, we can set "recommended properties" for the document, such as headlines, recommendations, slides, etc. These properties can be obtained througharchiveDetailorarchiveListtags to get the document'sFlagfield.
For example, if you want to display all the latest documents with the 'Slide[f]' attribute on the homepage:
{% archiveList archives with type="list" flag="f" limit="5" order="id desc" %}
{% for item in archives %}
<a href="{{ item.Link }}">{{ item.Title }}</a>
{% endfor %}
{% endarchiveList %}
If you want to determine whether the current document is recommended on the document detail page,ifCheck statement:
{% if archive.Flag contains "c" %} {# 假设“c”代表推荐属性 #}
<span class="recommend-tag">推荐</span>
{% endif %}
2. Judge the classification characteristics, build intelligent navigation
When handling the category list, you may need to decide whether to display the submenu or directly link to the category based on whether the category has subcategories.categoryListthe tags returned byitemThe object includes:HasChildrenProperty.
{% categoryList categories with moduleId="1" parentId="0" %}
<ul>
{% for item in categories %}
<li>
<a href="{{ item.Link }}">{{ item.Title }}</a>
{% if item.HasChildren %}
<!-- 如果有子分类,这里可以再次循环显示子分类,或者显示下拉菜单 -->
{% categoryList subCategories with parentId=item.Id %}
<ul>
{% for subItem in subCategories %}
<li><a href="{{ subItem.Link }}">{{ subItem.Title }}</a></li>
{% endfor %}
</ul>
{% endcategoryList %}
{% endif %}
</li>
{% endfor %}
</ul>
{% endcategoryList %}
3. Using custom fields to create personalized content modules
AnQiCMS's 'Content Model' feature allows us to add custom fields to documents or categories.These custom fields are the key to displaying content individually.For example, a product model may have a name calledPricecustom field.
{# 在产品详情页,如果产品价格大于0,则显示价格信息 #}
{% archiveDetail productPrice with name="Price" %}
{% if productPrice > 0 %}
<p>价格:¥ {{ productPrice }}</p>
{% else %}
<p>价格面议</p>
{% endif %}
You can also determine whether a custom field exists or its value is empty to decide whether to render the relevant HTML block.
4. Determine if the image exists and optimize the page structure.
In a list or detail page, if the document does not have a thumbnail or cover image, you may not want to display an empty image placeholder. Document detail labelarchiveDetailOr list itemitemofThumborLogofield can be directly used to judge:
{% if item.Thumb %}
<img src="{{ item.Thumb }}" alt="{{ item.Title }}">
{% else %}
<img src="/static/images/default-thumb.png" alt="无图"> {# 显示一个默认占位图 #}
{% endif %}
Loop condition judgment: enhance list display
InforCombining in the loopifTags can provide more detailed control over list items.
1. Prompt when the list is empty
forLoop-specificemptyThe tag is very useful, when traversing the empty object,emptythe content inside the tag will be rendered, which is very suitable for displaying prompts such as 'No content'.
{% archiveList archives with type="list" categoryId="10" limit="10" %}
<ul>
{% for item in archives %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% empty %}
<li>该分类下暂无文章。</li>
{% endfor %}
</ul>
{% endarchiveList %}
2. Highlight the current navigation item or a specific element in the list
In the navigation menu or pagination component,itemusually includes aIsCurrentThe attribute can be used to determine whether the current item is the page being visited by the user, thereby addingactivethe class for highlighting.
{% navList navs %}
<ul>
{% for item in navs %}
<li class="{% if item.IsCurrent %}active{% endif %}">
<a href="{{ item.Link }}">{{ item.Title }}</a>
</li>
{% endfor %}
</ul>
{% endnavList %}
Similarly, in the pagination tagpaginationofpages.Pagesloop, it can also be used.item.IsCurrentHighlight the current page number.
Use the system and contact information settings to implement site-level conditional display.
BysystemandcontactLabel, we can obtain the global configuration information of the website and make conditional judgments accordingly.
1. Website shutdown status reminder
During website maintenance, you can set the website status to closed.At this time, it is possible to display a shutdown prompt by judging the system variables.“`twig {% system siteCloseTips with name=“SiteCloseTips” %} {% system siteStatus with name=“SiteStatus” %} {# Assume SiteStatus 0: Disabled 1: In Use #} {% if siteStatus ==