As an experienced website operations expert, I know that how to flexibly customize page layout in a content management system is crucial for improving user experience and optimizing SEO.AnQiCMS (AnQiCMS) leverages its powerful template engine and rich tag features to provide us with the tool we need to achieve this goal.Today, let's delve deeply into a common and practical need: how to determine whether the current page in AnQi CMS is a detail page of a document tag and apply a unique layout for it.

Understanding the document tag detail page in Anqi CMS

In AnQi CMS, document tags (Tag) are an important way to organize content.When a user clicks on a tag or accesses the aggregated content of a tag directly through a URL, we will usually enter a 'document tag detail page'.This page is mainly responsible for displaying all relevant documents under the tag, and may also accompany some introduction information about the tag itself.

The template design of AnQi CMS follows a set of intuitive conventions, which automatically matches the corresponding template file based on the URL path. For example, for a page displaying a document list under a certain tag, the system will usually search for and applytag/list.html(In folder organization mode) ortag_list.html(In flat organization mode) it serves as the main template. This means that if you want to design a completely different layout for the label detail page, the most direct method is to customize these exclusive template files.

However, sometimes we may not want to create an independent template for each tag detail page, but rather hope to have a generic layout file (like the websitebase.htmlor headerpartial/header.htmlIn this case, based on whether the current page is a tag detail page, we dynamically adjust the content or style of certain blocks. This requires us to introduce conditional judgment logic from the template.

Core judgment mechanism: Is the current page a tag detail page?

To determine whether the current page is a document tag detail page, we can cleverly utilize the Anqi CMS providedtagDetailTag. This tag is used to retrieve the detailed information of the current page. If the current page is indeed a tag detail page, thentagDetailThe tag can successfully retrieve the data of the tag, such as its ID, title, description, etc. Conversely, if the current page is not the tag detail page (such as the article detail page, category list page, or homepage), thentagDetailThe tag cannot retrieve valid tag data.

Based on this principle, we can construct a simple conditional judgment:

{%- tagDetail currentTagId with name="Id" %} {# 尝试获取当前页面的Tag ID #}
{% if currentTagId %}
    {# 代码块:当前页面是标签详情页时执行 #}
    {# 在这里,你可以访问 {% tagDetail with name="Title" %} 来获取标签标题等信息 #}
{% else %}
    {# 代码块:当前页面不是标签详情页时执行 #}
{% endif %}

In this code block,{%- tagDetail currentTagId with name="Id" %}It will try to assign the current page tag ID tocurrentTagIdVariable. This-symbol is used to remove extra whitespace from label lines to maintain code neatness. If the assignment is successful (i.e.currentTagIdthere is a value), then{% if currentTagId %}This condition will be met, thus executing the unique layout code of the tag detail page.

This judgment logic can be placed in any template file of the website, including the generalbase.htmlor beincludeto each pagepartial/header.htmlso that conditional layout adjustments can be implemented throughout the website.

Create a custom layout: two practical strategies

Once we can accurately determine the type of the current page, we can start implementing special layouts. Here are two common strategies that you can choose according to your actual needs:

Strategy one: Make conditional judgments and content adjustments in the general template (suitable for local fine-tuning)

When you only need to adjust some local elements on the tag detail page, for example, modify the page'stitleAdd a special header banner or change the display content of the sidebar, this strategy is very applicable. You can do this in the general layout file of the website, such asbase.htmlOr public fragments such aspartial/header.htmlUsed as described aboveifConditional judgment.

Example scenario: Set a unique page title and introduction for the tag detail page

Suppose yourbase.htmlContains<title>Tag and welcome information at the top of the page:

`twig {# base.html #} <!DOCTYPE html>

<meta charset="UTF-8">
{%- tagDetail currentTagId with name="Id" %}
{% if currentTagId %}
    {# 标签详情页的专属标题和描述 #}
    <title>{% tagDetail with name="Title" %} - 相关文档 - {% system with name="SiteName" %}</title>
    <meta name="description" content="关于{% tagDetail with name="Title" %}标签的所有相关文档和信息。{% tagDetail with name="Description" %}">
{% else %}
    {# 其他页面的通用标题和描述 #}
    <title>{% tdk with name="Title" siteName=true %}</title>
    <meta name="description" content="{% tdk with name="Description" %}">
{% endif %}
{# 其他通用 head 内容 #}

<header>
    {%- tagDetail currentTagId with name="Id" %}
    {% if currentTagId %}
        <div class="header-tag-banner">
            <h1>标签:{% tagDetail with name="Title" %}</h1>
            <p>{% tagDetail with name="Description"|default:"探索与此标签相关的所有精彩内容。" %}</p>
        </div>
    {% else %}
        <div class="header-general-banner">
            <h1>欢迎来到我们的网站</h1>