The first or last value of a string or array

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

firstThe filter can get the first character of the string or the first value of the array. If the original string or array is empty, nothing will be returned. If the string is Chinese, the first Chinese character is returned.

lastFilters can get the last character of a string or the last value of an array. If the original string or array is empty, nothing will be returned. If the string is Chinese, the last Chinese character is returned.

How to use

firstHow to use filters:

{{ obj|first }}

lastHow to use filters:

{{ obj|last }}

For example, you need to returnTestThe first character in it can be written like this:

{{ "Test"|first }}
# Show result
T

Sample Demo

firstFilter

{{ "Test"|first }}
{{ complex.comments|first }}
{{ 5|first }}
{{ true|first }}
{{ nothing|first }}
{{ "Hello World"|first }}
# Show results
T
<pongo2_test.comment Value>


You

lastFilter

{{ "Test"|last }}
{{ complex.comments|last }}
{{ 5|last }}
{{ true|last }}
{{ nothing|last }}
{{ "Hello World"|last }}
# Show results
t
<pongo2_test.comment Value>