How to display a specific content block based on the `Id` value of `archiveDetail` or `categoryDetail`?

As an experienced website operations expert, I am well aware that how to flexibly display specific content is crucial for improving user experience and achieving operational goals.AnQiCMS provides us with great convenience with its powerful template engine and rich tag system.archiveDetailandcategoryDetailTagsId[en]To accurately control the display of content blocks on the website.

[en]Safe CMS: Make good use ofarchiveDetailWithcategoryDetail[en]the ID value to achieve accurate content block placement

In the daily operation of the website, we often need to display some unique information blocks for specific content (such as a product introduction, detailed information of an event) or category pages (such as a product series, a blog special topic).These information blocks may be special promotional advertisements, customized sidebar recommendations, or unique descriptions specific to the content/category.If you always achieve it by modifying the code, it is not only inefficient but also prone to errors.archiveDetailandcategoryDetailLabel combination conditions can elegantly solve this problem.

The core idea is to: use labels to obtain the unique identifier of the current or specified content/classification -IdValues, then through the built-in conditional judgment statements of the template ({% if %}To determine whether to render a specific content area.This is like giving each page and category of the website a unique 'identity code', which we can use to 'command' them to display or hide certain elements.

I. Precise customization of document detail page:archiveDetailapplication

When we want to display unique content blocks on a specific document (such as a product detail page or a news article),archiveDetailtags can really shine.

archiveDetailTags are mainly used to obtain detailed data of documents. On the document details page, we can directly access{{archive.Id}}To get the current document ID value. If we need to display content for a specific document ID, we can do it like this:

{% archiveDetail archiveInfo %} {# 默认获取当前页面的文档详情,并赋值给archiveInfo变量 #}

{% if archiveInfo.Id == 10 %}
    <div class="special-promo-block">
        <p>🎉 仅限此产品!立即购买可享八折优惠!</p>
        <img src="/static/images/promo_banner_10.jpg" alt="专属优惠" />
    </div>
{% elif archiveInfo.Id == 25 %}
    <div class="urgent-notice-block">
        <p>⚠️ 这款产品即将下架,欲购从速!</p>
    </div>
{% else %}
    <div class="default-promo-block">
        <p>欢迎浏览我们的热门商品!</p>
    </div>
{% endif %}

In the above example, we first usearchiveDetailLabel retrieves the document information of the current page and assigns it toarchiveInfothe variable. Next, by{% if archiveInfo.Id == 10 %}Check if the current document ID is 10.If so, a dedicated promotional message will be displayed; if it is a document with ID 25, an urgent notification will be displayed; for other documents, a general promotional content will be displayed.This approach allows us to have fine-grained content control over each document page, greatly enhancing the flexibility of content operations.

In addition to retrieving the ID of the current document,archiveDetailthe label also supportsidParameters specify the ID of another document. Although it is usually to customize the content of the current page, in some special scenarios (such as in the sidebar of a document detail page, where you want to display content blocks that are specific to the ID but unrelated to the current page based on user behavior or other logic), you can refer to it in this way:

{# 假设你想在任何页面都显示ID为50的文档特有内容,但不获取其全部信息 #}
{% if archiveDetail("Id", id=50) == 50 %} {# 这里archiveDetail直接返回ID值,与50比较 #}
    <div class="global-special-feature">
        <p>发现我们最新推出的特别功能,详情请点击!</p>
    </div>
{% endif %}

HerearchiveDetail("Id", id=50)This will directly retrieve the document with ID 50.IdField value and compare it with the number 50. Although this method is feasible, a more common and recommended approach is to retrieve the current page.IdAnd make a judgment, because it is more in line with the actual needs of content customization.

Second, flexible control of the classification page:categoryDetailuses

As with the document detail page, we can also display unique content blocks for specific category pages on the website. At this point,categoryDetailtags become our weapons.

categoryDetailTags are used to obtain detailed information about document categories. On the category list page or category detail page, we can go through{{category.Id}}To get the current category ID value. For example, if you want to display a registration entry on the "Latest Activities" category page and a subscription button on the "News Updates" category page, you can do it like this:

{% categoryDetail categoryInfo %} {# 默认获取当前页面的分类详情,并赋值给categoryInfo变量 #}

{% if categoryInfo.Id == 5 %}
    <div class="event-signup-block">
        <h3>🎉 最新活动报名!</h3>
        <p>点击下方按钮,立即参与精彩活动!</p>
        <button>我要报名</button>
    </div>
{% elif categoryInfo.Id == 12 %}
    <div class="newsletter-subscribe-block">
        <h3>✉️ 订阅我们的新闻</h3>
        <p>第一时间获取最新动态和优惠信息。</p>
        <button>立即订阅</button>
    </div>
{% else %}
    <div class="default-category-sidebar">
        <p>这里是通用分类推荐。</p>
    </div>
{% endif %}

Similarly, we utilizecategoryDetailRetrieve the current category information and through{% if categoryInfo.Id == 5 %}[en]Perform condition judgment to accurately control content blocks on different category pages. This is very helpful for marketing activities or content guidance at the category level.

categoryDetail[en]Tags also support this.idParameters can be used to obtain information about a category with a specified ID. For example, you might want to display related promotional information based on the category ID of a product on the bottom of a product detail page, even if the current page is not the homepage of that category:

{# 假设你在一个产品详情页,想根据产品所属分类(productId=100的产品属于分类ID 8)来显示分类特有内容 #}
{% archiveDetail productArchive with id=100 %} {# 获取产品文档详情 #}
{% if productArchive.CategoryId == 8 %}
    {% categoryDetail categoryData with id=productArchive.CategoryId %} {# 获取该分类的详细信息 #}
    <div class="product-category-feature">
        <h3>该分类产品特惠!</h3>
        <p>购买 {{ categoryData.Title }} 下的任意商品,即可享受满减活动!</p>
    </div>
{% endif %}

This example demonstrates how to indirectly access the category of a document page.CategoryIdby using the document's.Id, and further use it to.categoryDetailGet the detailed information of the category, thus driving the display of the content block.

III. Advanced Applications: Implementing dynamic content with custom fields.

Although using directlyIdValue judgment is very effective, but when the conditions become very many (for example, you need to customize content for hundreds of pages), hard-coding a large number ofif/elifThe statement makes the template bulky and difficult to maintain. At this point, the advantage of 'Flexible Content Model' in Anqi CMS is demonstrated.

You can add custom fields to document models or category models in the background. For example, add a field namedspecial_block_typeThe field of (special content block type) has a field type of 'dropdown selection', and the options can be 'none', 'Promotion A', 'Advertisement B', 'Special Recommendation C', and so on.

Then, in the template, you can judge like this:.

{% archiveDetail archiveInfo %} {# 获取当前文档信息,包括自定义字段 #}

{% if archiveInfo.special_block_type == "促销A" %}
    <div class="dynamic-promo-a">
        <p>动态加载的促销A内容...</p>
    </div>
{% elif archiveInfo.special_block_type == "广告B" %}
    <div class="dynamic-ad-b">
        <p>动态加载的广告B内容...</p>
    </div>
{% else %}
    {# 默认内容块 #}
{% endif %}

Through this method, the display logic of the content block no longer depends on specific ID values, but rather on the configurable custom field values on the backend.This allows content operation personnel to flexibly adjust the content blocks of specific pages or categories without touching the code, greatly improving operational efficiency and maintainability.

IV. Practical Suggestions &