Discussion on how to get the length of the array in Smarty and how to Smarty call PHP functions _php Tips

Source: Internet
Author: User

How to get the length of an array in Smarty
Premise hypothesis:
An array of arrays is assigned to smarty, assuming that the Smarty's delimiters are ' {' and '} '.
As you can see in a lot of data, the length of the array is required in the smarty, and the |count method call is used after the array. The length of the array is obtained through {array|count}. But today, when writing a template, you find that you don't get the length of the array, just the string array that's returned. That is, it simply returns the result of the {array} and does not return the length of its array.

Looking at the Smarty\plugins folder, I found that there was no method for count, that is, Count is a direct call to the method in PHP.
Later, through the data on the network, it was found that you can add @ before count to get the correct length of the array. Further look at the source code of Smarty, found that the smarty of the property adjuster after the method name processing, will be the front plus @ for special treatment. so make a judgment: when you call a defined function in PHP in the property adjuster in Smarty, you can express it by adding @.

1. When testing a method that is an array of types, it is found that no @ symbol can be faulted. For example, to call the Count method on an array to find the length of the array, you can call {array| @count}, and you can pass the {array| @end} if you want the last set of data to be derived from the arrays by calling the end method.
2, in the relevant function of the string test, found that the addition of @ can be normal calls.
3, the other has not been carefully tested. It is not encouraged to invoke complex PHP functions in Smarty, because Smarty's intention is to implement code and template separation, it should be recognized that the template is the interface design personnel, if too many complex logic, for them is a kind of torture.


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.