As an experienced website operations expert, I deeply understand that how to efficiently obtain and display content is the core of building a website. Today, let's delve into a very important and powerful tag in the Anqi CMS template -archiveListIt can help us flexibly obtain various document lists, providing infinite possibilities for the presentation of website content.
Overview of core features: archiveListthe function
In AnQi CMS,archiveListTags are the foundation of content display. It is not just a simple document list retrieval tool, but also a multifunctional 'content scheduler'.With it, we can easily filter, sort, and display various types of documents, whether it's a regular article list, product display under a specific category, recommended articles related to the current content, or even a large amount of content that requires pagination.archiveListAll of them can handle it with ease.
Imagine, your website has a 'Latest News' section that needs to display the latest ten articles; or on the product detail page, you need to recommend several 'related products'; or on a category page, you need to present all articles in that category in a paginated form. The acquisition and display of these dynamic contents cannot be separated fromarchiveListThe exquisite application.
Basic syntax and beginner: how to callarchiveList
archiveListThe use of tags conforms to the unified style of Anqi CMS template engine, namely with{% %}wrapped, and throughwithPass parameters with keywords. Its basic structure is concise and clear:
{% archiveList 变量名称 with 参数 %}
{% for item in 变量名称 %}
{# 在这里展示文档内容 #}
{% empty %}
{# 如果没有内容,显示此段落 #}
{% endfor %}
{% endarchiveList %}
Here, 变量名称is a variable you define yourself, for examplearchivesorarticles.archiveListThe tag will store the list of documents obtained in this variable, and then you can go on to do things likeforLoop through this variable, displaying the details of each document one by one.{% empty %}A clause is a very considerate design, which can elegantly display a prompt message when the list is empty, rather than making the page look empty.
Deep understanding:archiveListCommon parameters
archiveListThe power of tags lies in their rich parameter options. By combining these parameters, you can precisely control the scope and presentation of the documents you want to obtain.Let's take a look at several core parameters:
Locate the content range:
moduleIdwithcategoryIdmoduleIdSpecify the document of the content model you want to retrieve. For example,moduleId="1"may represent the article model, andmoduleId="2"may represent a product model. This allows you to easily switch between different types of content.categoryIdIf you only want to retrieve documents under a specific category, you can use this parameter. For example,categoryId="5"Will retrieve all documents under the category with ID 5. If you need to retrieve documents from multiple categories, you can separate them with a comma, such ascategoryId="1,2,3". It is worth mentioning that if set tocategoryId="0"Or completely omit this parameter,archiveListIt will try to retrieve the documents of the current page category or all categories, depending on the cooperation of other parameters.excludeCategoryId: withcategoryIdOn the contrary, it is used to exclude documents of specified categories.parentIdUsed to retrieve child documents under a parent document, which is very useful when building content with hierarchical relationships such as project cases and multi-step tutorials.
Filter content attributes:
flagwithexcludeFlagThe AnQi CMS supports setting various recommended attributes for documents (such as headlines [h], recommendations [c], slides [f], etc.).flag: Passflag="c"You can only retrieve documents marked as 'recommended'.excludeFlag: Used to exclude documents with specific properties, for exampleexcludeFlag="h"The headline article will not be displayed.showFlag: If you need the flag attribute of the document to be displayed in the list, set it toshowFlag=true.
Sorting and limiting:
orderwithlimitorder: Determines the sorting method of the documents. Common values include:id desc(Sorted by ID in descending order, usually the most recent posts),views desc(Sorted by views in descending order, popular articles),sort desc(Sorted by custom backend). You can choose the most suitable sorting method according to your needs.limit: Controls the number of documents returned.limit="10"It will only retrieve 10 documents. It also supportsoffsetpattern, such aslimit="2,10"Indicates starting from the second item to fetch 10 items, which is very suitable for skipping the first few items to display.
Display mode:
typetypeThe parameter isarchiveListOne of the core features, which defines the display type of the document list:type="list": Default mode, retrieves a specified number of documents without pagination. Suitable for sidebars, recommendation lists, etc.type="page": Pagination mode. When you need to display a large number of documents in multiple pages, you must use this type. It will cooperatepaginationCreate pagination links with tags.type="related": Related document mode. Typically used on detail pages, intelligently recommending related content based on the current document category, keywords, or backend settings.
Dynamic search and filtering:
qWith custom filtering parametersqUsed to implement search functionality. For example,q="关键词"Only documents with the keyword in the title will be displayed. Whentype="page"then,archiveListIt will automatically read the parameters in the URL.qfor searching.- Custom filter parameters: If you define filterable fields in the background content model (such as "gender", "region"), these fields can be passed to the URL parameters via
archiveListPerform advanced filtering, for examplesex=男.
Multiple sites and combined documents:
siteId/combineIdwithcombineFromIdsiteId: If you have enabled the multiple sites feature, you cansiteIdspecify from which site to retrieve documents.combineId/combineFromIdThis is a unique and powerful feature of AnQi CMS, used to implement the 'combination comparison' of documents.For example, on a travel route website, you can combine the travel information of "Beijing" and "Shanghai" into a "travel route from Beijing to Shanghai", and then obtain the data of these two documents in the template for comparison and display.
Data access:archiveListFields available in the loop
OncearchiveListFill data into your custom variables (for examplearchives) so you can access various properties of each document. Common fields include:forthrough the loopitemVariable access to various properties of each document. Common fields include:
item.Id: Document ID.item.Title: The document title.item.Link: Document link.item.Description: Document description.item.Logo: The cover image of the document (usually the large image of the thumbnail).item.Thumb: The thumbnail of the document cover.item.Views: Document views.item.CreatedTime: The creation time of the document (timestamp, which needs to be配合stampToDateFiltered formatting()).item.UpdatedTimeThe document update time (timestamp, formatted as well).item.CategoryIdThe document category ID it belongs to.- And all custom fields in the content model, you can directly access through
item.自定义字段名.
Time formatting prompt: Due toCreatedTimeandUpdatedTimeIt is a timestamp, you need to usestampToDateThe filter to convert it to a readable date format, such as{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}.
Practical Scenarios and Examples
To help you understand betterarchiveListActual application, we