It is crucial to provide users with a smooth reading experience in website content operation.When a reader is immersed in an excellent article, if they can be smoothly guided to discover more related content, it will undoubtedly greatly enhance user stickiness.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, with its efficient, flexible, and scalable features, providing a powerful set of tools for content operators.Today, let's delve into a very practical template tag in AnQiCMS -prevArchiveand focus on how to use it to display the classification of the previous documentCategoryId.
AnQiCMS Template Tag Overview
AnQiCMS uses syntax similar to the Django template engine, making template creation intuitive and powerful. In AnQiCMS template files, we mainly operate data and control logic through two forms: double braces{{ 变量 }}Used to output variable content, while single curly braces and percent signs{% 标签 %}It is used to perform conditional judgments, loop traversal, and call various built-in function tags.These well-designed tags give us great flexibility to customize every corner of the website.
prevArchiveWhat is the tag?
In the document detail page, users usually expect to see 'Previous' and 'Next' navigation links to facilitate continuous browsing of content.prevArchiveThe label is born for this. It will intelligently identify and provide the data of the previous document in the sorting sequence of the current document.When you want to display the title, link, and even the category information of the previous document on the current document page,prevArchiveTags come in handy.
It is worth noting that,prevArchiveThe tag itself is designed simply, it does not accept any parameters, which means you cannot directly filter the specific attributes of the 'previous' document through this tag. It will only return the current document'sDirect to the previous articleThe complete data object of the document. However, this does not hinder us from obtaining the information we need, because the returned data object already contains the information we are interested inCategoryId.
How to get the category ID of the previous document
To display the category of the previous documentCategoryIdwe first need to use it on the document detail pageprevArchiveUse a tag to get the data of the previous document. This tag is often used in conjunction with oneifJudgment to ensure that the previous document actually exists, thereby avoiding errors when there is no previous content.
WhenprevArchiveSuccessfully retrieved the previous document, it will assign its data to a variable we define (for exampleprev)。ThisprevThe variable includes all the available fields of the previous document, includingCategoryId.
Here is a basic code example that demonstrates how to retrieve and display the title, link, and category of the previous document in a templateCategoryId:
{# 使用 prevArchive 标签获取上一篇文档的数据,并将其赋值给变量 prev #}
{% prevArchive prev %}
{% if prev %} {# 检查是否存在上一篇文档 #}
<div class="prev-article-navigation">
<p>上一篇文档:<a href="{{ prev.Link }}">{{ prev.Title }}</a></p>
{# 直接通过 prev 变量获取上一篇文档的 CategoryId #}
<p>所属分类ID:<span>{{ prev.CategoryId }}</span></p>
</div>
{% else %}
<div class="prev-article-navigation">
<p>已经是第一篇了,没有上一篇文档。</p>
</div>
{% endif %}
{% endprevArchive %}
In this code block,{% prevArchive prev %}Attempt to retrieve the previous document, if successful,prevThe variable will contain all the properties of the document. Then, we proceed to{{ prev.CategoryId }}This can directly output the digital ID of the category of the previous document. This method is intuitive and efficient, and can meet the needs of most display category ID requirements.
Deep understanding: CombinecategoryDetailTag
Just getting the category ID may not be enough. Often, we may want to display the category name, even the category link, so that users can directly click to return to the list page of the category of the previous document. At this time, AnQiCMS has another powerful tag -categoryDetailIt came in handy.categoryDetailThe tag can obtain the detailed information of the category according to the category ID.
We can combineprevArchiveObtainedprev.CategoryIdas a parameter tocategoryDetailThe tag, thus obtaining the complete information of the category to which the previous document belongs, such as the category title (Title) And category link (Link).
{# 使用 prevArchive 标签获取上一篇文档的数据 #}
{% prevArchive prev %}
{% if prev %} {# 检查是否存在上一篇文档 #}
<div class="prev-article-full-info">
<p>上一篇文档:<a href="{{ prev.Link }}">{{ prev.Title }}</a></p>
{# 使用 categoryDetail 标签,传入上一篇文档的 CategoryId 来获取分类详情 #}
{% categoryDetail prevCategory with name="Title" id=prev.CategoryId %}
<p>所属分类:
{# 再次使用 categoryDetail 获取分类的链接,并结合分类标题展示 #}
<a href="{% categoryDetail with name='Link' id=prev.CategoryId %}">{{ prevCategory }}</a>
</p>
<p>分类ID(同样可以获取):<span>{{ prev.CategoryId }}</span></p>
</div>
{% else %}
<div class="prev-article-full-info">
<p>已经是第一篇了,没有上一篇文档的分类信息。</p>
</div>
{% endif %}
{% endprevArchive %}
In this enhanced code,{% categoryDetail prevCategory with name="Title" id=prev.CategoryId %}Of the previous document,CategoryIdpass tocategoryDetailAnd assign its category title to,prevCategoryVariable. In this way, we can display more friendly and guiding category names and links on the page, greatly enriching the user experience.
Application scenarios and practical skills
It is very useful in many scenarios to get the category ID and details of the previous document:
- Dynamic breadcrumb navigation:Although AnQiCMS provides
breadcrumbTags, but in some custom layouts, you may want to include a dynamic link such as “Return to [the category of the previous document]” in the navigation. - Recommended related content:At the bottom of the document, in addition to displaying the usual "Previous/Next" links, you can also add an entry to view more articles of the [category name of the previous document], guiding users to delve deeper into similar content.
- Personalized Sidebar:On the sidebar of the document detail page, dynamically display some personalized recommendations or navigation modules based on the current document and its adjacent documents' categories.
Practical Tips:
- Always perform existence checks:While using
prevBefore a variable is used, make sure to{% if prev %}Check. If the current document is already the first in the category,prevArchiveNo data will be returned, directly accessprev.Linkorprev.CategoryIdIt will cause the template to render incorrectly. - Understand the sorting logic of “Previous article”:
prevArchiveThe previous one is retrieved based on the system's default sorting rules (usually in reverse order of publication time or ID).If you have special requirements for the sorting of your documents, make sure that the backend settings are consistent with the front-end display logic. - Pay attention to the performance of the template:Although combined
categoryDetailProvided more comprehensive information, but this means increased database queries.In most cases, this performance overhead can be ignored, but in high concurrency or extreme optimization scenarios, it can be weighed according to actual needs.
ByprevArchiveLabel, AnQiCMS makes the content navigation and user experience of the website more flexible and powerful.Mastering these basic and practical tag usages will undoubtedly make your website operation twice as effective.
Frequently Asked Questions (FAQ)
1.prevArchiveDoes the tag support filtering, such as only getting the previous document of the same model or category?
prevArchiveThe tag itself does not accept any parameters for filtering. It always returns the current document based on the default sorting rule.Direct to the previous articleDocument. If you need to get the previous one based on specific conditions (such as categories), you need to go through first.archiveListLabel to get a list of documents sorted and filtered according to your needs, then locate the current document in the list and get the previous document.
2. If the current document is the first in the category,prevArchivewhat will be returned?
If the current document does not have a previous one,prevArchivewithin the tag,prevthe variable will benil(null). This is why it is always recommended to use the structure in the template,{% if prev %}...{% else %}...{% endif %}to checkprevDoes the variable exist to avoid template rendering errors due to attempting to access the attributes of an empty object.
3. BesidesCategoryId, can I still getprevArchiveWhat information can be obtained from the previous document tag?
prevArchive`