As an experienced website operations expert, I know that flexible content display is crucial for the operation efficiency and user experience of the website.In a powerful content management system like AnQiCMS, understanding how to display differentiated content according to different content models is a key step in achieving fine-grained operation.Today, let's delve deeply into this topic and reveal how AnQiCMS can help us easily achieve this goal.
The core concept of AnQiCMS content model
One of AnQiCMS's core strengths is its flexible content model.This means that the website administrator can create and manage various types of content according to business needs.For example, an e-commerce website may need a "product modelThese content models greatly enrich the types of content, allowing each type of content to be managed and displayed in the most fitting way for its characteristics.
When a user visits a page on a website, the page usually is associated with a specific content model.For example, when you view an article, it may belong to the 'article model'; when you browse a product, it may belong to the 'product model'.To implement dynamic content display, the primary task is to accurately identify the content model of the current page.
Identify the content model of the current page
In the AnQiCMS template system, the most direct and effective method to identify the content model associated with the current page is to use the provided template tags to obtain the page context information. For any document published through the content model (whether it is an article, product, or other custom model), we canarchiveDetailTag to get its details, which includes the crucialModuleId(Document model ID).
ThisModuleIdIt is an integer that uniquely identifies the content model of the current document. For example, the default built-in 'article model' of Anqi CMS may correspond toModuleId=1,“product model”may correspond toModuleId=2(The actual ID is set by the backend). Call it like this in the template:
{% archiveDetail currentModuleId with name="ModuleId" %}
Then we can assign the content model ID of the current page tocurrentModuleIdThis variable. With this ID, you can make conditional judgments next, thus realizing differentiated content display.
It is worth mentioning that in order to make the template code more readable and maintainable, we usually do not hard-code the model ID directly in the template. AnQiCMS providesmoduleDetailThe tag allows us to dynamically retrieve the ID based on the model's URL alias (token) or name. This is particularly useful when the model ID may change with the environment or customization.
{# 动态获取文章模型和产品模型的ID,避免硬编码 #}
{% moduleDetail articleModelId with name="Id" token="article" %}
{% moduleDetail productModelId with name="Id" token="product" %}
{# 获取当前页面的内容模型ID #}
{% archiveDetail currentModuleId with name="ModuleId" %}
This way, even if the model ID is changed on the backend, our frontend template does not need to be modified.
Conditionally display based on the content model
Once we successfully obtain the current page'sModuleIdCan combine with the AnQiCMS template engine's{% if %}Logical judgment label, build refined conditional display logic.This allows us to render completely different page structures or content segments within the same template file according to different content models.
Assuming we want the page of the "Article Model" to display author information and the publication date, while the page of the "Product Model" displays product price, inventory, and purchase button. Using the above obtainedarticleModelIdandproductModelIdWe can organize the template code like this:
`twig {# This is some detail page template, or a common page fragment #}
{% if currentModuleId == articleModelId %}
{# 这是文章模型特有的内容区域 #}
<article class="article-detail">
<h1>{% archiveDetail with name="Title" %}</h1>
<div class="article-meta">
<span>作者:{% archiveDetail with name="author" %}</span> {# 假设“文章模型”有自定义字段“author” #}
<span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
<span>阅读量:{% archiveDetail with name="Views" %}</span>
</div>
<div class="article-body">
{% archiveDetail articleContent with name="Content" %}{{ articleContent|safe }}
</div>
{# 还可以包含文章特有的评论区、相关文章推荐等 #}
</article>
{% elif currentModuleId == productModelId %}
{# 这是产品模型特有的内容区域 #}
<div class="product-detail">
<div class="product-images">
<img src="{% archiveDetail with name='Logo' %}" alt="{% archiveDetail with name='Title' %}">
{# 可以循环展示更多产品图片:
{% archiveDetail productImages with name="Images" %}
{% for img in productImages %}
<img src="{{ img }}" alt="{% archiveDetail with name='Title' %}">
{% endfor %}
#}
</div>
<div class="product-info">
<h2>{% archiveDetail with name="Title" %}</h2>
<p class="price">价格:¥{% archiveDetail with name="Price" %}</p>
<p class="stock">库存:{% archiveDetail with name="Stock" %} 件</p>
<button class="buy-button">立即购买</button>
<div class="product-description">
<h3>产品简介</h3>
<p>{% archiveDetail with