As an experienced website operations expert, I know that the flexible use of templates in content management systems is the key to enhancing the dynamism of the website and user experience.AnQiCMS (AnQiCMS) provides us with powerful content display capabilities with its efficient architecture based on the Go language and Django-style template syntax.archiveListIn the loop, cleverly judge whether the current document belongs to the category of the current page.
The foundation of flexible content presentation: understandingarchiveListWith category judgment
In daily content operations, we often encounter such needs: on a category page, we may need to display articles under the category, as well as some of the latest and most popular articles in the sidebar or footer, which may come from different categories.If it can be clearly identified which articles are in the current category and which are not, it can greatly improve the user experience and the logic of content organization.
Of Security CMSarchiveListThe tag is a powerful tool for obtaining and displaying document lists.It can flexibly filter out the required documents based on various conditions (such as model ID, category ID, recommendation attributes, sorting method, etc.)When we talk about 'the category of the current page', it usually refers to the category list page that the user is browsing, or the category to which a document detail page belongs.
To implement this judgment, we need two key pieces of information: first is the category ID represented by the current page; second is thearchiveListIn the loop, the classification ID of each document itself. The template tag system of Anqi CMS perfectly supports us in obtaining this information.
Step 1: Get the classification ID of the current page.
No matter where you are on the category list page or on the detail page of a document, as long as the page is associated with a category, Anqi CMS can pass throughcategoryDetailEasily obtain the detailed information of the current category, including its ID.
We can retrieve the ID of the current category and store it in a variable like this:
{# 假设我们当前正处于一个分类页面,或者一个文章详情页,且该文章归属于某个分类。 #}
{# categoryDetail 标签会自动识别当前页面的分类信息。我们从中提取其ID。 #}
{% categoryDetail currentCategory with name="Id" %}
{% set currentCategoryId = currentCategory %}
{# 此时,变量 currentCategoryId 就存储了当前页面的分类ID,比如:10、25等。 #}
Here, we usecategoryDetailThe label obtained the ID of the current category and assigned it tocurrentCategoryIdthis variable. The advantage of doing this is,currentCategoryIdNow is a common variable in the template context, which is convenient for us to compare in subsequent loops.
Second step: Traverse the document list and compare the classification ID.
Next, we usearchiveListTag to get the document list we want to display.To demonstrate the judgment logic, we can deliberately obtain a wider range of document lists, such as the latest 10 articles on the website, which may come from different categories.
{# 假设我们现在需要展示网站上最新的10篇文章,无论它们属于哪个分类。 #}
{# 我们使用 moduleId="1"(通常代表文章模型),不指定 categoryId 参数,以获取一个跨分类的近期文章列表。 #}
{% archiveList allRecentArchives with moduleId="1" type="list" limit="10" %}
<div class="article-listing">
{% for archiveItem in allRecentArchives %}
<div class="article-card">
<a href="{{ archiveItem.Link }}">
<h4>{{ archiveItem.Title }}</h4>
</a>
{# 核心判断逻辑:检查当前循环中的文档是否属于当前页面的分类 #}
{% if archiveItem.CategoryId == currentCategoryId %}
<span style="color: green; font-weight: bold;">[当前分类精选]</span>
{% else %}
<span style="color: gray;">[其他推荐]</span>
{% endif %}
<p>{{ archiveItem.Description }}</p>
<small>发布于:{{ stampToDate(archiveItem.CreatedTime, "2006-01-02") }}</small>
</div>
{% if not forloop.Last %}<hr>{% endif %} {# 除了最后一项,每项后面加分隔线 #}
{% empty %}
<p>很抱歉,当前暂无相关的文章内容。</p>
{% endfor %}
</div>
{% endarchiveList %}
In this code block,archiveItemIsarchiveListThe representative of each article in the loop. ByarchiveItem.CategoryIdWe can directly access the category ID of the current loop article. Then, we use{% if archiveItem.CategoryId == currentCategoryId %}This condition judgment, compares the article's category ID with the one we previously obtainedcurrentCategoryId. If they are equal, it means that this article belongs to the category of the current page.
In this way, we can display different styles or information in the same document list, based on the relevance of the article category to the current page, such as adding a special corner mark, changing the title color, or providing more specific prompt information.
Actual application scenarios and value
This judgment ability is not just a simple technical implementation, but also the key to improving the logicality of website content organization and user experience:
- Enhance user guidance:In a list containing mixed content, clearly identifying the 'current category' helps users quickly find articles most relevant to their interests, reducing the burden of information overload.
- Personalized content recommendation:You can highlight articles consistent with the current category of the user's browsing path in the related recommendations to enhance the click-through rate and conversion rate of the content.
- Flexible UI Design:This judgment brings greater freedom to template design.You can design a unique card style, background color, or icon for the current category of articles to make it visually more attractive and easily noticeable at a glance.
- SEO Optimization Aid:By displaying targeted content, it can further strengthen the theme of the page, helping search engines better understand the content of the page, and potentially improve the ranking of relevant keywords.
The Anqi CMS template system, with its concise and powerful tags, makes these complex logics easy to implement.Mastering this technique of classification judgment in a loop allows you to control the content display more finely, providing website users with a smoother and more personalized browsing experience.
Frequently Asked Questions (FAQ)
1.archiveListlabel'scategoryIdParameters and within loopsitem.CategoryIdWhat is the difference?
archiveListlabel'scategoryIdParameters are used inQuery phaseFilter documents, tell the system 'I only need these specific categories of documents'. For example,categoryId="1"Only documents under the category with ID 1 will be retrieved. Anditem.CategoryIdisarchiveList inside the loopfor eachitem(i.e., each document) has a property that indicates which category the document actually belongs to.
This article introduces the technique that you can letarchiveListGet a broader list of documents (for example, without specifyingcategoryIdor specifying multiplecategoryId), then use it inside the loopitem.CategoryIdTo determine the specific attribution of each document, thereby achieving a more refined display logic.
2. In addition to determining whether a document belongs to the current category, I can also useitemWhat conditions can the object perform?
archiveListin the loopitemThe object contains rich information about the document, you can make various conditions judgments:
- Recommended properties (
Flag):{% if item.Flag contains "h" %}(Determine if it is a headline article). - Views (
Views):{% if item.Views > 1000 %}(Determine if it is a hot article). *