In the template development of AnQi CMS, in order to display content more efficiently and flexibly, we often use various filters to process the data.These filters are like a variety of small tools in a toolbox, which can help us quickly format, truncate, or extract data.firstandlastThey help us easily get the first or last element of a string or array.
1. IntroductionfirstandlastFilter
Imagine that you are displaying a product list, where each product may have a set of images, but you only want to show the first image as the cover on the list page; or, you have a comment list and you want to highlight the latest comment.Or maybe, you need to extract the first or last character from a piece of text for some special design.firstandlastThe filter can come into play. Its design purpose is to simplify this 'take the head and tail' data operation, making the template code more concise and understandable.
2.firstFilter: Quickly get the first element
firstThe function of the filter is self-evident: it can get and return the first element from a string or an array.
Processing strings:If you apply to a string
firstFilter, it will return the first character of the string.It is worth mentioning that the template engine of Anqi CMS supports Chinese well. If you are processing a Chinese string, it will correctly return the first Chinese character instead of bytes.Example:
{{ "AnQiCMS"|first }} {# 显示结果: A #} {{ "你好世界"|first }} {# 显示结果: 你 #}Process an array or list:When you have an array or list containing multiple elements,
firstThe filter will return the first member of this collection. This is very convenient for scenarios that require displaying the first item of a list.Example:Assume
complex.commentsIt is an array containing comment objects.{{ complex.comments|first }} {# 显示结果: 返回数组中的第一个评论对象,通常是其值表示 #}If it is a simple string array:
{% set tags = ["CMS", "模板", "运营"] %} {{ tags|first }} {# 显示结果: CMS #}
When the original string or array is empty,firstThe filter will not return any content, which means you do not need to worry about template errors due to empty values.
3.lastFilter: Easily get the last element
WithfirstCorresponding filter,lastThe filter is used to get the last element of a string or an array. It works in a similar way tofirstThe filter works in the opposite direction.
Processing strings:For strings,
lastThe filter will return its last character. Similarly, for Chinese string, it can also accurately return the last Chinese character.Example:
{{ "AnQiCMS"|last }} {# 显示结果: S #} {{ "你好世界"|last }} {# 显示结果: 界 #}Process an array or list:When applied to an array or list,
lastThe filter will return the last member of the collection. This is very useful when it is necessary to display the latest or the last record.Example:Assume
complex.commentsIt is also an array containing comment objects.{{ complex.comments|last }} {# 显示结果: 返回数组中的最后一个评论对象 #}If it is a simple string array:
{% set tags = ["CMS", "模板", "运营"] %} {{ tags|last }} {# 显示结果: 运营 #}
WithfirstFilter as well, if the original string or array is empty,lastthe filter will not return any content.
4. Summary
firstandlastThe filter is one of the two small but powerful tools in the Safe CMS template engine.They can help us extract the information we need most from strings or arrays with极高的效率和清晰度——whether it is the first character/element at the beginning or the last one at the end.In daily template development, being proficient in using them can make our code more concise, reduce unnecessary loops and logical judgments, thereby improving development efficiency and the readability of the template.
Common Questions (FAQ)
Q1:firstandlastWhat does the filter return when processing an array of complex objects (such as structures)?
A1:When you have an array containing complex objects (such as article objects, product objects, etc. structures),firstorlastThe filter will return the first or last complete complex object instance in this array.It does not automatically extract a specific attribute of the object (such as the title or ID) but returns the object itself.{{ obj|first.Title }}) further access the specific properties of the object.
Q2: How do these filters recognize and return Chinese characters when the string is in Chinese?
A2:The template engine of Anqi CMS has good support for Unicode character sets. When the string is Chinese,firstandlastThe filter can correctly identify each Chinese character as an independent 'character', so it will accurately return the first or last Chinese character in the string, rather than displaying garbled text or only returning part of the bytes.
Q3: If the variable being processed is neither a string nor an array, or is a null value,firstandlastwhat will the filter return?
A3:
- Non-string and non-array types (such as numbers, boolean values):if you have a single number (like
5) or a boolean value (such astrue)usingfirstorlastfilter, they will usually treat it as a 'contains itself' single-element set and return the value itself directly (for example,{{ 5|first }}it will return5,{{ true|last }}it will returntrue). - null value (
nilIf empty or an array:If the variable being processed isnilAn empty string ("") or an empty array ([])firstandlastThe filter will not return any content and will not raise any errors, making it more robust to use in templates.