How to dynamically generate a link to the comment area based on `item.Id` in `archiveList`?

Calendar 👁️ 71

In Anqi CMS, in order to enhance the user experience of the website, we often hope that users can quickly find specific content on the page.For example, when you click on the "View Comments" link next to an article title on a document list page, you can directly jump to the comments section of the article detail page.This not only saves users time, but also makes interaction more direct.

Today, let's discuss how toarchiveListIn a loop, based on the unique identifier of the documentitem.IdDynamically generate a link to jump to the comment area. This process is actually very intuitive, mainly involving the coordination of two parts of the template: the document list page and the document detail page.

How anchor links work

Before we delve into the specific implementation, let's briefly review the basics of anchor links. The core of anchor links lies inURLthe one following#Symbol, followed by a certainHTMLelementID. When the browser loads a page with anchor links, it will automatically scroll to thisIDThe position of the element corresponds. Therefore, the key is to ensure that there is one in the comment section.UniqueandDynamically generatedofID.

Step one: Prepare an anchor for the comment section on the document detail page.

Firstly, we need to add a unique jumpable comment section in the details page of each document (usuallyarchive/detail.htmlor a custom document details template)ID.

The document detail page of AnQi CMS automatically encapsulates all information of the current document when loading, which includes the uniquearchivevariable of the document.ID, that isarchive.IdWe can take advantage of this to dynamically generate aID.

Assuming your comment section is loaded bycommentListtags, you can place it in adivIn the container, and give this container a dynamicIDFor examplecomments-{{ archive.Id }}.

You can arrange the comment section like this in the template file:

{# archive/detail.html 或其他文档详情模板 #}

<div id="comments-{{ archive.Id }}">
    <h3>用户评论</h3>
    {% commentList comments with archiveId=archive.Id type="page" limit="10" %}
        {# 这里是评论内容的循环展示 #}
        {% for item in comments %}
            <div>
                <span>{{ item.UserName }}</span>
                <span>于 {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }} 评论:</span>
                <p>{{ item.Content }}</p>
            </div>
        {% endfor %}
    {% endcommentList %}
</div>

{# 如果您还想添加评论表单,也可以放置在此ID范围内,或者紧随其后 #}
<form method="post" action="/comment/publish">
    <input type="hidden" name="archive_id" value="{{ archive.Id }}">
    {# 其他表单字段,例如用户名、评论内容等 #}
    <button type="submit">提交评论</button>
</form>

In this example,id="comments-{{ archive.Id }}"It will be based on the current document'sIDAn unique one will be generated automaticallyID. For example, if the documentIDIs123Then the comment area will beIDit will becomments-123. This is a very reliable dynamicIDgeneration method.

Second step: inarchiveListDynamically generated jump link

Next, we go back to the document list page, which is usingarchiveListThe tag loops to display articles. Here, we need to generate a link to the comment section of the corresponding detail page.

InarchiveListin the loop, each document item's data is assigned toitemvariable. Thisitemvariable also contains the uniqueID(item.Id)and the complete link to the article(item.Link)

Now we can useitem.LinkAnchor point defined earlier with usIDCombined together, it forms a complete jump link:

{# 列表页面,例如 index/index.html 或 {模型table}/list.html #}

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <div class="article-item">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>{{ item.Description|truncatechars:150 }}</p>
            <div>
                <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                <span>浏览量:{{ item.Views }}</span>
                {# 这里的链接会跳转到对应文章详情页的评论区 #}
                <a href="{{ item.Link }}#comments-{{ item.Id }}" class="view-comments">查看评论 ({{ item.CommentCount }})</a>
            </div>
        </div>
    {% empty %}
        <p>目前没有文章。</p>
    {% endfor %}
{% endarchiveList %}

{# 如果是分页列表,别忘了添加分页导航 #}
{% pagination pages with show="5" %}
    {# 分页代码 #}
{% endpagination %}

Here, href="{{ item.Link }}#comments-{{ item.Id }}"This line is crucial.item.LinkThe detail page URL of the current list item document will be automatically provided, and then we proceed through#comments-{{ item.Id }}To set the anchor point of the comments sectionIDAttach it to the end of the URL. This way, when the user clicks on this link, it will directly jump to the corresponding article detail page and automatically locate to the comment section of that page.

Integration and optimization

By combining the above two steps, we have achieved inarchiveListDynamically generated jump link to the comment area. This method is simple and efficient, fully utilizing the flexibility of the Anqi CMS template engine.

You can further optimize the user experience, for example:

  • Smooth scrolling effect:By introducing some JavaScript code on the page, it can be achieved that when clicking on the anchor link, the page does not jump to the target location instantly, but scrolls smoothly to it, which appears more friendly.
  • Comment count display:Utilizeitem.CommentCountVariable, dynamically displays the comment count of the current article in the link text, to attract users to click.
  • Style beautification:Add appropriate CSS styles to the comment section and jump links to make them more prominent and beautiful.

The Anqi CMS, with its powerful content model and flexible template tags, makes this kind of personalized function easily accessible.I hope this guide can help you better utilize AnQi CMS, providing a more convenient and friendly browsing experience for your website visitors.


Frequently Asked Questions (FAQ)

1. Why did I not jump to the comment area after clicking the link after following the steps?

This is usually because of the anchor point of the comment areaIDWith you atarchiveListThe generated link does not match completely. Please check the following points:

  • Comments section of the detail pagedivHave you setid="comments-{{ archive.Id }}"?EnsureidThe properties are complete and correct, especiallycomments-Prefix and{{ archive.Id }}Section.
  • archiveListLink inhrefWhether the attribute is used correctlyitem.Linkand#comments-{{ item.Id }}?Check the anchor part as well#comments-{{ item.Id }}Whether it matches the detail page'sIDremain consistent.
  • Check the browser developer tools:Open the browser developer tools (usually press F12), check the comments section of the detail pagedivactualIDwhat it is, as well as the list page linkhrefIs the anchor part consistent with it.

2. What should I do if my article detail page does not have a comment section or the comment section structure is different.

If your article detail page currently does not have a comment section, you need to first refer to the Anqi CMS document incommentListthe tag description, inarchive/detail.htmlAdd and configure the comment feature. Ensure that the comment feature is displayed normally before following the first step to add one for it.id="comments-{{ archive.Id }}"The container. If you have other custom structures in your comment section, just make sure to set a dynamic for the outermost comment containerIDfor exampleid="my-custom-comments-{{ archive.Id }}"),thenarchiveListuse the corresponding anchor (for example#my-custom-comments-{{ item.Id }})to do so.

Can I dynamically link to other parts of the article detail page?

Of course, this method is not limited to the comment section. As long as anyHTMLelement has a uniqueIDyou can bearchiveListconstruct the corresponding anchor link. For example, if you want to link to the "product parameters" section of the article, and this section is in the detail page, then inid="product-specs-{{ archive.Id }}"ofdiv, you can bearchiveListcan be createda href="{{ item.Link }}#product-specs-{{ item.Id }}"such links. The key is to keep the anchor in the list page linksIDwith the target element of theIDdynamic and consistent.

Related articles

How to combine other filters with the `add` filter to ensure safe concatenation and prevent XSS attacks when processing user input?

In AnQi CMS, managing website content involves entering a variety of information regularly.Whether it is the main text of the article, user comments, or form submissions, this user-generated content injects vitality into the website while also bringing potential security risks, the most common and severe of which is XSS (Cross-site Scripting attack).This article will discuss how the `add` filter works in processing user input content, how it协同acts with other security filters to build a strong defense line, effectively preventing XSS attacks.

2025-11-07

How to use the `add` filter to dynamically add a uniform prefix or suffix to the single page titles in the `pageList`?

In Anqi CMS, content management is not limited to the backend's add, delete, modify, and query, but also manifests in the flexibility and diversity of front-end display.In the face of common single-page websites (such as "About Us", "Contact Us", etc.), sometimes we hope that they can be unified with specific identifiers when displayed in lists or navigation, such as If you modify the background title one by one, it is not only inefficient but also lacks uniformity and maintainability.At this time, utilizing the powerful template engine function of AnqiCMS, with the clever filter

2025-11-07

How does the `add` filter handle the addition/pasting operation with boolean values `true` and `false` as well as numbers or strings?

In Anqi CMS template development, we often need to process and display data.Among them, the `add` filter is a very practical tool that allows us to add numbers and concatenate strings.However, when the boolean values `true` and `false` are involved in these operations, their behavior may puzzle some users who are new to the subject.Today, let's take a deep dive into how the `add` filter handles addition or concatenation operations with boolean values, numbers, or strings.

2025-11-07

How to dynamically generate JavaScript code snippets in AnQiCMS templates and use the `add` filter to concatenate variables?

In website operations, we often need to make the page more intelligent and interactive.AnQiCMS as an efficient and flexible content management system not only provides powerful content organization and display capabilities, but also allows us to ingeniously integrate dynamic logic in templates, such as by generating JavaScript code snippets to achieve more complex functions.AnQiCMS's template engine syntax is similar to Django, which provides great convenience for us to dynamically process data.It is developed based on the Go language, runs fast

2025-11-07

`add` filter combined with `default` filter to set default values for variables that may be empty before concatenation?

In the daily operation of AnQi CMS, we often need to concatenate different field content to form a complete information, such as generating a complete address or combining an attractive product title.However, a common challenge with dynamic content is that some variables may be empty.If concatenated directly, the blank content not only affects the appearance but may also lead to missing information or disordered format.Fortunately, AnQiCMS's powerful Django template engine grammar provides us with a flexible solution

2025-11-07

How to dynamically display

Display the record number at the bottom of the website, which is not only a requirement to comply with relevant laws and regulations, but also an important factor in enhancing the professionalism and user trust of the website.For users of AnQiCMS, dynamically displaying the filing number through the built-in powerful functions of the system is a very convenient operation, eliminating the need to frequently modify the code and greatly improving the maintenance efficiency of the website. ### Set the record number content in the background First, we need to set your website record number in the AnQi CMS background management interface. This is the basis of all dynamic display content, ensuring the accuracy of information

2025-11-07

Can the `add` filter be used to concatenate CSS style strings to implement dynamic inline styles?

AnQiCMS with its flexible and powerful template engine provides great convenience to users, allowing content presentation to be highly customized according to actual needs.In daily template development, we often encounter scenarios where we need to dynamically adjust the page display based on backend data, such as changing the title color based on the article reading volume, or displaying different background styles based on user permissions.At this point, we might wonder whether the built-in `add` filter of AnQiCMS templates can be used to concatenate CSS style strings to achieve these dynamic inline style controls

2025-11-07

How to dynamically add language parameters in the `add` filter of the `languages` tag that returns multilingual site links?

Under the flexible multi-language support of AnQiCMS, providing content to users worldwide has become very convenient.The system built-in `languages` tag can easily list all the language versions supported by your website and their corresponding links.However, in some scenarios, we may not only want to provide language switching functionality, but also dynamically add additional parameters to these multilingual links, such as for marketing tracking, A/B testing, or loading personalized content in specific languages.

2025-11-07