As an experienced website operations expert, I know that every data indicator contains profound insights into the performance of website content and user behavior.In AnQiCMS (AnQiCMS) such an efficient and practical content management system, the flexible invocation and display of core data such as "document views" are indispensable abilities for content operators.This not only helps us understand the popularity of the content intuitively, but is also an important basis for optimizing content strategies and improving user experience.
AnQi CMS, with its high-performance architecture developed in Go language, excels in data processing and template rendering, making it convenient and efficient to call various data.Today, let's delve into how to elegantly call and display the number of views of an article in Anqi CMS template.
The 'Views' in AnQi CMS: data value and convenient call
Under the context of website operation, document views are not just a simple number; they represent the interest of users in the content and are an important indicator of the attractiveness of the content.High traffic usually means that the content has stronger dissemination and user stickiness.
- Enhance the credibility of the content:Users tend to read content that has been viewed by more people, forming a herd effect.
- Guide users to discover popular content:Help visitors quickly identify popular articles or products on the website.
- Inspire content creators:Intuitive data feedback can inspire the creativity of the content team.
AnQi CMS knows these operational needs and therefore, from the beginning of its design, it has included views (Views) as an intrinsic attribute of content models such as articles, products, and more, making it extremely simple to call and display through its powerful template tag system.
Core feature analysis: How to call document page views?
In AnQi CMS, document view calls are mainly achieved through two scenarios: one is to display the detailed view of a single document, and the other is to display the view of a list of multiple articles.
One, display the browsing volume of a single document on the document details page
When a visitor clicks to enter a detailed page of a document, we usually want to see the specific number of times this article has been read. AnQi CMS provides for thisarchiveDetailTags, allowing you to easily obtain detailed information about the current document, including views.
archiveDetailThe design of tags is very flexible. When not specifiedidortokenIn the case of the parameter, it will automatically recognize the current page's document. You need toname="Views"parameter to explicitly tell the system that you want to retrieve the browsing volume data of the document.
Let's look at an example code that shows how to display the document title, publish time, and view count on the document detail page:
<article class="document-detail">
{# 获取并显示文档标题 #}
<h1 class="document-title">{% archiveDetail with name="Title" %}</h1>
<div class="document-meta">
{# 获取并显示文档发布时间,使用stampToDate标签进行格式化 #}
<span>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
{# 获取并显示文档浏览量 #}
<span>浏览量:{% archiveDetail with name="Views" %}次</span>
</div>
<div class="document-content">
{# 显示文档内容,并安全解析HTML #}
{%- archiveDetail articleContent with name="Content" %}
{{articleContent|safe}}
</div>
</article>
In the code above,{% archiveDetail with name="Views" %}This line will directly output the number of views of the current document. We can add units such as 'times' or 'readings' after the number to make it more readable. It is important to note,archive.CreatedTimeAssumingarchiveIt is a globally available current document object, itsCreatedTimeis a timestamp that needs to be formatted with astampToDatetag. If the object is not available, you can also usearchivethe same object.{% archiveDetail with name="CreatedTime" %}Get the timestamp and then format it.
Second, display the number of views for multiple articles on the document list page.
On the homepage, category list page, Tag list page, or search results page, we often need to display an overview of multiple articles, including the number of views for each article, so that users can quickly filter the content they are interested in. AnQi CMS'sarchiveListandtagDataListTags are a powerful tool for handling such scenarios.
When you arearchiveListortagDataListusing tags insideforWhen looping through a list of articles, the data of each article will be assigned to a loop variable (for example, in the following example it isarticle). At this point, you can directly access the variable'sViewsProperties to get page views.
The following code demonstrates how to display the title, description, and page views of each article on the article list page:
<div class="article-list">
{# 使用archiveList标签获取文章列表,并指定为分页模式,每页显示10条 #}
{% archiveList articles with type="page" limit="10" %}
{% for article in articles %}
<div class="article-item">
<a href="{{ article.Link }}" class="article-link">
<h3 class="article-title">{{ article.Title }}</h3>
<p class="article-description">{{ article.Description }}</p>
<div class="article-meta">
{# 显示文章发布时间 #}
<span>发布时间:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
{# 直接访问循环变量的Views属性显示浏览量 #}
<span>浏览量:{{ article.Views }}次</span>
</div>
</a>
</div>
{% empty %}
<p class="no-content">暂无更多文章。</p>
{% endfor %}
{% endarchiveList %}
{# 如果是分页列表,别忘了调用分页标签 #}
<div class="pagination-controls">
{% pagination pages with show="5" %}
{# 这里省略分页的具体HTML结构,但确保它被正确调用 #}
...
{% endpagination %}
</div>
</div>
In this example,{{ article.Views }}Directly from the loop.articleThe object retrieved the current page views.type="page"Parameter indicationarchiveListGenerate pagination data, andpaginationThe tag is responsible for rendering the complete