How to determine if the current page belongs to a specific content model (such as 'article model'), and display different content accordingly?

Calendar 👁️ 103

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

Related articles

Does the `moduleDetail` tag support direct retrieval of custom field values defined in the content model, and if so, how do you call it?

As an experienced website operations expert, I know the importance of a flexible content model and convenient template calls for a modern CMS.AnQiCMS (AnQiCMS) performs well in this aspect, its powerful template tag system makes content presentation flexible and efficient.Today, let's delve deeply into a question that concerns everyone: Does the `moduleDetail` tag support direct retrieval of custom field values defined in the content model?How can it be called if it can? Firstly, let's clarify the division of responsibilities of different tags in the Anqi CMS

2025-11-07

How to retrieve the database table name (`TableName`) of the content model in a template for a custom query?

As an experienced website operations expert, I deeply understand the powerful capabilities brought by the flexible content model in AnQiCMS through my practice.Many times, we not only need to display the content itself, but also need to drive deeper page logic or style based on the type of content.Today, let's delve deeply into a seemingly minor but crucial feature: how to obtain the database table name (TableName) of the content model in AnQiCMS templates, thus achieving more refined custom control.--- ###

2025-11-07

What is the difference and usage of the `Title` and `Name` fields in the content model in practice?

In the daily operation of AnQiCMS, we often encounter content models and their fields.Among the field names "Title" and "Name", which seem similar, there are completely different responsibilities in practical application.As an experienced website operations expert, I fully understand the importance of understanding these subtle differences for efficient content management, optimizing website structure, and improving user experience.

2025-11-07

If my AnQi CMS is a multi-site deployment, how to use the `moduleDetail` tag to retrieve the content model data of other sites?

AnQi CMS is an enterprise-level content management system designed specifically for small and medium-sized enterprises, self-media, and multi-site management, and its multi-site management capabilities are undoubtedly one of the core highlights.In daily operations, we often encounter such scenarios: we hope to display certain information of the child site on the main site, or to cross-reference content or data models between different sites.Today, let's delve into a specific and practical scenario - how to use the `moduleDetail` tag to obtain content model data from other sites in a multi-site deployed Aiqi CMS.###

2025-11-07

When creating a new content model, what are the naming restrictions and practices for the `model table name`?

## Build an Efficient Content Model: The Art and Practice of Naming `Model Table Name` in Anqi CMS As an experienced website operations expert, I fully understand that every detail in a content management system can affect the performance, stability, and future scalability of a website.AnQiCMS (AnQiCMS) provides strong support for building a diverse content structure with its flexible content model features.However, when customizing content models, a seemingly trivial aspect - the naming of the `model table name`, entails many limitations and**practical considerations, worthy of in-depth exploration

2025-11-07

How should the `URL alias` of the content model be set to better support the `{module}` variable in the SEO static rules?

## Unlock AnQiCMS SEO Potential: The Clever Combination of Content Model URL Aliases and Permalinks In today's highly competitive online environment, whether a website can stand out in search engines largely depends on the implementation of its SEO optimization strategy.As an experienced website operations expert, I am well aware of the importance of URL structure for SEO.A clean, meaningful, and SEO-friendly URL is the foundation for improving website ranking.

2025-11-07

What impact will modifying the `URL alias` of the content model have on the existing page URL structure?

Good, as an experienced website operation expert, I am willing to delve into the impact and coping strategies of modifying the content model 'URL alias' in AnQi CMS.In AnQi CMS, such an efficient and flexible content management system, the setting of `URL alias` seems trivial, but in fact, it affects the whole body, especially in content operation and SEO, its impact should not be overlooked.

2025-11-07

I want to set the document title prompt for the "Product Model" to "Product Name", where should I configure the setting item in the content model?

As an experienced website operations expert, I am well aware of the importance of a flexible and user-friendly content management system for improving operational efficiency.AnQiCMS (AnQiCMS) meets the personalized needs of enterprises in content management with its powerful content model customization capabilities.Today, let's delve deeply into a common issue in actual operation that can significantly improve the content entry experience: how to set the document title prompt of the 'product model' to be more intuitive and closely aligned with the business scenario, namely the 'product name'.### Optimize content entry experience

2025-11-07