In the template development process of AnQi CMS, we often need to handle various data, among which an array (or list) is a commonly used data organization structure. When it comes to directly defining an array containing different data types in the template, many friends may be curious, as for types like numbers, they can also belistFilter processing and including it?
The answer is yes. In the Anqi CMS template,listThe filter design is very flexible, it fully supports the inclusion of numeric data types in the defined array.This means that when you are building a template, you can conveniently manage and operate different types of data such as strings, integers, floating-point numbers, etc. in a single array, without worrying about type incompatibility issues.
To be specific,listThe way the filter works is to convert a string that conforms to the JSON array format into an array object that is usable by the template.This string can be a pure text list, or a mixed list containing numbers or other serializable data types.When the template parser encounters this format, it can intelligently recognize and preserve the type characteristics of the numbers.
Let's look at a typical example from the AnQi CMS document, which clearly demonstrates this capability:
{% set values = '["安企CMS","AnQiCMS","内容管理系统","免费建站系统","免费模板",4,5]'|list %}
{% for item in values %}
<span>{{item}}</span>/
{% endfor %}
In this code,'["安企CMS","AnQiCMS","内容管理系统","免费建站系统","免费模板",4,5]'This string islistFiltering has generated a mixed array containing strings and numbers. When we use{% for item in values %}traversing this array, whether it is a string or a number4and5All can be correctly accessed and displayed.The numeric element maintains its numerical characteristics in the template, for example, you can perform arithmetic operations on them, or automatically convert them to strings for display when necessary. This intelligent type handling greatly simplifies the writing of templates.
This mixed type array definition capability provides great convenience in the presentation of actual website content.Imagine that you might need to display product names, prices, stock information, and other data in a list. This data often contains strings (names) and numbers (prices, stock).listFilter, you can efficiently organize this diverse information.Also, when building a special navigation or configuration item, some are text labels, and some are corresponding IDs (numbers). Packaging them into an array can lead to a more concise code structure.
Although AnQi CMS also providessplitormake_listThe auto filter is used to split strings into arrays, butlistThe unique feature of the filter is that it allows us to directly declare an array containing various data types in a template in a structure similar to JSON. This avoids first usingsplitSplit the plain text and then perform additional type conversion steps, making it more intuitive and efficient to handle complex data in templates.
In summary, the Anqi CMS'slistThe filter is a powerful and flexible tool that not only helps us conveniently define arrays in templates but also allows for a variety of element types within the array, including numeric types, which undoubtedly brings more possibilities and convenience to the presentation of website content.In your safe CMS template development practice, boldly try and make use of this feature, as it will save you a lot of trouble.
Common Questions and Answers (FAQ)
问:If I only want to define an array of pure numbers, for example
[10, 20, 30], do I also have to enclose the numbers in quotes?答:No need.listFilter can accurately identify numeric types. You can write numbers directly, for example{% set my_numbers = '[10, 20, 30]'|list %}. Only string elements need to be enclosed in double quotes or single quotes.Question: Using
listFilter definition array element, is it operated as a string or number in the template?答:The template engine of AnQi CMS will perform intelligent processing in this case.If the array element is defined as a number (i.e., it is not quoted in the source string), it will exist as a numeric type in the template as much as possible, which means you can perform arithmetic operations on it.但如果它被用在一个需要字符串的上下文中(例如直接显示),它也会自动转换为字符串形式,确保兼容性。Q:
listFilter is related tosplitWhat are the main differences when a filter creates an array?Answer:splitThe filter is mainly used to split a long string into an array of strings according to a specified delimiter, for example"apple,banana,orange"|split:",".listFilters are more focused onDefine a structured array directly in the templateThis array can contain predefined mixed type data (such as strings and numbers), and its input format must be a valid JSON array string.listIt is more convenient to define fixed and complex structured arrays.splitIt is often used to handle lists of dynamically generated plain text data.