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 refined control in SEO optimization.Fortunately, AnQi CMS's powerful template engine provides an intuitive way to determine the context of the current page and conditionally render accordingly.

Understanding the template context of AnQiCMS

The Anqi CMS template engine is very smart, it automatically loads relevant context information when rendering pages based on the current URL and data model.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 will also 'know' that this is a category.We can cleverly use its built-in tags and logical judgments to determine the specific type of the current page.

Determine whether 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 obtain the article detail data of the current page. Anqi CMS providesarchiveDetailLabel, by default, it will try to get the article data on 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 tocurrentArchiveIdVariable.{%-is used to avoid generating extra blank lines during actual rendering. IfcurrentArchiveIdA variable has a value (i.e.{% if currentArchiveId %}if the condition is true), it 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. Anqi CMS providescategoryDetailLabel, similarly, it is unspecifiedidWhen a parameter is specified, it will also try to obtain 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 may have both article ID and category ID, but we want to prioritize identifying it as article details), this judgment is usually placed after the judgment on the article detail page.

`twig {%- categoryDetail currentCategoryId with name=“Id” %}

{% if currentCategoryId %}

{# 这里是分类列表页特有的内容 #}
<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="