As an experienced AnQiCMS website operator, I fully understand the importance of efficient content management and flexible display.In AnQiCMS, the filtering and display of document lists are indispensable functions for building dynamic websites.Whether it is to display the latest articles under a specific category, or to distinguish products and news based on the content model, or to highlight recommended content, AnQiCMS's powerful template tags can help us easily achieve these needs.

This article will detail how to usearchiveListThe tag and its rich parameters, filtered and displayed by category, model, recommended attributes, and other conditions.

archiveListTag: Core of dynamic content display.

In the AnQiCMS template system,archiveListTags are the core tools used to retrieve and display document lists.It provides a diverse set of parameters, allowing us to precisely control the content to be displayed according to specific business needs.This tag allows us not only to get document data but also to sort it, limit the number, and even paginate the display.

UsearchiveListWhen labeling, it is usually like this to define a variable to store the document list obtained:{% archiveList archives with ... %}. Here,archivesThat is the collection of documents we will iterate over.

Filter the document list based on category

When we need to display documents under a specific category,categoryIdthe parameter becomes the key.

We can retrieve the corresponding documents by specifying one or more category IDs. For example, to get all documents under the category with ID 1, you can use:{% archiveList archives with categoryId="1" type="list" limit="10" %}If you need to display documents of multiple categories, the category IDs can be separated by commas, such as:categoryId="1,2,3".

In some cases, we may want to automatically display the current category's documents on the category page. At this point,categoryIdthe parameter can be omitted,archiveListThe label will intelligently read the current page's category ID to retrieve the document. If you need to explicitly not read the current category ID, you cancategoryIdis set to"0".

Furthermore,archiveListthe tags also providechildThe parameter controls whether to include documents with subcategories. By default,childWithtrueThat is, including documents with subcategories. If you want to display only the current category without including its subcategories, you can set it tochild=false. Additionally,excludeCategoryIdParameters allow us to exclude specific categories of documents, which is very useful when building some special content areas.

Filter document lists based on the content model.

AnQiCMS supports custom content models, such as articles, products, etc.moduleIdThe parameter allows us to filter the list based on the content model of the document.

Each content model has a unique ID. For example, suppose the ID of the article model is 1, and the product model is 2, we can specifymoduleIdParameters to retrieve the documentation for the corresponding model. To display the list of all article model documentation, we can write the template code like this: {% archiveList archives with moduleId="1" type="list" limit="10" %}This allows us to easily display different types of content in different areas of the website, such as showing articles only in the 'News Center' and products only in the 'Product Showcase'.

Filter document list based on recommended attribute (Flag)

The AnQiCMS documentation has various recommended attributes (Flag) used to mark the special properties of content, such as 'Top News', 'Recommended', 'Slideshow', and so on.These properties can be set when adding documents in the background and are represented by specific letter codes.

To filter documents based on recommended attributes, you can useflagParameter. For example, to display all documents marked as "recommended" (corresponding flag isc), you can do this:{% archiveList archives with flag="c" type="list" limit="5" %}. AnQiCMS supported recommended attribute letters include: headline[h], recommended[c]of slides[f], special recommendation[a], scroll[s], bold[h], image[p], jump[j].

withcategoryIdsimilar,excludeFlagParameters can be used to exclude documents with specific recommendation properties, which is very useful when building lists that do not display specific recommendation content. If you need to display the flag tag of the document in the document list, you need toshowFlagthe parameter totrue.

Combine multiple conditions for filtering and display

archiveListThe power of tags lies in the ability to combine these filtering conditions to meet more complex business logic.For example, we can filter documents under specific categories, specific content models, and those with certain recommendation properties at the same time.

A common use case is: Display the latest 5 articles in the "Featured News

{% archiveList articles with moduleId="1" categoryId="1" flag="c" order="id desc" type="list" limit="5" %}
    {% for item in articles %}
    <li>
        <a href="{{ item.Link }}">{{ item.Title }}</a>
        <span>{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
    </li>
    {% empty %}
    <li>暂无精选新闻。</li>
    {% endfor %}
{% endarchiveList %}

When displaying a document list, we usually useforto loop througharchiveListtags to return the document collection. Each one in the loopitemrepresents a document object, which contains various fields of the document, such asId/Title/Link/Description/Thumb(thumbnail),Views(Views) andCreatedTime(Creation time) and so on. We can call these fields in a loop to build a rich and diverse display effect. For example,{{ item.Title }}the document title will be displayed, while{{ stampToDate(item.CreatedTime, "2006-01-02") }}The timestamp can be formatted into a readable date.

Sorting, quantity and list type control

Besides the filter conditions,archiveListTags also provide fine-grained control over list sorting, quantity limits, and display types.

orderParameters allow us to define the sorting method of documents, such as by most recent releaseid desc, by viewsviews descOr sort by custom order in the backgroundsort desc.

limitThe parameter controls the number of documents returned. If we need to start from the Nth document and retrieve M documents, we can useoffsetpattern, such aslimit="2,10"It means to get 10 data items starting from the 2nd one.

typeParameters define the display type of the list.type="list"Used to retrieve a fixed number of documents whiletype="page"then withpaginationtags used together to implement pagination display. Whentype="page"then,archiveListAutomatically handles pagination parameters in URLs and generates corresponding pagination links on the page, greatly simplifying the implementation of pagination functions. For example, when using the category list page,type="page"CombinepaginationTags can easily build a complete list of articles with pagination.

Implementation of pagination in document list

When implementing pagination function,archiveListlabel'stypeThe parameter must be set topage. Subsequently, we need to usepaginationTags to render pagination navigation.

The following is a complete pagination list code example, it displays the documents under the article model, categorized by ID 1, and implements pagination with 10 records per page:

<div>
    {% archiveList archives with moduleId="1" categoryId="1" type="page" limit="10" %}
        {% for item in archives %}
        <article>
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p>{{ item.Description }}</p>
            <footer>
                <span>发布日期: {{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                <span>浏览量: {{ item.Views }}</span>
            </footer>
        </article>
        {% empty %}
        <p>此分类下暂无文档。</p>
        {% endfor %}
    {% endarchiveList %}

    <nav class="pagination">
        {% pagination pages with show="5" %}
            <ul>
                {% if pages.FirstPage %}
                <li><a href="{{ pages.FirstPage.Link }}">首页</a></li>
                {% endif %}
                {% if pages.PrevPage %}
                <li><a href="{{ pages.PrevPage.Link }}">上一页</a></li>
                {% endif %}
                {% for pageItem in pages.Pages %}
                <li {% if pageItem.IsCurrent %}class="active"{% endif %}><a href="{{ pageItem.Link }}">{{ pageItem.Name }}</a></li>
                {% endfor %}
                {% if pages.NextPage %}
                <li><a href="{{ pages.NextPage.Link }}">下一页</a></li>
                {% endif %}
                {% if pages.LastPage %}
                <li><a href="{{ pages.LastPage.Link }}">尾页</a></li>
                {% endif %}
            </ul>
        {% endpagination %}
    </nav>
</div>

By using the above method, we can flexibly filter and display document lists in the AnQiCMS template based on various conditions such as categories, models, recommended attributes, etc., which greatly enhances the dynamism and manageability of the website content.

Frequently Asked Questions (FAQ)

1. IfarchiveListNo documents matching the criteria were found, how to display 'No content'?In{% for ... %}You can add inside the loop{% empty %}...{% endfor %}structure. When the list is empty,emptythe content within the tag will be displayed. For example:

{% archiveList articles with categoryId="1" type="list" limit="10" %}
    {% for item in articles %}
        {# 显示文档内容 #}
    {% empty %}
        <p>很抱歉,当前没有找到符合条件的文档。</p>
    {% endfor %}
{% endarchiveList %}

2. Can IarchiveListAre you getting each document's custom field in a loop?Yes. EachitemObject contains default core fields. If you need to access custom fields defined in the document model, you can directly access them throughitem.自定义字段名Visit, provided that this custom field is set in the background and the corresponding value is filled in the document. Or, you can usearchiveParamsto pass the tag,item.IdHow to retrieve all custom parameters of a document and then iterate to display them.

{% archiveList articles with moduleId="1" type="list" limit="5" %}
    {% for item in articles %}
    <p>文档标题: {{ item.Title }}</p>
    {% archiveParams params with id=item.Id %}
        {% for param in params %}
        <p>{{ param.Name }}: {{ param.Value }}</p>
        {% endfor %}
    {% endarchiveParams %}
    {% endfor %}
{% endarchiveList %}

3. How to implement the function of filtering by custom fields on the list page? archiveListThe tag itself does not directly support dynamic filtering of custom fields through parameters in the template. However