In website operation, we often need to quickly extract certain specific information from the article list, such as the homepage may need to display the latest article titles, or in some special modules, we need to obtain the title of the first or last entry in the article list.AnQiCMS (AnQiCMS) has a flexible template engine and rich filter functions, making it easy to meet these requirements.
AnqiCMS's template system draws on the syntax of mainstream template engines such as Django, allowing developers and operators to process data and control page logic through concise and intuitive tags and filters. Filters are an important part of template variable processing, enabling the transformation, formatting, or extraction of specific information from variables, and their syntax is usually expressed as{{ 变量 | 过滤器名称: 参数 }}.
Core Tools:firstandlastFilter
In the many filters provided by AnqiCMS,firstandlastthe filter is a powerful assistant when processing list data.
firstFilter: As the name implies,firstThe filter extracts the first element from a sequence (such as a string, array, or list).If applied to a string, it will return the first character of the string; if applied to an array or a list of articles, it will return the first complete object in the list.lastFilter: WithfirstFilter relatively,lastThe filter is used to extract the last element from the sequence.Similarly, when applied to a string, it returns the last character, and when applied to an array or a list of articles, it returns the last complete object in the list.
These two filters greatly simplify our work in locating and retrieving data from both ends of the list, without the need to manually traverse the entire list.
Get the list of articles:archiveListtags
To applyfirstandlastFilter, first you need to have a list of articles as the operation object. In AnqiCMS,archiveListThe tag is a core tool for retrieving article lists. It can flexibly query articles based on various conditions such as category ID, module ID, sorting method, and display quantity.
For example, we can obtain the latest 10 articles in the following way and store the results in a list namedarticles:
{% archiveList articles with type="list" limit="10" order="id desc" %}
{# 列表内容通常在这里循环显示,但为了获取首尾文章,我们只需获取列表本身 #}
{% endarchiveList %}
Here,type="list"This means to get a list without pagination,limit="10"Limited the number of articles to 10 articles,order="id desc"to ensure that we are getting the latest articles.
Practical Exercise: Quickly Get the Title of the First and Last Articles
There is a list of articlesarticlesAfter the variablefirstandlastFilter to get the required information.
Step 1: Make sure the article list exists.
Before trying to get the articles in the list, it is best to check first.articlesVariable is empty to avoid template error when there is no content in the list.
{% if articles %}
{# 文章列表存在,可以继续操作 #}
{% else %}
<p>当前没有任何文章。</p>
{% endif %}
Step 2: ApplyfirstandlastFilter and get the title
IfarticlesThere is content in the list,articles|firstIt will return the first article object in the list,articles|lastThen return the last article object. The article object usually containsTitle(Title),Link(Link),Description(description) and other properties. We just need to use the dot operator.) to access these properties.
For example, to get the title of the first article:
{% set firstArticle = articles|first %}
{% if firstArticle %}
<h3>最新文章:<a href="{{ firstArticle.Link }}">{{ firstArticle.Title }}</a></h3>
{% endif %}
Similarly, to get the title of the last article:
{% set lastArticle = articles|last %}
{% if lastArticle %}
<h3>最旧文章:<a href="{{ lastArticle.Link }}">{{ lastArticle.Title }}</a></h3>
{% endif %}
Complete code example
Integrate the above steps, and we can quickly retrieve and display the first and last titles of the article list in the template:
{# 1. 获取最新发布的 10 篇文章列表 #}
{% archiveList articles with type="list" limit="10" order="id desc" %}
{% endarchiveList %}
{# 2. 检查文章列表是否存在内容 #}
{% if articles %}
{# 获取列表中的第一篇文章对象 #}
{% set firstArticle = articles|first %}
{# 获取列表中的最后一篇文章对象 #}
{% set lastArticle = articles|last %}
<div class="article-summary">
{% if firstArticle %}
<p><strong>最新文章:</strong>
<a href="{{ firstArticle.Link }}">{{ firstArticle.Title }}</a>
</p>
{% endif %}
{% if lastArticle %}
<p><strong>最早文章(此列表内):</strong>
<a href="{{ lastArticle.Link }}">{{ lastArticle.Title }}</a>
</p>
{% endif %}
</div>
{# 如果需要,这里可以继续循环显示完整的文章列表 #}
<ul class="article-list">
{% for article in articles %}
<li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>抱歉,目前没有找到任何文章。</p>
{% endif %}
Common Questions (FAQ)
Q1:Can I get other information about the article besides the title? For example, the link or thumbnail?A1:Of course you can.firstandlastThe filter returns the complete article object. This means you can access any available property of the article object using the dot operator。“.)to access any available property of the article object, for example,“{{ firstArticle.Link }}to retrieve the link,“}]{{ firstArticle.Thumb }}Used to obtain the thumbnail,{{ firstArticle.Description }}Used to get descriptions and so on. Just replace according toarchiveListthe fields listed in the tag documentationitemthe.Title.
Q2: If the article list is empty, usefirstorlastDoes the filter report an error?A2: Use the empty list directlyfirstorlastThe filter usually does not report errors directly, but the results returned will benil(empty value). If notnilcheck the value and directly access its properties (for example{{ nil.Title }}),then it may lead to template rendering errors. Therefore, it is strongly recommended to usefirstArticleorlastArticlebefore using{% if firstArticle %}or{% if lastArticle %}to make sure that the variable indeed contains a valid article object.
Q3: Can I use a loop to check if the current article is the first or last one in the article list?firstandlastFilter to determine whether the current article is the first or last one?A3: Inside the loop, for example{% for article in articles %}autoarticlesautofirstorlastautoforloopThe provided attribute, such as{% if forloop.first %}(Determine if it is the first article in the loop) and{% if forloop.last %}(Determine if it is the last article in the loop).firstandlastThe filter is more suitable for use in the loopoutsideRetrieve the first and last elements of the list independently.