During the development of AnQi CMS templates, we often need to handle various data, among which arrays (or lists) are commonly used to organize data. When it comes to directly defining an array containing different data types in the template, many friends may be curious, as types like numbers can also belistDoes the filter process and include it?
The answer is yes. In the Anqi CMS template,listThe filter is designed to be very flexible, it fully supports including numeric data types in the defined array.This means that when you are building a template, you can conveniently unify different types of data such as strings, integers, floating-point numbers, etc. in an array for management and operation, without worrying about type incompatibility issues.
To be specific,listThe way a filter works is to convert a string that conforms to the JSON array format into an array object that can be used 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 identify and retain the type characteristics of the numbers within.
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 block,'["安企CMS","AnQiCMS","内容管理系统","免费建站系统","免费模板",4,5]'This string islistAfter filtering, a mixed array containing strings and numbers was generated. When we use{% for item in values %}Traversing this array, whether it is a string or a number4and5All can be accessed and displayed correctly. Numeric elements maintain their numerical properties in templates, for example, you can perform arithmetic operations on them, or automatically convert them to strings for display when needed. This intelligent type handling greatly simplifies template writing.
This mixed type array's definition ability provides great convenience in the presentation of website content.Imagine that you might need to display product information such as names, prices, and inventory in a list, which often contains strings (names) and numbers (prices, inventory). ThroughlistFilter, 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 bring a more concise code structure.
Although Anq CMS also providedsplitormake_listThe filter is used to split strings into arrays butlistThe uniqueness of the filter lies in its ability to directly declare an array containing various data types in a structure similar to JSON in the template. This avoids using firstsplitSplitting plain text and then performing additional type conversion steps makes it more intuitive and efficient to handle complex data in templates.
In summary, of Anqi CMS'slistThe filter is a powerful and flexible tool that not only helps us define arrays conveniently in templates but also allows for a variety of element types within the array, including numeric types. This undoubtedly brings more possibilities and convenience to the presentation of website content.In your Anqi CMS template development practice, boldly try and make use of this feature, as it will save you a lot of trouble.
Frequently Asked Questions (FAQ)
Ask: 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?Answer: No need.listThe filter can identify numeric types well. You can write numbers directly, for example{% set my_numbers = '[10, 20, 30]'|list %}. Only elements of string type need to be enclosed in double or single quotes.Question: Use
listThe array element defined by the filter, is it operated as a string or a number in the template?Answer: The AnQi CMS template engine will perform intelligent processing in this case.If the array element is defined as a number (i.e., without quotes in the source string), it will be treated as a numeric type in the template, which means you can perform arithmetic operations on it.But if it is used in a context that requires a string (such as direct display), it will also automatically be converted to a string to ensure compatibility.Question:
listthe filter meetssplitWhat are the main differences when the 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:","HoweverlistThe filter is 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 an array with a fixed and complex structure, whilesplitit is often used to handle lists of dynamically generated plain text data.