How to customize the document list template for a specific category?

Calendar 👁️ 55

As a website operator who deeply understands the operation of AnQiCMS, I understand your needs for personalized content display and user experience optimization.The document list template under the custom category is the key step to achieve this goal.AnQiCMS provides a flexible template mechanism that allows you to present a unique list layout according to the characteristics of specific categories.

AnQiCMS template mechanism overview

AnQiCMS uses a syntax similar to the Django template engine, with.htmlas the template file suffix, and stores them uniformly in the site's/templateUnder the directory. Variables in the template are enclosed in double curly braces{{变量}}defined, while conditional judgment, loop control, and other logical tags use single curly braces and percentage signs{% 标签 %}This design concept makes the template structure clear, easy to understand and customize

In AnQiCMS, templates are usually processed throughextendsInherits a basic skeleton template (for examplebase.htmlorbash.html), to ensure consistency of the page structure and in differentblockRegional implementation of content differentiation. This inheritance relationship greatly enhances the reusability and maintenance efficiency of the template.

Two methods for customizing document list templates under specific categories.

In AnQiCMS, you can achieve customization of document list templates for specific categories in two main ways, each with its own focus, and you can choose to use them according to your specific needs.

Method one: automatically apply the template through naming conventions

A smart template naming convention mechanism is built into AnQiCMS, allowing you to create custom templates for specific category lists, the system will automatically recognize and apply according to the agreed filename.

The specific naming format is:{模型table}/list-{分类id}.html

To adopt this method, you need to complete the following steps:

  1. Determine the model table name ({模型table})Each content model in AnQiCMS has a corresponding table name. For example, if you want to customize a category list of the 'article' model, the model table name is usuallyarticle. You can see the 'model table name' or 'URL alias' of each model in the 'Content Model' management interface of AnQiCMS.
  2. Get the category ID ({分类id})In AnQiCMS backend, go to the 'Document Category' management interface to find the specific category you want to customize. Each category will have a unique number ID.
  3. Create a template fileIn the directory of the template theme you are currently using (for example/template/default), find or create the corresponding model folder (for examplearticle/), and then create a folder namedlist-{分类id}.htmlThe file. For example, if your article model table name isarticleand the target category ID is10then the template file path will bearticle/list-10.html.

Once the file is created and placed in the correct location, AnQiCMS will automatically load and apply when the user accesses the category list page with ID 10article/list-10.htmlThis template. This method is suitable for scenarios where deep customization is needed for a few important categories, as it directly associates with the category ID, achieving one-to-one precise matching.

Method two: Set a custom template through the background category settings

AnQiCMS also provides the flexibility to manually specify custom template files for each category in the background.This method provides greater freedom in file names and is convenient for unified management in the background.

The operation steps are as follows:

  1. Prepare the custom template fileIn your template theme directory, you can create a custom template file, for example, you can name itspecial_category_list.html, and place it in sucharticle/directory, forming a patharticle/special_category_list.htmlOr, to better organize, you can also place it in other custom paths, as long as the path is relative to the root directory of the template theme.
  2. Edit category settings: Log in to the AnQiCMS backend, navigate to the "Content Management" section under the "Document Category" page. Find the specific category you want to customize, click the "Edit" button.
  3. Specify "Classification Template"In the 'Other Parameters' section of the category editing page, you will find an input box named 'Category Template'.Enter the relative path and filename of your custom template file here.For example, if the file you create isarticle/special_category_list.htmlEnter herearticle/special_category_list.html.

After filling and saving, when the user visits the list page of this category, AnQiCMS will give priority to the custom template specified in the background.The advantage of this method is that the filename is not limited by the category ID, is more descriptive, and can be centrally managed and adjusted through the back-end interface.At the same time, if the template file does not exist, AnQiCMS will fallback to the default list template to prevent the page from not opening.

Create the content of a custom template

In any way you choose, the core content of the custom template file is similar.It needs to include the page structure and use AnQiCMS template tags to dynamically obtain and display data.

The content of a typical classification list template is roughly as follows:

{% extends "base.html" %} {# 继承您的基础模板 #}

{% block title %}
    <title>{% tdk with name="Title" siteName=true %}</title> {# 使用TDK标签获取当前页面标题 #}
{% endblock %}

{% block content %}
    <div class="category-list-header">
        {# 获取当前分类的详细信息 #}
        {% categoryDetail currentCategory with name="Title" %}
        <h1>{{ currentCategory }}</h1>
        {% categoryDetail categoryDesc with name="Description" %}
        <p>{{ categoryDesc }}</p>
        
        {# 如果分类有Banner图,可以这样显示 #}
        {% categoryDetail bannerImages with name="Images" %}
        {% if bannerImages %}
            {% set pageBanner = bannerImages[0] %}
            <div class="category-banner" style="background-image: url({{pageBanner}});"></div>
        {% endif %}
    </div>

    <div class="document-list-container">
        {# 使用 archiveList 标签获取当前分类下的文档列表,并启用分页 #}
        {% archiveList archives with type="page" limit="10" %}
            {% for item in archives %}
            <div class="document-item">
                <a href="{{item.Link}}">
                    {% if item.Thumb %}<img src="{{item.Thumb}}" alt="{{item.Title}}">{% endif %}
                    <h2>{{item.Title}}</h2>
                    <p>{{item.Description}}</p>
                    <div class="meta">
                        <span>发布日期: {{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                        <span>浏览量: {{item.Views}}</span>
                    </div>
                </a>
            </div>
            {% empty %}
            <p>该分类下暂无文档。</p>
            {% endfor %}
        {% endarchiveList %}
    </div>

    <div class="pagination-area">
        {# 使用 pagination 标签展示分页导航 #}
        {% pagination pages with show="5" %}
            <nav>
                <ul>
                    {% if pages.FirstPage %}<li class="{% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a></li>{% endif %}
                    {% if pages.PrevPage %}<li class="prev"><a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a></li>{% endif %}
                    {% for item in pages.Pages %}<li class="{% if item.IsCurrent %}active{% endif %}"><a href="{{item.Link}}">{{item.Name}}</a></li>{% endfor %}
                    {% if pages.NextPage %}<li class="next"><a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a></li>{% endif %}
                    {% if pages.LastPage %}<li class="{% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a></li>{% endif %}
                </ul>
            </nav>
        {% endpagination %}
    </div>
{% endblock %}

In the above code example,{% extends "base.html" %}Declared the template inherits frombase.html,{% block title %}and{% block content %}Then rewritten the corresponding content area of the basic template.tdkThe tag is used to get the SEO title,categoryDetailThe tag is used to get the title, description, and other information of the current category, andarchiveListThe tag is responsible for retrieving the document list under the category and thentype="page"the parameter enables pagination. Finally,paginationthe tag generates the page navigation.

**Practice and Precautions

When customizing the template, please pay attention to the following points:

  • Path and namingEnsure that your custom template file is located in the current template theme directory, and that the filename or path set in the background is accurate.
  • Template inheritanceUse well{% extends %}and{% block %}Tags, this helps keep the code tidy, modular, and reduce maintenance costs.Place the common structure of the page in a basic template and then overwrite the parts that need to change in the specific page template.
  • Tag usageFamiliarize yourself with the various template tags provided by AnQiCMS (such asarchiveList,categoryDetail,pagination,tdkThey are the key to acquiring and displaying data. Be sure to refer to the official document for tag usage instructions to ensure that the parameters are correct.
  • Cache cleaning: After modifying the template file, the front-end page may not update immediately.This is usually due to system cache or browser cache.Please try to perform the "Update Cache" operation in the AnQiCMS background and clear the browser cache (Ctrl+F5) to ensure that your changes take effect.
  • Error checking: If the page displays an error, first check the server error logs and the AnQiCMS background log records, as they usually provide valuable clues.Template syntax errors may also cause the page to fail to render normally.

By using the above method, you can create a unique document list display for the specific category of the AnQiCMS website, thereby enhancing the level of content operation and user experience.


Frequently Asked Questions (FAQ)

1. I created a custom template file, but after selecting it in the background or placing it according to the naming convention, the page still displays the default template content, or the page reports an error. What is the reason for this?

This is usually caused by several reasons:

  • Incorrect file path or file namePlease carefully check whether the custom template file is located in the currently enabled template theme directory, and whether the filename matches the backend settings or naming conventions (such as{模型table}/list-{分类id}.htmlCompletely match. Even a case error may cause the system to fail to recognize it.
  • AnQiCMS cache is not updatedAfter modifying or adding template files, AnQiCMS may have page cache.Please log in to the backend, click the 'Update Cache' feature, then try to clear the browser cache (Ctrl+F5) and visit the page.
  • Template syntax errorIf there is a syntax error in the custom template file (such as unclosed tags, misspelled variable names, etc.), the system may not be able to parse the template correctly, causing the page to report an error or fall back to the default template.Please check the server error log for detailed error information.

Can I get information about other categories or documents outside the current category in the custom category list template?

Completely. The template tag design of AnQiCMS is highly flexible. You can continue to use it in your custom category list template.categoryListLabel to retrieve other category lists (for example, display sibling categories or subcategories in the sidebar), and can also be usedarchiveListTag to get popular documents, recommended documents, and other categories, just specify the corresponding tags in the tag.moduleId/categoryId/flagParameters can be. For example,{% categoryList subCategories with parentId=currentCategory.Id %}You can get the subcategories of the current category.

3. How should CSS and JavaScript files be referenced in custom templates?

It is recommended to use{% system with name="TemplateUrl" %}Tag to get the static resource root path of the current template theme. For example, if your CSS file is located/template/您的主题名/static/css/style.css, you can use it in the template:<link href="{% system with name="TemplateUrl" %}/static/css/style.css" rel="stylesheet">The benefit of doing this is that even if you change the template theme name, as long as the static resource structure remains unchanged, the reference path does not need to be modified, the system will automatically adapt, enhancing the versatility of the template

Related articles

What are the naming conventions to follow for a specific document detail page template?

As an experienced security CMS website operations personnel, I deeply understand the importance of the naming rules of the content management system (CMS) template for the long-term operation, maintenance efficiency, and content presentation quality of the website.A clear and standardized template naming not only allows team members to quickly understand the purpose of the file, reduce collaboration costs, but also provides strong flexibility and scalability when the website faces a redesign or feature upgrade.In AnQi CMS, the naming of templates for specific document detail pages follows a set of flexible and hierarchical rules, aimed at meeting the needs of content display at different granularity.

2025-11-06

How can I implement a custom template name so that the system can automatically recognize and apply it?

As an experienced security CMS website operator, I am well aware of the importance of flexible template customization for website content display and user experience.The Anqi CMS, with its concise and efficient architecture, provides us with great freedom to manage and display content.How to implement a custom template name so that the system can intelligently recognize and apply it is the key to improving operational efficiency and meeting the needs of personalized display.

2025-11-06

Where should the mobile template files be stored in a specific subdirectory?

As a senior security CMS website operation personnel, I fully understand the core position of content in user experience, and providing optimized content display for different devices is the key to retaining users.When it comes to the mobile template management of AnQi CMS, specifying the storage location of the template files is crucial for efficient content publishing and maintenance.In AnQi CMS, in order to adapt to the display of mobile devices, the system provides a specific mechanism to manage the template files for the mobile end.

2025-11-06

How to design a mobile template for independent mobile sites or code adaptation mode?

As a senior content operator specializing in AnQiCMS, I am well aware that in today's mobile internet era, providing users with an excellent mobile experience is the cornerstone of website success.Anqi CMS, with its flexible architecture, provides us with a variety of solutions to meet the challenges of mobile devices.Today, I will elaborate on how to design high-quality mobile templates for independent mobile sites or code adaptation mode in Anqi CMS.

2025-11-06

How to create and apply an independent template for a specific single page (such as 'About Us')?

As an experienced website operator who deeply understands the operation of AnQiCMS, I understand that you want to create and apply an independent template for a specific single page (such as "About Us").This not only allows the page content to be displayed more personalized, but also effectively meets specific design and functional requirements.AnQiCMS provides a flexible template mechanism, making this process intuitive and efficient.### A detailed guide to creating and applying independent templates for specific single pages in AnQiCMS AnQiCMS is known for its high customizability and flexibility

2025-11-06

How to define a new template, what is the role of the `config.` file?

As an experienced CMS website operation personnel, I am well aware that a flexible and efficient template system is crucial for the presentation of website content and user experience.The AnQi CMS was designed with full consideration of the customizability and ease of use of the templates, allowing both beginners and experienced developers to easily handle it.Next, I will elaborate on how to define a new template in Anqi CMS and deeply analyze the core role played by the `config.` file in this process.

2025-11-06

What are the possible values and their meanings for the `template_type` field in `config.`?

As an experienced CMS website operation personnel, I fully understand the importance of template configuration for the front-end display and user experience of the website.The `config.` file is the core configuration file for each AnQiCMS template, defining its various basic properties and behaviors.Among them, the `template_type` field plays a crucial role, directly affecting the display of the website on different devices.

2025-11-06

How to control the enable status of the template through the `status` field in `config.`?

In the daily operation of Anqi CMS, we often need to adjust the appearance and layout of the website, which usually involves switching or enabling different templates.AnQi CMS provides an intuitive and efficient way to manage the enabled status of templates, which is mainly achieved through the `status` field in the `config.` file under each template directory.

2025-11-06