In AnQiCMS template development, we often encounter situations where we need to display different content based on different conditions. Beginners may tend to use a large number of{% if 条件 %}The statement, as the project requirements grow, these conditional judgments are nested layer by layer, and it will soon make the template difficult to read and maintain, and even hide potential logical errors.Code readability once decreased, not only will the development efficiency be affected, but the future feature iteration and problem debugging will also become extremely difficult.
It is fortunate that AnQiCMS, based on the Django-like template engine of the Go language, provides a rich set of features, which we can use to effectively avoidifThe excessive nesting of statements makes template code clear and elegant.
First, make good use ofsetandwithDeclare variables, preprocess complex conditions
ReduceifThe first step in nesting is to simplify the conditions themselves.When the condition expression is complex or needs to be used multiple times, we can store its result in a variable in advance.{% set 变量名 = 值 %}and{% with 变量名 = 值 %}Two ways to define variables.
For example, if you need to determine whether a document is recommended, pinned, and belongs to a specific category, the traditional way might be as follows:
{% if archive.Flag contains "c" and archive.Flag contains "h" and archive.CategoryId == 10 %}
{# 展示推荐置顶文章的特殊样式 #}
{% else %}
{# 普通文章样式 #}
{% endif %}
Such conditions are long and hard to see at a glance. We can make them clearer and more concise bysetorwithdecomposing them:
{% set isSpecialRecommend = archive.Flag contains "c" and archive.Flag contains "h" and archive.CategoryId == 10 %}
{% if isSpecialRecommend %}
{# 展示推荐置顶文章的特殊样式 #}
{% else %}
{# 普通文章样式 #}
{% endif %}
This is,ifThe conditions of the statements become clear and concise.setThe declared variables are valid within the current module block andwithThen it is usually used to define multiple variables temporarily, and it needs to be{% endwith %}end. Proper use of variable declaration can effectively improve the clarity of conditional judgments.
English translation: Using filters to simplify conditional judgments and data display
AnQiCMS provides a rich set of filters (Filters), which can perform various operations on variables, often replacing simpleif/elselogic.
defaultFilter:When a variable may be empty, provide a default value to avoid lengthyif/else.{# 避免:{% if archive.Description %}{{ archive.Description }}{% else %}暂无描述{% endif %} #} {{ archive.Description|default:"暂无描述" }}yesnoFilter:Used to handle boolean values, output specified text based on true or false.{# 假设 archive.IsPublished 是一个布尔值 #} {{ archive.IsPublished|yesno:"已发布,草稿,未知" }}lengthor directly judge:Check if a list, string, or object is empty, and avoid{% if list|length > 0 %}.AnQiCMS's template engine is similar to Django and can be used directly
{% if 变量 %}Determine if a set or string is non-empty.{# 避免:{% if tags|length > 0 %} #} {% if tags %} {# 标签列表不为空时 #} {% endif %}containFilter:Quickly determine if a keyword is present in a string or array without manual traversal or complex string functions.{% if archive.Flag|contain:"c" %} {# 判断是否包含“推荐”标记 #} <span>推荐</span> {% endif %}
With these filters, many simple conditional judgments and data formatting can be directly completed when the variable is output, and there will not be so many in the template.ifblocks.
Three, modular template, embraceincludeandmacro
When a complexifWhen the branch contains a large amount of content, even if the condition itself is clear, the overall volume of the template will become large. At this point, modularization is**practical.
includeTags:Extract the common part of the template or complex branch logic into a separate file.For example, if your article detail page
Flaghave multiple display modes, you can organize it like this:{% if archive.Flag contains "f" %} {# 幻灯模式 #} {% include "partial/archive_layout_slideshow.html" with article=archive %} {% elif archive.Flag contains "a" %} {# 特荐模式 #} {% include "partial/archive_layout_featured.html" with article=archive %} {% else %} {# 默认模式 #} {% include "partial/archive_layout_default.html" with article=archive %} {% endif %}Each
partial/archive_layout_*.htmlThe file is responsible for rendering its own specific layout, and the main template remains simple.with article=archiveIt allows you to pass specific variables to the included templates, avoiding global pollution and making the modules more independent and reusable.macroMacro definition:You can use it for small HTML structures with specific logic that need to be repeated in multiple places.macroThe macro can receive parameters like a function and encapsulate internal logic.Assuming you need to display an article card with a thumbnail and title on multiple list pages, and the style may vary depending on whether there is a thumbnail or not:
{% if article.Thumb %} <img src="{{ article.Thumb }}" alt="{{ article.Title }}"> {% endif %} <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3> <p>{{ article.Description|