The first or last value of a string or array

How to get the first or last value of a string or array in the Anqi CMS template?

firstThe filter can obtain the first character of a string or the first value of an array. If the original string or array is empty, nothing is returned. If the string is Chinese, the first character is returned.

lastThe filter can obtain the last character of a string or the last value of an array.If the original string or array is empty, nothing is returned.If the string is Chinese, return the last Chinese character.

Usage Instructions

firstHow to use the filter:

{{ obj|first }}

lastHow to use the filter:

{{ obj|last }}

For example, if you need to returnTestthe first character of:

{{ "Test"|first }}
# 显示结果
T

Here is an example demonstration

firstFilter

{{ "Test"|first }}
{{ complex.comments|first }}
{{ 5|first }}
{{ true|first }}
{{ nothing|first }}
{{ "你好世界"|first }}
# 显示结果
T
<pongo2_test.comment Value>



你

lastFilter

{{ "Test"|last }}
{{ complex.comments|last }}
{{ 5|last }}
{{ true|last }}
{{ nothing|last }}
{{ "你好世界"|last }}
# 显示结果
t
<pongo2_test.comment Value>



界