In website content operation, providing users with a smooth reading experience is crucial.When readers immerse themselves in a wonderful article, if they can smoothly guide them 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, which provides a powerful toolkit for content operators with its efficient, flexible, and easy-to-expand features.prevArchive,and focus on how to use it to display the category of the previous documentCategoryId.

AnQiCMS Template Tag Overview

AnQiCMS uses a syntax similar to Django template engine, making template creation intuitive and powerful. In AnQiCMS template files, we mainly operate data and control logic through two forms: double curly braces{{ 变量 }}Used to output variable content, and single curly braces and percentage signs{% 标签 %}Then it is used to perform conditional judgments, loop traversals, and call various built-in function tags.It is these well-designed labels that give us great flexibility to customize every corner of the website.

prevArchiveWhat is a label?

In the document detail page, users usually expect to see 'Previous' and 'Next' navigation links to facilitate continuous browsing of content.prevArchiveTags are born for this.It will intelligently recognize and provide the data of the previous document in the sorting sequence of the current document.prevArchiveTags are very useful.

It is worth noting that,prevArchiveThe label itself is designed simply, it does not accept any parameters, which means you cannot directly filter specific properties of the "previous" document through this label. It will only return the properties of the current document.直接上篇英语文档的完整数据对象。不过,这并不妨碍我们获取到所需的信息,因为返回的数据对象中已经包含了我们感兴趣的信息CategoryId.

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 pageprevArchiveLabel to retrieve data from the previous document. This label is usually used with anotherifcheck to ensure that the previous document actually exists, thus avoiding errors when there is no previous content.

WhenprevArchiveSuccessfully obtained the previous document, it will assign its data to a variable we define (for exampleprev)。ThisprevThe variable includes all available fields of the previous document, includingCategoryId.

The following is a basic code example that demonstrates how to retrieve and display the title, link, and category of the previous document in a template.CategoryId:

{# 使用 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,{% prevArchive prev %}Attempts to retrieve the previous document, if successful,prevThe variable will contain all the properties of the document. Subsequently,{{ prev.CategoryId }}This method 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.

Deep understanding: combinedcategoryDetailtags

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 category list page of the document they are reading. At this time, AnQiCMS has another powerful tag——categoryDetailIt came in handy.categoryDetailLabels can obtain detailed information about the category based on the category ID.

We can imagine thatprevArchiveobtainedprev.CategoryIdas a parameter tocategoryDetailLabels, 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 %}Refer to the previous document's.CategoryIdPass tocategoryDetailAnd assign its category title toprevCategoryVariables. 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 classification ID and details of the previous document:

  1. Dynamic breadcrumb navigation:Although AnQiCMS provides:breadcrumbLabels, but in some custom layouts, you may want to include a dynamic link such as “Return to [category of the previous document]” in navigation.
  2. 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 into similar content.
  3. Customized Sidebar:On the document detail page sidebar, dynamically display some personalized recommendations or navigation modules based on the current document and its adjacent documents' categories.

Practical Tips:

  • Always perform existence check:.When usingprevEnsure through before.{% if prev %}Check. If the current document is the first in the category,prevArchiveno data will be returned, and directly access.prev.Linkorprev.CategoryIdIt will cause the template rendering error.
  • Understand the sorting logic of “Previous article”: prevArchiveThe “previous” post is retrieved based on the system's default sorting rule (usually in reverse chronological order of publication or ID).If your document sorting has special requirements, make sure the backend settings are consistent with the frontend display logic.
  • Focus on template performance:Although combinedcategoryDetailProvided more 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.

PassprevArchive标签,AnQiCMS makes the website's content navigation and user experience more flexible and powerful.Mastering these basic and practical tag usages will undoubtedly make your website operation twice as effective.


Common Questions (FAQ)

1.prevArchiveIs the label support filtering, such as only getting the previous document of the same model or category?

prevArchiveThe label itself does not accept any parameters for filtering. It always returns the current document sorted by default rules.直接上篇英语Document. If you need to get the previous one based on specific conditions (such as category), you need to firstarchiveListLabel retrieves a document list sorted and filtered to your requirements, and then locates the current document in this list to get the previous document.

2. If the current document is the first in the category,prevArchivewhat will it return?

If the current document does not have a previous one,prevArchiveWithin the tags,prevthe variable will benil(null). That's why it is always recommended to use{% if prev %}...{% else %}...{% endif %}the structure in the template to checkprevThe variable exists to avoid template rendering errors caused by attempting to access attributes of an empty object.

3. BesidesCategoryId, can I also get fromprevArchiveWhat information can I get from the previous document's tag?

prevArchivetag returns the `