How to use AnQiCMS template tags to get a list of documents that meet certain conditions (archiveList)?

As an experienced CMS website operation personnel in the security industry, I fully understand the crucial role of efficient content management and accurate content presentation in the success of a website.In AnQiCMS, flexibly using template tags is an important way to achieve this goal.archiveListTemplate tags, retrieve and display document lists based on specified conditions to meet diverse content display needs.

MasterarchiveListTag: The powerful foundation for content display.

In AnQiCMS template development,archiveListTags are the core tools for retrieving document lists. Whether you need to display the latest articles, popular products, content under specific categories, or even implement complex filtering and search results,archiveListIt can provide strong support. It can extract document data that meets the conditions you set from the database and display it in the front-end template for looping.

archiveListThe basic syntax structure of the tag is concise and clear, usually with{% archiveList 变量名称 with 参数 %}...{% endarchiveList %}in the form. In which, "variable name" is a variable you define to carry the document data set obtained in the loop;withAfter the keyword, you can add a series of parameters to precisely control the filtering and sorting of documents.

Core Parameters Explanation: Precisely control the retrieval of documents

To give full play toarchiveListThe power, understanding its various parameters is crucial. Here are some core parameters you will commonly use when applyingarchiveList:

  • moduleId(Model ID)This parameter allows you to specify from which content model to retrieve documents. For example,moduleId="1"It is usually used to retrieve documents from the article model,moduleId="2"May be used to obtain the document of product models. Accurately specifying the model ID can avoid data confusion across models and ensure the professionalism of the content.

  • categoryId(Category ID)PasscategoryIdYou can specify to retrieve documents under a specific category. For example,categoryId="1"Retrieve all documents under the category with ID 1. If you need to retrieve documents from multiple categories, you can separate multiple IDs with commas, likecategoryId="1,2,3"It is worth noting that if this parameter is not specified,archiveListit will attempt to automatically read the category ID of the current page. If you wish to disable this automatic behavior completely,categoryId="0".

  • excludeCategoryId(Exclude Category ID)WithcategoryIdOn the contrary,excludeCategoryIdUsed to exclude documents under a specific category. This is very useful when you need to display all content except for certain categories.

  • userId(Author ID)If you need to display documents published by a specific author, you can useuserIdparameters, such asuserId="1"The documents published by the author with user ID 1 will be retrieved.

  • parentId(Parent document ID)This parameter is very useful when there is a hierarchical relationship between documents in the documentation. For example, you may need to get all the 'child documents' under a 'parent document'.

  • flag(Recommended property)'} ]AnQiCMS provides various recommended attributes for documents, such as headline[h], recommendation[c], slideshow[f], and so on. Throughflag="c"You can easily filter documents with the 'recommended' attribute, which is very helpful for displaying recommended content on the website homepage or category page.

  • excludeFlag(Exclude recommended attribute)'} ]Withflagis similar,excludeFlagAllow you to exclude documents with specific recommendation properties, such as not displaying 'Top Stories' content in a certain area.

  • showFlag(Whether to display the flag of the document)'} ]By default, the recommended properties of the document (Flagfield) are not visible in the list items. If you need to display these properties on the front end, for example using icons or text labels, you must setshowFlagsettrue.

  • child(Whether to display child category content)When you retrieve documents for a category,childThe parameter (default is)true)Determine whether to include documents under its subcategories. If you only want to display documents of the current category without including its subcategories, please setchild=false.

  • order(Sorting method)The sorting method of documents is crucial.orderParameter supports multiple sorting rules:

    • order="id desc"Sorted by document ID in descending order (latest release):
    • order="views desc"Sorted by view count in descending order (most popular):
    • order="sort desc":Sort by custom backend (default behavior) You can select an appropriate sorting method according to your needs.
  • limit(Number of displayed items) limitParameter controls the number of documents fetched and displayed at one time. For example,limit="10"This will only display the first 10 documents. It also supports the "offset" mode, for examplelimit="2,10"This indicates starting from the second document and fetching 10 data items.

  • type(list type)This isarchiveListA very important parameter that determines the purpose and behavior of the list:

    • type="list": Default value, only displays the specifiedlimitnumber of documents without involving pagination.
    • type="page": Used for document lists that require pagination. When using this type, it is usually配合paginationtags to implement pagination functionality. In addition,qparameters and custom filter parameters also only take effect under this type.
    • type="related"Used to retrieve a list of related documents. It is usually used on the document detail page and can recommend related content based on the current document's keywords or manually associated documents.
  • q(Search keyword)Whentype="page"whenqThe parameter allows you to specify a search keyword to filter the document list. For example,q="seo"only documents with the keyword 'seo' in the title will be displayed. If the URL already containsq=关键词Query parameters,archiveListwill also be automatically read and applied.

  • Custom filter parametersAnQiCMS supports custom filtering of documents via URL query parameters. If you define additional filterable fields in the content model (such as "property type"), users can filter throughurl?房屋类型=住宅This way to filter documents.

  • siteId(Site ID)In a multi-site management environment,siteIdThe parameter allows you to call document data across sites.

  • combineId/combineFromId(Combined document ID)These special parameters are used to create composite document lists, such as 'Travel routes from place A to place B'.They allow you to combine each document in the list with another specified document and reflect this combination relationship when displaying the title and link.

Document fields available within the loop

WhenarchiveListAfter obtaining the document set, you can useforLoop through each document and access its field information. In the loop,itemThe variable name (or you can customize it) represents the current document object, and you can access its properties like this:{{item.Title}}/{{item.Link}}etc.

The following are some commonly used document fields:

  • Id: The unique ID of the document.
  • Title: Document Title.
  • Link: Full URL Link of the Document.
  • Description: Brief Description of the Document.
  • Logo: Cover Image URL of the Document.
  • Thumb: Thumbnail URL of the Document's Cover.
  • Images: The group chart of the document (an array of URLs).
  • CreatedTime: Document creation timestamp (required)stampToDateto be formatted).
  • Views: Document views.
  • Flag: Document recommendation attributes (required)showFlag=trueCan be displayed).
  • Category: The category object of the document (can be accessed directly)item.Category.Titleetc.).
  • Other field parameters set in the document model: Through{% archiveParams params with id=item.Id %}Or directly access{{item.自定义字段名}}to get.

Practice cases: diversified content display

Now, let's demonstrate through several actual code snippets.archiveListOf the powerful functions.

Example one: Display the latest article list

{# 显示最新发布的文章,仅限文章模型,前10条 #}
<div class="latest-articles">
    <h2>最新文章</h2>
    <ul>
        {% archiveList articles with moduleId="1" order="id desc" limit="10" %}
            {% for article in articles %}
            <li>
                <a href="{{ article.Link }}">
                    <img src="{{ article.Thumb }}" alt="{{ article.Title }}">
                    <h3>{{ article.Title }}</h3>
                    <p>{{ article.Description }}</p>
                    <span>发布于: {{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
                </a>
            </li>
            {% empty %}
            <li>暂无最新文章。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

Example two: Product list of a specific category with pagination

Suppose we want to display products under the 'Electronics' category (ID 5) and support pagination.

{# 显示电子产品分类下的产品,带分页功能 #}
<div class="product-category-list">
    <h2>电子产品</h2>
    {% archiveList products with moduleId="2" categoryId="5" type="page" limit="12" %}
        <div class="product-grid">
            {% for product in products %}
            <div class="product-item">
                <a href="{{ product.Link }}">
                    <img src="{{ product.Logo }}" alt="{{ product.Title }}">
                    <h3>{{ product.Title }}</h3>
                    <p>价格: ¥{{ product.Price }}</p>
                </a>
            </div>
            {% empty %}
            <p>该分类下暂无产品。</p>
            {% endfor %}
        </div>

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

Example three: Display related articles on the document detail page.

In the article detail page, it is common to recommend some other articles related to the current article to increase user stay time.

{# 在文章详情页显示相关文章,根据关键词推荐 #}
<div class="related-articles">
    <h3>相关推荐</h3>
    <ul>
        {% archiveList relatedDocs with type="related" like="keywords" limit="5" %}
            {% for doc in relatedDocs %}
            <li>
                <a href="{{ doc.Link }}">
                    <span>{{ doc.Title }}</span>
                    <span>({{ stampToDate(doc.CreatedTime, "2006-01-02") }})</span>
                </a>
            </li>
            {% empty %}
            <li>暂无相关推荐。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

Summary

archiveListTags are an extremely powerful and flexible component in the AnQiCMS template system. By mastering its various parameters and the document fields available within the loop, you can achieve everything from simple content lists to complex filtering and searching.