As an experienced website operations expert, I know that efficiently obtaining and utilizing content IDs is the key to implementing dynamic functions and optimizing user experience in a content management system.AutoCMS (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 very beginning and provides a simple yet powerful template tag system.Today, let's delve into how to easily obtain the current article ID on the document detail page of AnQi CMS.

Understanding the context of the document detail page in AnQi CMS's 'Content Awareness'

In the Auto CMS, when you visit a document detail page, the system does not simply render a static page.It possesses a 'content-aware' ability, that is, it can identify which specific article the current page is displaying 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, allowing us to directly manipulate the data of the current article in the front-end template without the need for complex queries or backend programming, thereby realizing various dynamic content display and interactive functions.

核心秘诀:直接访问archive.Idvariable

在安企CMS的文档详情页模板中,获取当前文章ID的最直接、最常用的方式,就是通过预定义的archive变量来访问其IdProperty. Here, it refers toarchiveYou can understand it as a container that the system automatically prepares for you while 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.html)In this case, you can simply use the following code to immediately obtain the current article ID:

{{ archive.Id }}

This code will directly output the current article's numeric ID.This method is powerful because of its simplicity and intuitiveness.archive.Idwill accurately point to the article ID being viewed.

Use flexiblyarchiveDetailTag to get article ID

Accessed directlyarchive.IdThe CMS also provides more powerful functionsarchiveDetailTemplate tag, which also helps us get the current article ID. Although getting the current article ID directly is simpler, but understanding{{ archive.Id }}is more straightforward, but understandingarchiveDetailThe way the tag works, allowing you to handle*other*article IDs with ease, or to obtain specific article IDs on non-document detail pages.

archiveDetailThe original intention of the label design is to obtain detailed data of specified documents. When you use it on the document detail page, if it is omittedidortokenThe parameter, it will default to obtain the document information of the current page. Therefore, to obtain 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 retrieve the ID field of the current document and output it.'

When would it be more倾向于使用archiveDetailtags?通常,当你在一个非详情页(比如首页、列表页)需要展示某一篇特定文章的ID,或者在详情页需要获取除当前文章ID之外的另一篇文章ID时,archiveDetailThe advantage of auto 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 tagging method provides greater flexibility and control, allowing you to accurately retrieve data according to your needs.

Actual application scenarios: Make the article ID more valuable.

Retrieve the article ID is not only for display, but more importantly, it is used to drive various dynamic functions and optimizations.

  1. 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 }}">
    

    So, the backend can accurately associate comments with articles, liketag-/anqiapi-other/158.htmlas shown in the document,archive_idThe parameter is used for this purpose.

  2. To implement relevant content recommendation:Anqi CMS'sarchiveListTag combinationtype="related"Parameters can intelligently recommend content related to the current article.And in some custom scenarios, if you need to perform more complex association logic based on the current article ID (for example, by making a JavaScript request to the backend API), the article ID is an indispensable parameter.

  3. Conditional content display:Sometimes, you may need to display specific content blocks, advertisements, or prompts based on the article ID. For example, only articles with ID 10 display a special announcement:

    {% if archive.Id == 10 %}
        <div class="special-announcement">🎉 恭喜!这是本站的里程碑文章!</div>
    {% endif %}
    
  4. Integrate third-party services:Many third-party services, such as social sharing buttons, external analytics tools, and customized widgets, all require the content ID of the current page to perform precise data tracking or feature binding.With JavaScript, you can easily retrieve the article ID from the page and pass it to these services.

  5. Custom template file:The default custom template name format supported by AQ CMS is:{模型table}/{文档id}.htmlThis means you can create a dedicated template for a specific ID article, and the article ID is the key for the system to recognize and apply the template.

Tip: Ensure the robustness of the code.

Althougharchive.IdIt is almost infallible on the document detail page, but as a serious operator, we always consider the edge cases. If you use it in a general template (which may be used on list pages or custom pages),archive.Id, and this page is not the article detail pagearchivethe object may be empty or do not containIdfield, which may cause template rendering errors.

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. It can also elegantly handle the context where it is used unexpectedly, avoiding page errors.

Summary

In the document detail page of AnQi CMS, obtaining the current article ID 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" %}Tags, the system provides intuitive and efficient solutions.Master this skill, not only can it help you deeply understand the template mechanism of Anqi CMS, but it can also lay a solid foundation for you to achieve various dynamic and personalized display needs in content operation, thereby enhancing the interactivity and user experience of the website.Auto CMS is dedicated to making content management simpler and more efficient, and these flexible template tags are the best embodiment of this concept.


Common Questions (FAQ)

Q1: BesidesId,I can directly accessarchivewhich articles of the variable can get information? A1:In the document details page,archiveThe variable is an object containing all fields of the current article. You can directly access{{ archive.Title }}(article title),{{ archive.Description }}(article description),{{ archive.Link }}(article link),{{ archive.CreatedTime }}(create timestamp, need to usestampToDateFormat)等形式访问其属性。具体可用的字段名称,可以参考tag-/anqiapi-archive/142.htmlDocument section "Available fields for name parameter".

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 non-document detail pages,archiveVariables usually do not represent the article on the current page. You need to usearchiveListLabel to traverse 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 do this onarchiveListSet incategoryId/moduleIdto filter the articles you need.

Q3: I get an article ID that is a number. Do I need to handle it as a string in the URL? A3:In most cases, you can directly {{ archive.Id }}Embed in the URL path, for example<a href="/article/{{ archive.Id }}.html">. The template engine of AnQi CMS will automatically convert it to a string. If your URL is in query parameter form and contains special characters (such as?id={{ archive.Id }}&param=...),then the ID itself usually does not need to be escaped, but the entire URL parameter string may need tourlencodeFilter to ensure integrity, but the ID itself is usually pure numbers and can be used directly.