How to get the length of an array in Smarty
Hypothesis:
An array is assigned to Smarty. Assume that the division of Smarty is '{' and '}'.
In many materials, we can see that when the length of the array is required in Smarty, it can be used after the array to add the | count method call. That is, the length of the array is obtained through {array | count. However, when writing a template today, we find that we cannot get the length of the Array, but only get a returned string Array. That is to say, only the {array} result is returned, but the length of its array is not returned.
Check the smarty \ plugins folder and find that there is no count-related method. That is to say, count directly calls the method in php.
Later, through the network information, we found that we can add @ in front of count to get the length of the array correctly. Then, we can further check the Smarty source code and find that when Smarty processes the method name behind the property regulator, it will perform special processing on the front side plus.Therefore, when calling a function defined in php in the property regulator of Smarty, it can be expressed by adding.
1. When testing methods of the array type, it is found that errors will occur if the @ symbol is not added. For example, to call the count method to obtain the length of an array, you can call {array | @ count }, when you call the end method to obtain the last data group of an array, you can use {array | @ end }.
2. When testing the functions related to the string, it is found that all functions can be called normally without adding.
3. Others have not been carefully tested. It is not encouraged to call complicated php functions in smarty, because Smarty is designed to implement code and template separation. It should be determined that the template is used by the interface design personnel, if complicated logic is added too much, it is a torment for them.