When managing content in AnQi CMS, we often encounter the need to process some text information stored in a specific format.For example, the keywords of an article are usually stored in a field in the form of a comma-separated string, such as “AnQi CMS, content operation, template creation”.When we want to highlight the most important keyword on the website front-end, such as in an article list or detail page, or to get the last keyword as a hint, we need to split these strings and extract the specific elements.
幸运的是,the AnQi CMS built-in template engine provides powerful filter functions, which can easily meet such needs. Among them,splitThe filter can split a string into a collection of data according to a specified delimiter, which in programming is usually called an array or slice, andfirstandlastThe filter can help us quickly obtain the first or last element of this data set.
Core function:splitFilter
First, we need to convert the original string content into a manipulable dataset. At this point,splitThe filter comes into play. Its function is to split a long string into a collection of short strings based on the delimiter you provide.
Assuming we have a variablearchive.KeywordsIts value is"安企CMS,内容运营,模板制作"And we hope to split by comma,Split it. We can first usesetlabels to declare a temporary variable, andsplitstore the result of the filter for later use:
{% set keyword_string = archive.Keywords %}
{% set keyword_array = keyword_string|split:"," %}
Now,keyword_arraythis temporary variable becomes a container that includes["安企CMS", "内容运营", "模板制作"]such a data set.
Get the first element: clever use.firstFilter
When we got the cut data set.keyword_arrayAfter, it is very simple to get the first element. Anqi CMS template engine provides an intuitivefirstA filter. This filter can be directly applied to a dataset, returning its first member.
Carrying on from the previous example, if we want to display the article's "main keyword", which is the first element in the data set, we can write it like this:
主要关键词:{{ keyword_array|first }}
After running, the page will display: "Main keywords: AnQi CMS."
Get the last element: Use flexibly.lastFilter
withfirstThe filter is analogous, if we need to get the last element of the data set, we can uselastThe filter is also simple and clear, able to directly return the last member of the data set.
If we want to display the 'other keywords' of the article, and assume that the last keyword can represent some secondary information, we can get it in this way:
其他关键词:{{ keyword_array|last }}
After running, the page will display: "Other keywords: template creation."
Comprehensive example: application in practice.
Imagine on your article detail page, you want to display several main keywords below the article title, and randomly select a tag as a recommendation in the footer "related tags" section.
{% set keywords_str = archive.Keywords %} {# 假设 archive.Keywords 为 "SEO,安企CMS,建站,网站优化" #}
{% set keyword_list = keywords_str|split:"," %}
<div class="article-meta">
<h1>{{ archive.Title }}</h1>
<p>主要聚焦:<span class="highlight-keyword">{{ keyword_list|first }}</span></p>
{# 其他元信息如发布时间、作者等 #}
</div>
{# 页面其他内容 #}
<div class="footer-related-tags">
<h3>猜你喜欢</h3>
<p>探索更多关于 <a href="/tags/{{ keyword_list|last }}">{{ keyword_list|last }}</a> 的内容。</p>
</div>
By such a combination application, we can not only flexibly structure the string data, but also accurately extract and display the key information according to different display requirements, making the template content more dynamic and rich.
Frequently Asked Questions (FAQ)
If my string is empty, or the delimiter does not exist in the string,
splitHow will the filter perform?firstorlastcan it still work?- when the string is empty (for example,
""),splitThe filter will return an array containing an empty string, i.e.[""]. At this point,|firstand|lastwill return an empty string"". - If the delimiter does not exist in the string (for example
"安企CMS"Use,delimiter),splitThe filter will return an array containing the original string itself, that is["安企CMS"]. At this point,|firstand|lastwill all return the original string"安企CMS". - Therefore, even in these special cases,
firstandlastThe filter can still provide predictable results without causing program errors.
- when the string is empty (for example,
firstandlastThe filter is not only used to processsplitthe array after, can it also be used for other data types?- Yes,
firstandlastThe filter can also be used to processsplitAfter an array (or slice), it can also be directly applied to a string. When applied to a string,firstit returns the first character of the string,lastwhile it returns the last character. For example,"你好世界"|firstwill return"你"while"你好世界"|lastwill return"界"This makes them very flexible in handling various types of data.
- Yes,
How do I get a specific element from the middle of an array after slicing, rather than the first or last one?
- When you need to access a specific element in an array, you can use the index directly. In the Anqi CMS template engine, array indices start from
0Start. For example, to get the second element, you can write like this:{{ keyword_array[1] }}. If you need a more complex cut, such as getting from the Nth to the Mth element,sliceThe filter would be a better choice, usually combinedfirstorforFor example, to use it cyclically{{ keyword_array|slice:"1:3" }}It will get a new array composed of elements from index 1 to index 2 (i.e., the 2nd and 3rd elements).
- When you need to access a specific element in an array, you can use the index directly. In the Anqi CMS template engine, array indices start from