When using AnQi CMS to manage website content, we often need to search and display related document lists based on specific keywords.It is crucial to master the method of keyword filtering, whether it is to create an in-site search results page or to display content closely related to a theme on a specific topic page.AnQi CMS provides flexible and powerful template tag functions, making this task intuitive and efficient.
Basics of keyword filtering:archiveListlabel'sqParameter
AnQi CMS through its corearchiveListThe tag is used to handle the retrieval of document lists. This tag not only allows filtering based on common conditions such as categories and models, but also provides a very practicalqThe parameter is specifically used for title search based on keywords.
When you need to display documents on the website front-end based on the user's input keywords,qThe parameter comes into play. For example, if the user enters "Anqi CMS tutorial" in the search box, we can pass this keyword toarchiveListThe tag will automatically match all documents containing "AnQi CMS tutorial" and list them.
A basic application scenario is on the search results page. The backend receives the keyword submitted by the user through the search form, and then uses it asqThe value of the parameter, called in the templatearchiveListTags:
<form method="get" action="/search">
<div>
<input type="text" name="q" placeholder="请输入搜索关键词" value="{{urlParams.q}}">
<button type="submit">搜索</button>
</div>
</form>
{% archiveList archives with type="page" q=urlParams.q limit="10" %}
{% for item in archives %}
<li>
<a href="{{item.Link}}">
<h5>{{item.Title}}</h5>
<p>{{item.Description}}</p>
<div>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</div>
</a>
</li>
{% empty %}
<li>
抱歉,没有找到符合“{{urlParams.q}}”关键词的文档。
</li>
{% endfor %}
{% endarchiveList %}
{% pagination pages with show="5" %}
<!-- 分页导航代码 -->
{% endpagination %}
In the above example,urlParams.qWill dynamically obtain the query parameter value namedqand then pass it as a keyword toarchiveList.type="page"The parameter indicates that we want to paginate the results, combiningpaginationTags can facilitate the generation of pagination navigation.
Using the content model to customize fields for advanced filtering
In addition to keyword filtering of document titles, the strong point of Anqi CMS is that it supports custom content models.This means we can add dedicated custom fields for different content types (such as articles, products, cases, etc.).These custom fields can not only enrich the content details but also serve as filtering conditions to achieve more accurate keyword filtering.
These custom fields defined in the document's "Other Parameters", such as "House Type", "Area", "Price Range", etc., can all be used as filter conditions on the front end. The key to achieving this advanced filtering is:
- Define filterable fields in the background content modelIn the content model settings, check the 'Filterable' option for fields that need to be filtered.
- Pass the filter value through the URL query parametersIn the front end, we can add custom field names and values in the form of URL query parameters (for example
?房屋类型=住宅&地区=上海) to the URL of the document list page. - Use
archiveFiltersLabel generation filter options: To facilitate user selection of filtering conditions, you can usearchiveFiltersTags dynamically generate these custom field filter options on the page.This label will automatically generate a filter list with correct links and selected states based on the content model you define and the current URL filtering status.
For example, if we have a real estate model and define custom fields such as 'House Type' and 'Area', we can organize it on the list page:
<div>
<h3>房源筛选</h3>
{% archiveFilters filters with moduleId="1" allText="不限" %}
{% for item in filters %}
<ul>
<li>{{item.Name}}: </li> {# 例如:房屋类型 #}
{% for val in item.Items %}
<li class="{% if val.IsCurrent %}active{% endif %}">
<a href="{{val.Link}}">{{val.Label}}</a>
</li>
{% endfor %}
</ul>
{% endfor %}
{% endarchiveFilters %}
</div>
{# 文档列表会根据URL中的筛选参数自动调整 #}
{% archiveList archives with type="page" moduleId="1" limit="10" %}
{% for item in archives %}
<li>
<a href="{{item.Link}}">
<h5>{{item.Title}}</h5>
<p>{{item.Description}}</p>
</a>
</li>
{% endfor %}
{% empty %}
<li>
没有找到符合条件的房源。
</li>
{% endarchiveList %}
{% pagination pages with show="5" %}
<!-- 分页导航 -->
{% endpagination %}
In this example,archiveFiltersTags will generate filters such as "House Type", "Area", etc., each option will generate a link, clicking on which will update the URL parameters and reload the document list,archiveListThe tag will display the matching documents based on all the filter parameters passed in the URL (including keywordsqand custom fields).
Key points of template integration and display
Please note the following points when integrating these filtering features into your website template:
- ConsistencyEnsure that the
nameproperties such asname="q") andarchiveListin the labelqparameters correspond. - User ExperienceAfter the user filters or searches, keep the filtering conditions visible on the page and clearly indicate the current selected filtering status, which can significantly improve the user experience.
archiveFilterslabel'sIsCurrentProperty provides good support in this aspect. - Page processing: The document list after filtering usually also needs to be displayed in pages, so don't forget to match
pagination. - Empty result prompt: Use.
forrepeatedlyemptyBlock handles the case of not finding a matching document gracefully, giving the user a friendly prompt.
By the above method, AnQi CMS makes it very flexible and powerful to filter and display specific document lists based on keywords, whether it is simple on-site search or complex faceted navigation (multidimensional filtering), it can be easily realized.
Frequently Asked Questions (FAQ)
1. I want to create a dedicated search page where users can enter keywords and see the results. How should I implement it?You can create a page namedsearch/index.htmlTo implement a template file. In this template, place a search form,actionproperty points to/search,inputfield'snameset the attribute toq. Then, usearchiveListLabel, andqThe parameter value is set tourlParams.qto get the document list corresponding to the user's input keywords. At the same time, combinepaginationTags can enable pagination of search results.
2. Besides keyword search, can I also filter by 'region' and 'house type' of the document?Of course. Anqi CMS'sarchiveListThe tag supports receiving multiple URL query parameters for filtering. If you have defined custom fields such as 'region' and 'house type' in your content model and these fields are marked as filterable, then you just need to include these parameters in the URL, for example/search?q=公寓&地区=上海&房屋类型=住宅.archiveListThe document will be filtered based on all matching conditions. You can also usearchiveFilterstags to generate these multi-dimensional filtering options on the front end.
3. What will the page display if no documents are found after searching or filtering?InarchiveListwithin the tag,forIn the loop, you can add a{% empty %}block. When no documents match the filtering criteria, forThe loop will executeemptyContent within the block, rather than iterating through the document. This way, you can place a friendly prompt here, such as 'Sorry, no documents matching the criteria were found.'