In website operation, the number of article views and comments is an important indicator for measuring the popularity of content and attracting user interaction.AnQiCMS as a flexible content management system, provides a convenient way to display these data on the website front-end.By using simple template tags, even without deep programming knowledge, these features can be easily implemented.

AnQiCMS's template system borrows the syntax of Django template engine, using double curly braces{{变量}}To output variable content, use single curly braces and the percentage sign{% 标签 %}Call the feature tag. Understand these basic rules and you can customize the display effect of the website more efficiently.

Display the number of views and comments on the article list page

When we need to display the reading count and comment count for each article in an article list (such as article recommendations on the homepage, category article lists, etc.), we can usearchiveListLabel to get article data, and display them one by one through a loop.

archiveListThe label can retrieve a collection of articles under specified conditions. While iterating through this collection, the data of each article will be represented by a variable (usually aitem), and we can directly access it.itemThe property of the object is used to obtain the reading volume and comment count.

The following is an example of code that displays the list of articles and the corresponding reading volume and comment count.

{# 假设我们正在获取文章模型ID为1,显示数量为10的文章列表 #}
{% archiveList articles with moduleId="1" type="list" limit="10" %}
    {% for article in articles %}
    <div class="article-card">
        <h3><a href="{{article.Link}}">{{article.Title}}</a></h3>
        <p class="article-meta">
            <span>发布日期:{{stampToDate(article.CreatedTime, "2006-01-02")}}</span>
            <span>阅读量:{{article.Views}}</span>
            <span>评论数:{{article.CommentCount}}</span>
        </p>
        <p>{{article.Description}}</p>
        <a href="{{article.Link}}" class="read-more">查看详情</a>
    </div>
    {% empty %}
    <p>暂时没有文章可供显示。</p>
    {% endfor %}
{% endarchiveList %}

In the above code:

  • {% archiveList articles ... %}Called the article list tag and assigned the obtained article collection.articlesa variable.
  • {% for article in articles %}Loop through each article.
  • {{article.Views}}Directly output the reading volume of the current article.
  • {{article.CommentCount}}Then output the comment count of the current article.

Through this method, you can dynamically display the reading volume and comment number of each article in any article list area of the website.

Display the reading volume and comment number on the article detail page

When the user clicks to enter the specific article detail page, it is usually displayed below or at the end of the article title, including the detailed data of the article, such as its reading volume and total number of comments. In the AnQiCMS article detail page, all the information of the current article will be automatically loaded into a namedarchiveThe global variable of. Therefore, we can directly accessarchivethe property of the object to get the required data.

The following is an example code snippet showing the reading volume and comment count on an article detail page.

<article class="article-detail">
    <h1 class="article-title">{{archive.Title}}</h1>
    <div class="article-info">
        <span>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</span>
        <span class="views-count">阅读量:{{archive.Views}}</span>
        <span class="comment-count">评论数量:{{archive.CommentCount}}</span>
    </div>
    <div class="article-content">
        {{archive.Content|safe}}
    </div>
    {# 这里可以继续添加评论列表等内容 #}
</article>

In this code block:

  • {{archive.Title}}The article title was output.
  • {{archive.Views}}The reading volume of the current article was directly retrieved and displayed.
  • {{archive.CommentCount}}Directly fetches and displays the current number of comments for the article.

This method is very intuitive and can be easily integrated into the article detail page without the need for additional tag calls.

About the Supplement of Comment List

Although the above mainly discusses the total number of comments displayed, if you need to further display the specific list of comment contents, AnQiCMS also providescommentListLabel. This label can get the comment details of the specified article, and is usually used with pagination labels.paginationUsed together.

For example, you can call it like this in the comment area of the article detail page:

<div class="comments-section">
    <h3>用户评论 (共 {{archive.CommentCount}} 条)</h3>
    {% commentList comments with archiveId=archive.Id type="page" limit="10" %}
        {% for comment in comments %}
        <div class="single-comment">
            <p><strong>{{comment.UserName}}</strong> 于 {{stampToDate(comment.CreatedTime, "2006-01-02 15:04")}} 评论:</p>
            <p>{{comment.Content}}</p>
        </div>
        {% empty %}
        <p>目前还没有评论,快来发表您的看法吧!</p>
        {% endfor %}

        {# 评论分页 #}
        {% pagination pages with show="5" %}
            {# 分页链接的渲染逻辑 #}
        {% endpagination %}
    {% endcommentList %}
</div>

Herearchive.CommentCountUsed to display the total number of comments.commentListTags are responsible for fetching and cyclically displaying the specific content of each comment.

Practical tips

In actual operation, please pay special attention to the case sensitivity of AnQiCMS template tags and field names, for exampleViewsandviewsIt is different.After modifying the template file and uploading it, if the front-end page does not update immediately, you can try to clear the browser cache or the system cache of AnQiCMS backend to ensure that the new template file takes effect.

These tag calling methods provided by AnQiCMS make the dynamic display of website content simple and efficient, which helps operation personnel better manage and present website content, enhancing user experience and website interactivity.


Common Questions and Answers (FAQ)

Q1: Why doesn't the front-end page display the reading volume or comment count after I add the code according to the tutorial?

A1: This situation may occur, you may need to check several aspects. First, please ensure that the labels and field names in the template file are spelled correctly, AnQiCMS template is case-sensitive, for example,ViewsandviewsWould be recognized as different fields.Next, check if the comment function of the article is enabled in the AnQiCMS backend.Sometimes, after updating the template file, the browser or system cache may not be refreshed in time. You can try clearing the browser cache, or manually clean the system cache in the "Update Cache" feature of AnQiCMS.

Q2: AnQiCMS 的文章阅读量是如何统计的?有没有办法自定义统计逻辑?

A2: AnQiCMS will automatically count the page views of articles.The system design includes traffic statistics and crawler monitoring functions, and the reading volume data is usually recorded through internal mechanisms each time a user accesses an article page.Currently, there is no direct mention of the method for custom reading statistics logic in the official documentation, which is usually an integrated core feature of the system.If you need more complex statistical analysis, consider combining the traffic statistics provided by AnQiCMS for analysis, or developing a second version to achieve specific statistical requirements.

Q3: I want to display the reading volume in the format of "1.2k" or "1k+", does AnQiCMS template support this?

A3: AnQiCMS template tags directly output the original number of article views, for example, '1200'.If you need to display in the format of "1.2k" or "1 thousand+", the template itself does not provide a built-in formatting filter to achieve this effect.{% if ... %}Tag) to manually concatenate strings based on the size of the number, but this will make the template logic relatively complex.