I am very happy to analyze the comment management and template configuration skills of Anqi CMS for you in depth.In website content operation, comments are an important bridge connecting users to content.AnQiCMS as an efficient content management system naturally also provides powerful comment functions and flexible template tags to help us accurately control the display of comments.
Today, we will focus on a common and core requirement:How to configurecommentListTags to display only specific articles (archiveId) comments?Let's unravel this mystery step by step.
The comment to the content: It is not just interaction, but also value
Before delving into technical details, let's first consider the significance of comments for website operation.User comments not only provide valuable feedback, enhance the interaction of the content, but also bring long-tail keywords for SEO and enrich the page information.Our CMS is deeply proficient in this field, therefore it has taken精细化 management and display of comments into account from the beginning of system design.
Get to knowcommentListTag: The magic wand of comment display
Anqi CMS provides a powerful and flexible template tag system for developers, among whichcommentListIt is the core tool for obtaining and displaying article comments.You can use it in the template file to render a comment list, such as below the article details page or a dedicated comment aggregation page.
You need to wrap this tag with{% commentList ... %}and{% endcommentList %}and specify a variable name for the comment data you get, such ascomments. Its basic structure is usually as follows:
{% commentList comments with ... %}
{# 循环展示评论数据 #}
{% for item in comments %}
{# 评论的HTML结构 #}
{% endfor %}
{% endcommentList %}
Accurate positioning:archiveIdThe mystery of parameters
Now, let's solve the core problem: how tocommentListonly display comments for specific articles? The answer lies incommentLista key parameter of the tag:archiveId.
archiveIdThe parameter's function is very intuitive, it clearly tells AnQiCMS which specific article's comment data you want to retrieve. When you arecommentListSet in the labelarchiveIdAt that time, the system will match the corresponding comment in the database according to this ID and return it.
As given in the document.commentListThe example of tag usage clearly illustrates this:
{% commentList comments with itemType="archive" itemId="1" type="page" %}...{% endcommentList %}
HereitemId="1"It actually refers toarchiveId="1"This indicates retrieving comments for the article with ID 1.itemType="archive"It further emphasizes that this is a comment for the article type. In practice, if you have already specifiedarchiveId,itemTypeusually can be omitted, the system will automatically identify.
dynamically obtainarchiveId: The practice of the article detail page **
In most cases, you will display the comments of the article on the article detail page.At this moment, the article ID on the current page is dynamic.archiveobject (in the article detail page, the data of the current article will usually be loaded intoarchivethe variable).
You can use it directly.{{ archive.Id }}To get the current article ID and then assign it tocommentListlabel'sarchiveIdthe parameter. This way, no matter which article detail page the user visits,commentListit can intelligently identify and display the corresponding article comments.
For example, in yourarchive/detail.htmlIn a template such as (or other templates used for article details),archiveVariables usually contain all the data of the current article. At this point, you can configure it like thiscommentList:
{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
{# ... 评论列表的渲染逻辑 ... #}
{% endcommentList %}
IfarchiveThe variable is unavailable for some reason, or you may need to load comments at specific locations on non-article detail pages (such as displaying comments for a popular article on the homepage), you can also explicitly througharchiveDetailTag to get the ID of a specified article, for example:
{# 假设您要获取ID为5的文章的评论 #}
{% archiveDetail specificArchive with name="Id" id="5" %}
{% commentList comments with archiveId=specificArchive type="page" limit="10" %}
{# ... 评论列表的渲染逻辑 ... #}
{% endcommentList %}
{% endarchiveDetail %}
But on the article detail page, use it directlyarchive.IdIt is usually a more concise and efficient way.
Complete example: Build a fully functional comment section
Here is a template code example that combines obtainingarchiveId, displaying comment lists, comment status judgment, and basic pagination. This code is usually placed in your article detail template (such astemplate/您的模板名/archive/detail.htmlBelow the article content.
"twig
{# 假设您正在文章详情页,且当前文章对象为archive` #