In the template design of AnQi CMS,firstandlastThe filter is a pair of concise and practical tools designed to help us quickly obtain the first or last character of a string, as well as the first or last element of an array.They perform well in handling simple data structures, but in practical applications, if their internal mechanisms are not understood deeply enough, some unexpected behaviors may occur.
First, let's understand their core functions. For string data,firstit will return the first character,lastIt returns the last character. This includes Chinese characters, where each character is treated as a separate entity, for example,{{ "你好世界"|first }}and you will get“you”,while{{ "你好世界"|last }}You will get the "universe". When faced with an array (or a slice in Go language),firstandlastThey perform equally intuitively, returning the first and last elements of the array. This is very convenient for situations where quick access to the edges of the list is needed.
However, when we apply these filters to non-string or non-array data types, the situation becomes less intuitive, which is also a place to pay special attention to when using them. For example, if you try to apply a filter to an integer (such as5)firstFilter, you might expect to get5the first character of the string representation (for example5), but actually, it will return the original integer value5. Similarly, boolean values (such astrueIt will be returned directlytrueThis means thatfirstandlastWill not internally convert these non-string, non-array type data into strings for processing, but will directly treat these values as their 'first' or 'last' elements.
Further, when dealing with complex objects or structures, this behavior becomes particularly prominent. For example, applying to an object likecomplex.commentssuch an object.firstA filter that does not intelligently extract a primary property of an object, nor does it convert it to a parseable string and then take the first character. Instead, it may return the default string representation of the object (for example<pongo2_test.comment Value>This is not usually the 'first element' we expect with business significance.This returns the type description of the object in memory, not the first part of its actual content.nilOr empty array/string, they will not return any content, this is the expected behavior, but still pay attention when using it to avoid blank output.
The reason why these "limits" appear is that the template engine used by Anqi CMS maintains a certain rigor in handling data types.It does not perform implicit type conversions that could lead to misunderstandings, but rather requires the data type passed to match the expected type of the filter as closely as possible.firstandlastWhen filtering, it is recommended to always ensure that the target data is the string or array type you expect. If you need to extract leading and trailing characters from numbers or boolean values, you should first convert them to strings.stringformatFilters will explicitly convert it to a string, for example{{ 123|stringformat:"%s"|first }}For complex objects, if they contain iterable lists or explicit attributes, they should be accessed directly rather than blindly accessing the entire object.firstorlast.
In summary,firstandlastThe filter is a powerful tool in Anqi CMS template for handling strings and arrays.But when using them, please be sure to pay attention to how they handle non-string, non-array, and complex object types.Understanding these 'restrictions' is not a flaw but a manifestation of maintaining clarity and rigor in data types, which helps us write more robust and expected template code.
Frequently Asked Questions (FAQ)
Q1: Why am I dealing with numbers123UsefirstThe filter still returns123instead of1?A1:firstandlastThe filter is mainly designed for string and array (slice) types.When applied to numbers, the template engine does not automatically convert it to a string before extracting the first character, but directly returns the original numeric value.{{ 123|stringformat:"%s"|first }}.
Q2: I have a complex custom objectMyArticleI want to get the first property value of it, but{{ MyArticle|first }}it returns a string like<pongo2_test.MyArticle Value>This text, why is it?A2:firstandlastThe filter cannot 'smartly' understand the internal structure of a custom object and extract its 'first property'.When applied to a complex object that is neither an array nor a string, they will return the default string representation of the object in the template engine, which is usually its type information and memory address (or a brief description).{{ MyArticle.Title }}or{{ MyArticle.Images|first }}Wait to access, make sure you are accessing specific iterable or extractable attributes.
Q3: How to judgefirstorlastIs the returned result empty?A3: IffirstorlastThe object applied by the filter is empty (such as an empty string"", an empty array[]ornil), they will return an empty value. You can useifa statement to check if the result is empty, for example{% if item|first %}Or more strictly, to process the string result{% if not (item|first == "") %}to judge.