As an experienced website operations expert, I know that efficiently obtaining and utilizing content IDs in a content management system is the key to dynamic functions and optimizing user experience.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, which fully considers the needs of developers and operators from the beginning of its design and provides a simple yet powerful template tag system.Today, let's delve into how to easily obtain the ID of the current article on the document detail page of Anqi CMS.
Understanding the context of the Anqi CMS 'Content Perception': Document detail page
In AnQi CMS, when you visit a document detail page, the system does not simply render a static page.It has a 'content-aware' ability, that is, it can identify which specific article is being displayed on the current page, and automatically load and pass all related data of this article to the template.This means that in the template environment of the document detail page, we can directly access various properties of the current article, including its unique identifier—the article ID.
This design philosophy greatly simplifies the development of templates, as we do not need to go through complex queries or backend programming, and can directly manipulate the data of the current article in the front-end template, thereby realizing various dynamic content display and interactive functions.
The core secret: direct accessarchive.IdVariable
The most direct and commonly used way to get the current article ID in the document detail page template of Anqi CMS is to access it through predefinedarchivevariables.IdProperty. HerearchiveIt can be understood as a container automatically prepared by the system when rendering the current document detail page, representing the 'current article object'.
When you are on the detail page of a specific article, for examplehttp://yourdomain.com/article/123.html(where 123 is the article ID), in the corresponding document detail template file (for examplearticle/detail.htmlIn it, you can simply use the following code to immediately obtain the ID of the current article:
{{ archive.Id }}
This line of code will directly output the numeric ID of the current article. This method is powerful due to its simplicity and intuitiveness.No matter how you configure your pseudo-static URL, as long as the current page is an article detail page,archive.IdAll of them will accurately point to the ID of the article you are browsing.
Flexible applicationarchiveDetailTag to get the article ID
Accessed directlyarchive.IdVariables, AnQi CMS also provides more powerful functionsarchiveDetailTemplate tag, it can also help us get the ID of the current article. Although it is more concise to get the ID of the current article directly, but understanding{{ archive.Id }}it is more concise, but understandingarchiveDetailThe way tags work, allowing you to effortlessly obtain *other* article IDs, or to get specific article IDs on non-document detail pages.
archiveDetailThe design of the label is to obtain detailed data of the specified document. When you use it on the document detail page, if it is omittedidortokenThe parameter will default to getting the document information of the current page. Therefore, to get the current article ID, you can use it like this:
{% archiveDetail with name="Id" %}
The effect of this line of code is{{ archive.Id }}It is equivalent, it explicitly tells the system: 'Please get the ID field of the current document and output it.'
Then, when would it be more倾向于使用archiveDetailWhat about the tagsGenerally, when you need to display the ID of a specific article on a non-detail page (such as the homepage, list page) or when you need to obtain the ID of another article other than the current article ID on the detail page,archiveDetailThe advantage becomes prominent. For example, if you want to display the title of an article with ID 520 next to the current article detail page, you can write it like this:
{% archiveDetail otherArticleTitle with name="Title" id="520" %}
<p>我关注的另一篇文章:{{ otherArticleTitle }}</p>
This labeling method provides greater flexibility and control, allowing you to accurately pull data according to your needs.
Application scenario: Make the article ID more valuable.
ID of the article is not only for display, but more importantly, it is used to drive various dynamic functions and optimizations.
Build a comment system:The comment system usually needs to know which article the user is commenting on. In the comment submission form, we can embed a hidden field to pass the current article ID:
<input type="hidden" name="archive_id" value="{{ archive.Id }}">This way, the backend can accurately associate comments with articles, such as
tag-/anqiapi-other/158.htmlas shown in the document,archive_idThe parameter is used for this purpose.To implement relevant content recommendation:Of Security CMS
archiveListLabel combinationtype="related"The parameter can intelligently recommend content related to the current article.In some custom scenarios, if you need to perform complex association logic based on the current article ID (for example, by requesting the backend API through JavaScript), the article ID is an indispensable parameter.Conditional content display:Sometimes, you may need to display specific content blocks, ads, or prompts based on the article ID. For example, only articles with ID 10 will display a special announcement:
{% if archive.Id == 10 %} <div class="special-announcement">🎉 恭喜!这是本站的里程碑文章!</div> {% endif %}Integrate third-party services:Many third-party services, such as social sharing buttons, external analytics tools, and customized widgets, all need to know the content ID of the current page in order to accurately track data or bind functions.By using JavaScript, you can easily retrieve the article ID from the page and pass it to these services.
Custom template file:The AnQi CMS supports the default custom template name format for the documentation:
{模型table}/{文档id}.htmlThis means you can create a dedicated template for articles with a specific ID, and the article ID is the key that the system recognizes and applies the template to.
Tip: Ensure the robustness of the code.
Althougharchive.IdIt is almost foolproof on the document detail page, but as a rigorous operator, we will always consider the edge cases. If you use the general template (which may be used on list pages or custom pages)archive.IdAnd if this page is not the article detail page, thenarchiveThe object may be empty or not containIdfield, which may cause the template to render incorrectly.
To avoid this situation, we can accessarchive.IdBefore, make a simple judgment:
{% if archive and archive.Id %}
<!-- 只有当 archive 对象存在且包含 Id 字段时才显示 -->
当前文章ID是:{{ archive.Id }}
{% else %}
<!-- 否则显示一个提示或不显示任何内容 -->
这不是一篇文章详情页。
{% endif %}
This judgment can make your template code more robust and adaptable, even when used in unexpected contexts, it can handle it gracefully and avoid page errors.
Summary
To get the current article ID on the document detail page of AnQi CMS is a very direct and basic operation. Whether it is through a concise{{ archive.Id }}variable, or through a more feature-rich{% archiveDetail with name="Id" %}Labels, the system provides intuitive and efficient solutions. Mastering this skill not only allows you to better understand the template mechanism of Anqi CMS, but also lays a solid foundation for you to meet various dynamic and personalized display needs in content operation, thus enhancing the interactivity and user experience of the website.AnQi CMS is committed to making content management simpler and more efficient, and these flexible template tags are the best embodiment of this concept.
Frequently Asked Questions (FAQ)
Q1: BesidesIdCan I go through directlyarchiveWhat information can the variable get from the articles?
A1: In the document detail page,archiveThe variable is an object containing all the fields of the current article. You can go through directly{{ archive.Title }}(Article Title),{{ archive.Description }}(article description),{{ archive.Link }}(article link),{{ archive.CreatedTime }}(Create timestamp, need to usestampToDateto access its properties in the format. Specific field names available can be referred to intag-/anqiapi-archive/142.htmlthe "name parameter available fields" section of the document.
Q2: If I am not on the document detail page, such as on the homepage or list page, how should I get the ID of an article?
A2: In a non-document detail page,archiveVariables usually do not represent the article on the current page. You need to usearchiveListTag to iterate over the article list and get the ID of each article in the loop. For example, to get the ID of the latest 10 articles:
{% archiveList articles with type="list" limit="10" %}
{% for item in articles %}
<p>文章标题:{{ item.Title }},ID:{{ item.Id }}</p>
{% endfor %}
{% endarchiveList %}
You can inarchiveListsetcategoryId/moduleIdto filter the articles you need.
Q3: The article ID I receive is a number, do I need to process it as a string in the URL?
A3: Under normal circumstances, you just{{ archive.Id }}Embed it in the URL path, for example<a href="/article/{{ archive.Id }}.html">. The AnQi CMS template engine will automatically convert it to a string. If your URL is in query parameter form and contains special characters (such as?id={{ archive.Id }}¶m=...), then the ID itself generally does not need to be escaped, but the entire URL parameter string may need to beurlencodeA filter is used to ensure integrity, but the ID itself is usually pure numbers and can be used directly.