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

As an experienced website operations expert, I am well aware that how to flexibly display specific content in content management is crucial for improving user experience and achieving operation goals.AnQiCMS (AnQiCMS) with its powerful template engine and rich tag system, has provided us with great convenience.Today, let's delve into how to巧妙ly usearchiveDetailandcategoryDetaillabel'sIdTo precisely control the display of content blocks on the website.

AnQi CMS: How to usearchiveDetailwithcategoryDetailID value, to achieve precise placement of content blocks.

In the daily operation of website management, we often need to display some unique information blocks for specific content (such as a product introduction, an event detail) or category pages (such as a product series, a blog topic).These information blocks may be special promotional advertisements, customized sidebar recommendations, or unique descriptions for the content/category.If each time is achieved by modifying the code, it is not only inefficient but also prone to errors.The template mechanism of Anqi CMS, especiallyarchiveDetailandcategoryDetailTags combined with conditional judgment, can elegantly solve this problem.

The core idea lies in using tags to obtain the unique identifier of the current or specified content/classification -IdThen, through the conditional judgment statements built into the template ({% if %}),to determine whether to render a specific content area. It's 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.

One, 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 the document. On the document details page, we can directly go through{{archive.Id}}To get the current document ID value. If we need to display content for a specific document ID, we can operate 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 usearchiveDetailThe tag retrieves the document information of the current page and assigns it toarchiveInfothe variable. Then, by{% if archiveInfo.Id == 10 %}Determine if the current document ID is 10. If so, a special promotional message will be displayed;If the document with ID 25 is displayed, an emergency notification will be shown;For other documents, a general promotional content will be displayed.This allows us to fine-tune content control for each document page, greatly enhancing the flexibility of content operations.

In addition to retrieving the ID of the current document,archiveDetailThe tag also supports passing throughidThe parameter specifies the ID of another document. Although we usually customize the content of the current page, in some special cases (such as in the sidebar of a document detail page, where you want to display content blocks unrelated to the current page but with a specific ID based on user behavior or other logic), you can also 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)The document with ID 50 will be directly retrievedIdThe field value is compared with the number 50. Although this usage is feasible, a more common and recommended way is to get the current page.IdAnd make a judgment, because it is more in line with the actual demand for content customization.

2. Flexible control of the category page:categoryDetailusefulness

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

categoryDetailThe tag is used to obtain detailed information about document categories. On the category list page or category detail page, we can{{category.Id}}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 Dynamics" 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 proceed by{% if categoryInfo.Id == 5 %}Judging by certain conditions, it accurately controls the content blocks of different category pages. This is very helpful for carrying out marketing activities or content guidance at the category level.

categoryDetailTags also support.idA parameter that can be used to retrieve category information for a specified ID. For example, you might want to display related promotional information at the bottom of a product detail page based on the category ID of the product, 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 access the category of a document indirectly through the document'sCategoryIdto get its categoryIdand further usecategoryDetailto get the details of the category, thereby driving the display of content blocks.

Step 3: Advanced Application: Implementing Dynamic Content with Custom Fields

Although used directlyIdIt is very effective to make judgments, but when the conditions become very many (for example, when you need to customize content for hundreds of pages), hardcoding a large number ofif/elifThe statement makes the template bloated and difficult to maintain. At this point, the advantage of Anqi CMS's 'flexible content model' becomes evident.

You can add custom fields to the document model or category model in the background. For example, add a field namedspecial_block_typeThe field is of type 'Dropdown Selection', options can be 'None', 'Promotion A', 'Ad B', 'Special Recommendation C', etc.

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 %}

In this way, the display logic of the content block no longer depends on the specific ID value, but on the customizable field values configurable on the backend.This allows content operations personnel to flexibly adjust specific page or category content blocks without touching the code, greatly improving operational efficiency and maintainability.

Four, practical suggestions and