How to get the length of the array in Smarty and how smarty calls the php function

Source: Internet
Author: User

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.


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.