In website operation, displaying different content based on different conditions is the key to improving user experience and achieving refined operation.AnQiCMS provides powerful and flexible template tags and logical judgment functions, allowing us to easily implement conditional display of content, thus meeting various business needs.
This article will delve into how to utilize the template engine features of AnQiCMS to precisely control the display of website content through conditional logic judgments.
Flexible and variable condition judgment basis:iftags
AnQiCMS template engine supports syntax similar to Djangoif/elif/elseandendifTags, this is the core of conditional judgment. Through these tags, we can decide whether to display a certain block 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==equals,!=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,notnot) to combine more complex judgments.
Master content properties 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.Using these properties, we can construct various meaningful conditional judgments.
1. Recommend properties based on content to display specific information
When editing documents in the background, we can set 'recommended attributes' for the document, such as headlines, recommendations, slides, etc. These attributes can be obtained througharchiveDetailorarchiveListtags for the document.Flagfield.
For example, if you want to display all the latest documents with the attribute "幻灯[f]" 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 %}
To determine if the current document is recommended on the document detail page:ifStatement Check:
{% if archive.Flag contains "c" %} {# 假设“c”代表推荐属性 #}
<span class="recommend-tag">推荐</span>
{% endif %}
2. Judge the classification characteristics, build intelligent navigation
When dealing with category lists, you may need to decide whether to display a submenu or link directly to the category based on whether it has subcategories.categoryListTag returnsitemThe object includesHasChildrenproperties.
{% 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. Utilizing custom fields, create personalized content modules
The "Content Model" feature of AnQiCMS allows us to add custom fields to documents or categories.These custom fields are the key to displaying content individually.PriceCustom fields.
{# 在产品详情页,如果产品价格大于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 if its value is empty to decide whether to render the related HTML block.
4. Determine if the image exists and optimize the page structure.
On the 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 tagarchiveDetailor list itemitemofThumborLogofield can be used directly 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: Enhanced list display
InforCombined in the loopifTags can provide more detailed control over list items.
Prompt when the list is empty
forLoop-specificemptyThe label is very practical, when the object being traversed is empty,emptythe content within the label will be rendered, which is very suitable for displaying a 'No content' prompt.
{% 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 %}
Highlight the current navigation item or a specific element in the list
In the navigation menu or pagination component,itemusually contains aIsCurrentProperties, used to determine if the current item is the page the user is visiting, so that it can be addedactiveClass 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 tabpaginationofpages.PagesLoop, you can also useitem.IsCurrentHighlight the current page number.
Use the system and contact information settings to implement site-level conditional display.
PasssystemandcontactLabels, we can obtain the global configuration information of the website and make conditional judgments based on it.
1. Website shutdown status reminder
During website maintenance, you can set the website status to closed.At this moment, you can display the shutdown prompt information by checking the system variables.