As an experienced website operations expert, I fully understand the importance of a flexible and powerful content management system for website operations.AnQiCMS with its high-performance architecture based on the Go language and similar Django template engine syntax, provides us with great convenience.{% if ... else ... %}It is particularly important.
Today, let's delve deeply into how to use AnQiCMS templates{% if ... else ... %}Structure, making your website content more intelligent and personalized.
AnQiCMS template smart decision-making: Master{% if ... else ... %}Achieve flexible content display
In the world of Anqi CMS templates, content is no longer a monotonous static display, but can change intelligently according to preset conditions. This includes,{% if ... else ... %}The structure is undoubtedly the core tool for realizing this 'intelligent decision-making'.It allows us to decide what to display and what not to display on the page based on the state of the data, the identity of the user, and even specific business logic, greatly enhancing the flexibility of content presentation and the user experience.
Why{% if ... else ... %}So important?
Imagine such a scene:
- Personalized content display: If the user is a VIP member, display exclusive preferential information; otherwise, display the guidance for registering members.
- Data integrity checkIf an article has a thumbnail, display it; if not, display a default placeholder or simply do not display the image area.
- Multi-site content adaptation:In a multi-site management environment, display different navigation or advertisement areas based on the specific needs of the current site.
- Module switch function: Through a simple configuration item, control the opening or closing of a certain front-end function module in the background.
These common needs in daily operations cannot be separated from{% if ... else ... %}This conditional judgment logic.The AnQiCMS template engine (with syntax similar to the Django template engine) provides an intuitive and easy-to-use syntax, allowing website operators and frontend developers to quickly implement these dynamic effects.
{% if ... else ... %}fundamental grammar and structure
AnQiCMS conditional tags, like loop control tags, are always written using single curly braces and percent signs{% %}to define. They always appear in pairs, which means{% if %}there must be{% endif %}Close it.
The basic structure is like this:
{% if 条件 %}
<!-- 当条件为真时,显示这部分内容 -->
{% endif %}
If your logic needs to handle the 'otherwise' case, you can introduce{% else %}:
{% if 条件 %}
<!-- 当条件为真时,显示这部分内容 -->
{% else %}
<!-- 当条件为假时,显示这部分内容 -->
{% endif %}
When you have multiple mutually exclusive conditions to judge,{% elif %}(else if abbreviation) comes into play:
{% if 第一个条件 %}
<!-- 当第一个条件为真时,显示这部分内容 -->
{% elif 第二个条件 %}
<!-- 当第一个条件为假,且第二个条件为真时,显示这部分内容 -->
{% elif 第三个条件 %}
<!-- 当前两个条件为假,且第三个条件为真时,显示这部分内容 -->
{% else %}
<!-- 所有条件都为假时,显示这部分内容 -->
{% endif %}
Understanding this hierarchy is to masterifThe key statement: The system will evaluate conditions sequentially from top to bottom, and once it finds the first true condition, it will execute the corresponding code block and then skip the rest.elifandelsePart, go directly to{% endif %}.
Deep into conditional judgment: what can you use to judge?
In{% if %}The "condition" part, you can use a variety of expressions and operators to construct complex logic:
Does the variable exist or is it empty?This is one of the most commonly used judgments. If a variable has a value (non-empty string, non-zero number, non-empty array or object), it is considered true.
{# 判断文章是否有缩略图 #} {% if archive.Thumb %} <img src="{{ archive.Thumb }}" alt="{{ archive.Title }}"> {% else %} <img src="/static/images/default-thumb.webp" alt="默认图片"> {% endif %} {# 判断变量不存在或为空 #} {% if not some_variable %} <p>该变量未设置或为空。</p> {% endif %}Comparison of numbers and strings: You can use common comparison operators to compare variable values:
==(equals,)!=(not equal,)>(greater than,)<(less than,)>=(greater than or equal to,)<=(less than or equal to).{# 判断文档ID是否为特定值 #} {% if archive.Id == 10 %} <p>这是ID为10的特别文档!</p> {% endif %} {# 根据浏览量显示热门标签 #} {% if item.Views > 1000 %} <span class="hot-badge">热门</span> {% endif %} {# 比较字符串 #} {% if category.Title == "安企CMS模板制作" %} <p>当前是模板制作分类</p> {% endif %}The set contains(
in/not in)This operator is very suitable for checking if a value exists in a list, array, or map (map).{# 假设 archive.Flag 是一个包含推荐属性的列表,如 ["h", "c"] #} {% if "h" in archive.Flag %} <span>头条推荐</span> {% endif %} {# 检查某项是否在某个列表内,也可以结合过滤器 #} {% set tags = "PHP,Java,Go,Python"|split:"," %} {% if "Go" in tags %} <p>我们关注 Go 语言。</p> {% endif %}In addition, you can also use
containA filter to determine if a string or array contains a specific keyword, for example{{ "欢迎使用安企CMS(AnQiCMS)"|contain:"CMS" }}.Boolean value judgmentDirectly judge the boolean variable.