As an experienced website operations expert, I know that the flexibility of templates is the key to supporting diverse content display and operation strategies.Auto CMS (AutoCMS) provides us with powerful content customization capabilities with its efficient architecture based on the Go language and Django-style template syntax.||oror),Look at how to skillfully use it in AnQiCMS to meet the flexible display needs of "any condition is met
AnQiCMS template logic or: make condition judgment more flexible
In website operation, we often need to dynamically display content based on different conditions.For example, a document may need to meet the conditions of "Top Recommendation" or "Homepage Slider" to be displayed in a specific area; a user may have different content access permissions because they are an "Administrator" or a "Paid VIP".||ororThis is the core to achieve this kind of "multiple conditions any satisfy" logic.
English CMS template basics: Review and Preparation
The flexibility of AnQi CMS in providing custom capabilities is due to its adoption of template engine syntax similar to Django and Blade. In AnQiCMS templates, we mainly use two symbols to manipulate content:
- Double curly braces
{{ 变量 }}:Used to output the value of a variable or the result of an expression. - Single curly braces and percentage signs
{% 标签 %}:Used for logical control, such as conditional judgments, loops, etc.
Conditional judgment, which is what we will focus on today.ifTags that allow us to determine whether to render a section of template content based on boolean (true/false) conditions. The logical OR operator is the key to expandingifthe capabilities of the tag.
The core of logical OR operation:||andor
When it is necessary to judge whether any of multiple conditions are met, the logical OR operation is particularly important. In AnQiCMS templates, you can use two symbols to represent the logical OR:
||(双竖线):这是在大多数编程语言中常见的逻辑或运算符。or(English word):这是Django模板引擎中更具可读性的逻辑或运算符。
These two operators have the same function, you can choose to use them based on personal preference or team coding standards.The basic principle is that as long as any of the conditions involved in the operation is true, the result of the entire logical expression is true.
Whether||OrorThey are most often used with{% if %}tags to form a conditional judgment block. Its basic syntax structure is as follows:
{% if 条件表达式一 || 条件表达式二 %}
<!-- 当条件表达式一或条件表达式二任一为真时,显示这段内容 -->
{% endif %}
or usingor:
{% if 条件表达式一 or 条件表达式二 %}
<!-- 当条件表达式一或条件表达式二任一为真时,显示这段内容 -->
{% endif %}
You can even use double curly braces{{ }}Directly evaluating a logical expression in the middle, the result will be a boolean value (trueorfalse):
当前页面是否为首页或文章详情页:{{ is_homepage || is_archive_detail }}
However, when it comes to controlling the rendering of content, we are more likely to embed it into{% if %}a statement block.
Application scenarios and code examples
Let us look at several common application scenarios to see how to use logical OR operations in AnQiCMS templates to make your website content more intelligent and dynamic.
Scene one: Display based on the recommended attributes of the document
假设你希望在首页的一个特定区域,显示所有被标记为“头条”或“推荐”的文档。在AnQiCMS的文档管理中,文档的推荐属性(Flag)May be identified by different letters, for example, 'h' represents headlines, 'c' represents recommendations.
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
{# 检查文档是否是头条 (h) 或 推荐 (c) #}
{% if item.Flag == "h" or item.Flag == "c" %}
<div class="featured-article">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>{{ item.Description|truncatechars:100 }}</p>
</div>
{% endif %}
{% empty %}
<p>暂无符合条件的推荐文档。</p>
{% endfor %}
{% endarchiveList %}
This code will traverse all documents and only display those articles that have both the 'Headline' or 'Recommended' attributes, greatly enhancing the flexibility of content operation.
Scene two: Control content visibility based on user role or status
In some cases, you may need to display or hide specific template elements based on the user's permissions or status.For example, only administrators or VIP members can see a special announcement or download link.
Assumecurrent_userIs a variable representing the current user, it may containIsAdmin(Boolean value) andIsVip(Boolean value) properties.
{# 假设 current_user 变量已在控制器层传入 #}
{% if current_user.IsAdmin or current_user.IsVip %}
<div class="exclusive-content">
<h4>欢迎尊贵的会员/管理员!</h4>
<p>这是仅限您访问的独家内容,感谢您的支持!</p>
<a href="/download/premium-resource">下载高级资源</a>
</div>
{% else %}
<div class="public-content">
<p>升级为VIP会员,即可解锁更多独家内容!</p>
<a href="/vip-upgrade">了解VIP会员</a>
</div>
{% endif %}
Through logical OR, we can easily provide customized experiences for different user groups with different privileges.
Scenario three: Flexible judgment of content model or classification
Your website may contain various content models (such as articles, products, events), and each model has different categories. If you want to display a module in the sidebar that contains content related to the "News" category or belongs to the "Product" model, you can do it like this:
{# 假设 category 是当前页面的分类对象,module 是当前页面的模型对象 #}
{% if category.Title == "新闻" or module.Name == "产品" %}
<div class="dynamic-sidebar-block">
<h4>热门 {{ category.Title or module.Name }}</h4>
<p>这里可以根据条件加载不同的热门列表...</p>
{# 实际内容加载逻辑会更复杂,这里仅作示意 #}
</div>
{% endif %}
Here, we flexibly judge the current page's category name or content model name, thus dynamically displaying the sidebar module.
Scene four: Check if a variable exists or has a value (truth value judgment)
In AnQiCMS templates, many times, if a variable does not exist, is an empty string, or0ornilIt is considered 'false' in conditional judgments.Using this feature, logical OR can also be used to check multiple alternative values, using any one that has a 'true' value.
{# 假设 article 变量存在,且可能包含 Logo 或 Thumb 属性,优先显示 Logo #}
{% set display_image = article.Logo or article.Thumb or "/static/default_image.jpg" %}
<img src="{{ display_image }}" alt="{{ article.Title }}">
In this example,display_imageWill try in sequencearticle.LogoIfLogoDoes not exist or is empty, then tryarticle.ThumbIfThumbDoes not exist or is empty, then use the default image path as a last resort.
Advanced usage: combination logic and nesting
The logical OR operator can be used with other logical operators (such as&&orandmeaning “AND”) as well as parentheses()to construct more complex conditional logic. Parentheses are used to specify the precedence of operators.
<p>该已发布文章可供管理员或编辑修改。</p>
{% elif current_user.IsVip