幸运的是,安企CMS内置的模板引擎提供了强大的过滤器功能,可以轻松应对这类需求。其中,splitThe filter can split a string into a dataset according to the specified delimiter (usually referred to as an array or slice in programming), andfirstandlastThe filter can help us quickly obtain the first or last element of this data set.

Core Function:splitFilter

Firstly, we need to convert the original string content into a manipulable dataset.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.

Suppose we have a variablearchive.Keywordswith the value"安企CMS,内容运营,模板制作"and we hope to split by comma,Separate it. We can first usesetlabels to declare a temporary variable, andsplitstore the results of the filter for subsequent use:

{% set keyword_string = archive.Keywords %}
{% set keyword_array = keyword_string|split:"," %}

Now,keyword_arraythis temporary variable becomes a container that includes["安企CMS", "内容运营", "模板制作"]This data set.

Obtain the first element: Clever usefirstFilter

When we get the sliced data setkeyword_arrayAfter, it is very simple to get the first element. The Anqi CMS template engine provides an intuitivefirstFilter. This filter can directly act on a dataset and return its first member.

承接上面的例子,如果我们想显示文章的“主要关键词”,即数据集合中的第一个元素,可以这样来写:

主要关键词:{{ keyword_array|first }}

After running, the page will display: "Main keywords: Anqi CMS".

Get the last element: Use flexibly.lastFilter

WithfirstThe filter performs a similar function, if we need to obtain the last element of the data set, we can uselastFilter. It is simple and clear, and can 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 do it like this:

其他关键词:{{ keyword_array|last }}

After running, the page will display: "Other keywords: Template creation".

Comprehensive example: Application in practice.

Imagine in your article detail page, you want to display the main keywords below the article title, and randomly select a tag as a recommendation in the "related tags" section of the footer.

{% 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>

Through such a combination of applications, 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.

Common Questions (FAQ)

  1. If my string is empty, or the delimiter does not exist in the string,splitHow will the filter perform?firstorlastCan it still work normally?

    • When the string is empty (for example),""){splitThe filter will return an array containing a single empty string,[""]At this point,|firstand|lastWould return an empty string"".
    • If the delimiter is not present in the string (for example"安企CMS"Use,separated by),splitThe filter would return an array containing the original string itself, i.e.["安企CMS"]. At this point,|firstand|lastWould return the original string"安企CMS".
    • Therefore, even in these special cases,firstandlastthe filter can still provide predictable results without causing program errors.
  2. firstandlastthe filter is not only for processingsplitCan the array after be used for other data types?

    • Yes,firstandlastThe filter can also be used to processsplitThe array after (or slice), they can also be directly applied to strings. When applied to strings,firstIt will return the first character of the string,lastor the last character. For example,"你好世界"|firstit will return"你"while"你好世界"|lastit will return"界". This makes them very flexible when handling various types of data.
  3. If I need to get a specific element from the middle of an array after slicing, rather than the first or last one, what should I do?

    • When you need to access a specific element in an array, you can use index directly. In the template engine of AnQi CMS, array indices start from0Start. For example, to get the second element, you can write it like this:{{ keyword_array[1] }}. If you need more complex slicing, for example, to get elements from the Nth to the Mth,sliceFilter will be a better choice, usually combinedfirstorforReuse, for example{{ keyword_array|slice:"1:3" }}Will get a new array composed of elements from index 1 to index 2 (i.e., the 2nd and 3rd elements).