In the template development of Anqi CMS, we often use various filters (filters) to format or extract data.firstandlastThe filter is one of the more common ones, used to retrieve the first or last element from a string or array.Many friends who use A safe CMS may be curious, when we process data containing Chinese characters, such as article titles or content snippets, will these two filters return a single Chinese character?
The answer is:Yes, Anqi CMS'sfirstandlastThe filter will return a single Chinese character intelligently and intuitively when processing Chinese string.
This is due to the excellent support of Unicode character set by the underlying Go language of AnQi CMS, which enables the template engine to maintain a high degree of accuracy and consistency when processing multilingual characters. Whether it is English characters or Chinese characters,firstThe filter can accurately extract the first character of the string.lastThe filter extracts the last one.
Let's understand this better with some specific examples:
If you have an English string{{ "AnQiCMS"|first }}it will outputA.{{ "AnQiCMS"|last }}it will outputS.
Similarly,{{ "安企内容管理系统"|first }}the output result is安English.{{ "安企内容管理系统"|last }}Then you will get统English.
This processing method is very in line with our daily habits and intuition, eliminating the trouble of writing complex logic to judge and extract Chinese characters, which greatly improves the efficiency and simplicity of template development.
Not only strings: handling multiple data types
firstandlastThe filter is not limited to strings.Their design philosophy is to obtain the "first or last element", which makes them also perform well when handling other types of data.
- Array or slice (Slice)If your variable is an array or slice,
firstIt will return the first element of the array,lastIt will return the last one. For example, suppose you have an array containing multiple comment objects.commentsArray,{{ comments|first }}It will return the first comment object in the array. - Numbers and boolean valuesWhen the variable itself is a number (such as
5) or a boolean value (such astrue){firstandlastthe filter will directly return this value itself, for example{{ 5|first }}it will output:5,{{ true|last }}it will output:true. - Empty or
nilIf a variable is an empty string, an empty array, or not defined at all,nil), thenfirstandlastThe filter will not output any content and will not cause any error, which makes conditional judgments in templates safer.
This versatility makesfirstandlastThe filter has good versatility in the template of the Safe CMS.Whether you want to quickly preview the first element in the list or need to make initial judgments and displays of some dynamic data, they can provide concise and effective solutions.
Application in actual scenarios
In the actual content operation and template design of AnQi CMS,firstandlastfilters have many practical scenarios:
- List overview displayIn an article list or product list, you may want to display the first character as a decorative icon or mark next to each item's title, or to process the last character of the summary in a special way.
- Dynamic content extraction: When the length of some fields (such as custom short description) is uncertain, you can use
firstorlastTo retrieve specific parts for display, although in more complex extraction scenarios,truncatecharsortruncatewordsfilters will be more commonly used. - Navigation and classification optimizationFrom the long classification name, extract the first character as a quick index, or briefly display the level name in multi-level navigation.
- Data status judgment: combined
ifLabel, determines whether an array has at least one element ({% if my_list|first %}) to decide whether to render a block.
When using these two filters, we just need to remember their working principles: they handle strings by characters (including Chinese characters) and handle sets by elements.This can help us better plan the template logic, avoiding unnecessary troubles.
Common Questions (FAQ)
1. In addition to Chinese string,firstandlastwhat other types of data can the filter handle?It can handle Chinese string, English string, array (or slice), numbers, and boolean values.For arrays, they return the first or last element; for numbers and booleans, they return the value of the variable itself.
2. If the string is empty or the variable isnilUsefirstorlastWhat will the filter result be?If the string is empty, the array is empty, or the variable itself isnil(undefined),firstandlastThe filter will not output any content. It will not cause any errors, which increases the robustness of the template when used.
3. In usefirstorlastWhat potential problems should be noted when using the filter?The most important thing to note is to make sure you have a clear understanding of the data type and expected results.For example, if you expect to get the first character of a string, but actually pass a number, you will get the number itself rather than the first character of its string representation.ifLogical judgment, for example{% if my_string|first %}{{ my_string|first }}{% endif %}To avoid outputting empty content when the data does not exist.