How to determine in the template whether the current page is an article detail page or a category list page, and display different content?

In the template development of AnQi CMS, it is a very common requirement to display different content according to the type of the current page (whether it is an article detail page or a category list page).This not only allows the website to provide a more personalized user experience, but also offers more precise control in SEO optimization.It is fortunate that the powerful template engine of AnQi CMS provides an intuitive way to determine the context of the current page and conditionally render accordingly.

Understanding the template context of AnQiCMS

The template engine of AnQi CMS is very intelligent, it will automatically load related context information according to the current URL and data model when rendering the page.This means that when you are on an article detail page, the template automatically 'knows' that it is an article; when you are on a category list page, it also 'knows' that it is a category.We can cleverly use its built-in labels and logical judgments to determine the specific type of the current page.

Determine if the current page is an article detail page

Determine whether the current page is an article detail page, the most direct and effective method is to try to retrieve the article detail data of the current page. Anqi CMS providesarchiveDetailLabel, by default, it will try to get the article data of the current page.If the current page is indeed an article detail page, then the article ID obtained through this tag will have a value.

We can make such a judgment in the template:

{%- archiveDetail currentArchiveId with name="Id" %}

{% if currentArchiveId %}
    {# 这里是文章详情页特有的内容 #}
    <div class="article-detail-header">
        <h1>{% archiveDetail with name="Title" %}</h1>
        <p>发布时间:{% archiveDetail with name="CreatedTime" format="2006-01-02" %}</p>
        {# 可以在文章详情页显示返回其所属分类列表的链接 #}
        {%- archiveDetail articleCategory with name="Category" %}
        {%- if articleCategory %}
            <a href="{{ articleCategory.Link }}" class="back-to-category">返回 {{ articleCategory.Title }} 列表</a>
        {%- endif %}
    </div>
    <div class="article-content">
        {%- archiveDetail articleContent with name="Content" %}
        {{ articleContent|safe }} {# 使用safe过滤器确保HTML内容正确渲染 #}
    </div>
    {# 可以在这里添加“上一篇/下一篇”导航、相关推荐等 #}
    <div class="navigation">
        {% prevArchive prev %}
            {% if prev %}<a href="{{ prev.Link }}">上一篇:{{ prev.Title }}</a>{% else %}<span>没有上一篇了</span>{% endif %}
        {% endprevArchive %}
        {% nextArchive next %}
            {% if next %}<a href="{{ next.Link }}">下一篇:{{ next.Title }}</a>{% else %}<span>没有下一篇了</span>{% endif %}
        {% endnextArchive %}
    </div>
{% endif %}

In this code, we first use{%- archiveDetail currentArchiveId with name="Id" %}Try to get the current article ID and assign it tocurrentArchiveIda variable.{%-is used to avoid generating extra blank lines during actual rendering.currentArchiveIdThe variable has a value (i.e.,{% if currentArchiveId %}The condition is true), which means that the current page is an article detail page, and we can display all the special content of the article detail page within this code block.

Determine if the current page is a category list page

If the current page is not an article detail page, we need to determine whether it is a category list page next. The Anqi CMS providescategoryDetailLabel, similarly, it is unspecifiedidWhen parameters are provided, it will also attempt to retrieve the category data of the current page. If the current page is a category list page, then the category ID obtained through this tag will have a value.

To avoid judgment conflicts (a page has both article ID and category ID, but we want to identify it as article details first), this judgment is usually placed after the judgment on the article detail page.

English

{% if currentCategoryId %} English

{# 这里是分类列表页特有的内容 #}
<div class="category-list-header">
    <h1>{% categoryDetail with name="Title" %}</h1>
    <p>{% categoryDetail with name="Description" %}</p>
    {# 显示分类Banner图,如果存在的话 #}
    {%- categoryDetail categoryBanner with name="Images" %}
    {%- if categoryBanner %}
        <img src="{{ categoryBanner[0] }}" alt="{% categoryDetail with name='Title' %}" class="category-banner">
    {%- endif %}
</div>
<ul class="article-list">
    {% archiveList archives with type="