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

Calendar 👁️ 96

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="

Related articles

How to enable automatic image compression and WebP format conversion in AnQiCMS to improve page loading speed?

In today's fast-paced online environment, website loading speed has become a key indicator of user experience and search engine rankings.If your website has many images, they may be slowing down your website speed quietly.Fortunately, AnQiCMS fully considers this point and provides us with automatic image compression and WebP format conversion functions, which can effectively accelerate page loading and make your website fly.Why is image optimization so important?\n\nImagine when a user visits a page, if the images take a long time to display or load slowly

2025-11-08

How to safely output article content containing HTML tags in AnQiCMS templates (to prevent XSS)?

When processing article content that contains HTML tags in AnQiCMS templates, securely outputting is a crucial issue, as it directly concerns the safety and user experience of the website.Especially when the content of the article may be input by the background rich text editor, or when comments and messages submitted by users contain custom HTML, improper handling is likely to be attacked by cross-site scripting (XSS).AnQiCMS as a content management system that focuses on security and efficiency, provides the corresponding mechanisms to help us meet this challenge.Understanding Risk

2025-11-08

How to set and display the website filing number and copyright information at the bottom of the AnQiCMS page?

The website's filing number and copyright information are important elements that demonstrate the compliance and professionalism of the website. They usually appear in the footer, providing visitors with a sense of trust.In AnQiCMS, setting and displaying this information is simple and efficient.We will guide you through this process, ensuring that the key content is clearly presented at the bottom of your website. ### Step 1: Set the record number and copyright information in the AnQiCMS background First, log in to your AnQiCMS background management interface. This is the starting point of the basic information configuration for all websites. 1. **Navigate to the global feature settings:**

2025-11-08

How to display the title and link of the previous and next articles on the article detail page?

In website content operation, improving user experience and content browsing depth is indispensable.After a visitor finishes reading an article, if they can easily browse to the titles and links of the "previous" and "next" articles, it will undoubtedly greatly encourage them to continue exploring the website's content, thereby extending their stay time, reducing the bounce rate, and even having a positive impact on the overall SEO performance of the website.AnQi CMS is an efficient and customizable content management system that fully considers this user requirement.A safe CMS to simplify the implementation of this common website feature provides intuitive and powerful template tags

2025-11-08

How does AnQiCMS implement pagination display on the article list page?

In website content operation, especially when the list content of articles, products, etc. becomes increasingly rich, how to efficiently display this information while ensuring user experience and website performance is a problem that needs to be carefully considered.Long list pages not only load slowly but also tend to make visitors feel tired.It is particularly important to reasonably introduce the pagination display function at this time.AnQiCMS as a powerful content management system fully considers this operation requirement.

2025-11-08

How to set and call the TDK (Title, Description, Keywords) on the AnQiCMS backend homepage?

In AnQiCMS, the TDK (Title, Description, Keywords) settings on the homepage are an indispensable part of website SEO optimization, directly affecting the first impression of the website by search engines and the willingness of users to click on the search results and enter the website.AnQiCMS as a highly SEO-friendly content management system provides an intuitive and convenient way to manage this key information.

2025-11-08

How to display a list of recommended content related to the current article on the article detail page?

In content operations, the value of a high-quality article is not only in itself, but also in its ability to guide readers to discover more valuable content.Properly displaying a list of related recommended content at the bottom of the article detail page is an effective strategy to improve user experience, extend visit duration, and reduce the bounce rate.For users of AnQiCMS, implementing this feature is quite direct and flexible.AnQiCMS with its concise and efficient architecture provides powerful template customization capabilities for content managers.Implement related recommendations for the article detail page

2025-11-08

How to call and render a custom Banner carousel in AnQiCMS template?

How to easily call and render a custom Banner carousel in AnQiCMS template?In website operation, the Banner carousel is an important element to enhance the visual appeal of the page, convey core information, and guide user behavior.AnQiCMS as a powerful content management system provides users with flexible ways to manage and call these custom Banners, allowing your website content to be displayed in a more vivid and dynamic way.### One, Back-end Management: Prepare content for Banner Carousel In AnQiCMS

2025-11-08