As an old soldier who has been deeply engaged in website operation for many years, I know that how to efficiently obtain and utilize content data in a content management system is crucial for improving user experience and website maintenance efficiency.AnQiCMS (AnQiCMS) boasts its concise and efficient features, providing many intuitive template tags to make content operation more flexible.Today, let's delve into a very practical scenario: how to use AnQiCMS byprevArchiveLabel to obtain the previous document'sIdfield.
How to cleverly get the previous document in AnQi CMSId?
When building website content, especially for blogs, news, or product detail pages, we often need to provide users with 'Previous' and 'Next' navigation links to guide users to continue reading and optimize the internal flow. Anqi CMS provides special template tags for this, whereprevArchiveThe tag is used to obtain the information of the 'previous' document of the current document.
Get to knowprevArchiveTag
prevArchiveThe design philosophy of the tag is to simplify operations as much as possible, it can automatically identify the context of the current page without passing any parameters, and try to get the document sorted before the current document.After successfully obtaining the previous document, it will fill all the relevant data of the document into a variable you specify, making it convenient for you to call it in the template.
For example, you can enable it in the template in the following way.prevArchiveTags:
{% prevArchive prev %}
{# 在这里,您就可以使用 'prev' 变量来访问上一篇文档的数据了 #}
{% endprevArchive %}
In this code block,prevIt is the variable name specified for the data of the previous document. Once the tag executes successfully,prevthe variable will become an object containing various properties of the previous document.
Get the previous document'sIdfield
SinceprevArchiveThe tag has placed all the available data from the previous document intoprevthe variable, so getting itsIdfield becomes extremely simple. You just need to use the dot.operator, and directly accessprevVariableIdproperties.
IdIs a unique identifier for each document in the Anqing CMS system, usually an integer.It is very useful for building dynamic links, performing backend data interaction, or handling specific logic in front-end JS.
The following is a document obtained and used in a real templateIdA complete example of a field:
{% prevArchive prev %}
{% if prev %}
<div class="prev-document-navigation">
<p>上一篇:
<a href="{{ prev.Link }}" title="{{ prev.Title }}">
{{ prev.Title }}
<small>(文档ID: {{ prev.Id }})</small>
</a>
</p>
</div>
{% else %}
<div class="prev-document-navigation">
<p>上一篇:没有了</p>
</div>
{% endif %}
{% endprevArchive %}
Let's parse this code step by step:
{% prevArchive prev %}: This line of code indicates that the AnQi CMS should find the previous content of the current document and assign the found data to a variable namedprev.{% if prev %}: This is a very important judgment. It checksprevDoes the variable really have a value. If the current document is the first in the series, then there will be no previous document,prevThe variable will be empty. This judgment can avoid trying to access a non-existent field when there is no previous document, which may cause a page error.<a href="{{ prev.Link }}" ...>IfprevIf it exists, we can safely access its properties.{{ prev.Link }}The URL link of the previous document will be output,{{ prev.Title }}The title of the document will be output.<small>(文档ID: {{ prev.Id }})</small>: This is exactly the core we are looking for!{{ prev.Id }}The previous document will be output directly.IdField value. You can decide whether to display this on the front end based on your actual needsIdOr use it behind the scenes for other logical processing.{% else %}IfprevThe variable is empty, which means there is no previous document, and we then display a prompt such as 'No more' to ensure the integrity of the user experience.{% endif %}and{% endprevArchive %}: This is to distinguish between conditional judgments andprevArchiveThe end tag, ensuring the correctness of the template syntax structure.
By the above method, you can not only easily get the previous document,Idbut also getTitle/LinkOther commonly used fields to build a beautiful and functional page navigation.The template tag system of AnQi CMS is like this, through the intuitive variable call, it converts complex backend data processing into a simple and easy-to-understand frontend display.
Frequently Asked Questions (FAQ)
Q1:prevArchiveThe label can retrieve the current document'sId?A1: No.prevArchiveThe design purpose of the label is to obtain the information of the previous document, it only returns the data of the document immediately preceding the current document in the sorting sequence. To obtain the current document'sIdYou should directly use it in the detail page template.{{ archive.Id }}Assuming the current document variable is namedarchiveor{% archiveDetail with name="Id" %}such tags.
Q2: If my website has multiple content models or categories,prevArchivehow will the tags determine 'Previous post'?A2:prevArchiveLabels are usually located in the current document locatedthe same content model and classificationLooking for the previous document. This means that if you are currently browsing an article under the "Article Model" category of the "Technical Sharing" category,prevArchiveIt will look for the previous article adjacent to the document list of the "Article Model" under the "Technology Sharing" category. This ensures the consistency and relevance of navigation.
Q3: BesidesId,prevArchiveWhat are some common document fields that the tag can retrieve?A3:prevArchiveTags retrieved from comments.prevThe variable contains many useful fields from the previous document, but also includesIdYou can also directly access{{ prev.Title }}(Document Title),{{ prev.Link }}(Document Link),{{ prev.Description }}(Document description),{{ prev.CategoryId }}(Category ID),{{ prev.Views }}(Views),{{ prev.Logo }}(Cover main image),{{ prev.Thumb }}(Cover thumbnail) as well{{ prev.CreatedTime }}(Creation time) and so on. These fields are sufficient to meet the needs of most navigation and information display.