How to call php built-in functions in smarty

Source: Internet
Author: User
Tags smarty template
In smarty, php built-in functions are called. I believe many people do not very well understand smarty's important function. They can call php built-in functions in the smarty template.

In smarty, php built-in functions are called. I believe many people do not very well understand smarty's important function. They can call php built-in functions in the smarty template.

I believe many of my friends still don't know. You can call php's built-in functions in the smarty template. Let's take a look at its usage.

Template writing:
{'Param1' | functionName: 'param2': 'param3 '}

Php function prototype:
Echo functionName ('param1', 'param2', 'param3 ');
Instance:
{'20140901' | substr: '1': '2 '}

The following is related to the parameter sequence of the function.

{'A' | str_replace: 'A': 'abcd '}

Directly extend to, directly write a function call in php, do not need to register the modifier.

Further research: an error occurs when an array is found.

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 separate code from templates, so it does not deviate from the original design intention.

Template writing:
{'Param1' | functionName: 'param2': 'param3 '}

Php function prototype:
Echo functionName ('param1', 'param2', 'param3 ');

Instance:
{'20140901' | substr: '1': '2 '}
The following is a bit strange about the Parameter order of the function.
{'A' | str_replace: 'A': 'abcd '}

Smarty calls User-Defined Functions
You must use register_function () to register a custom function.
Here is an example of common string cutting.

The function is as follows:
The Code is as follows:
Function SmartyLen ($ params ){
Extract ($ params );
$ Len = strlen ($ text );
$ Max = $ length;
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Chr = substr ($ text, $ I, 1 );
If (ord ($ chr)> 0x80) // The character is Chinese
{
$ Length ++;
$ I ++;
$ Len --;
}
}
$ Str = substr ($ text, 0, $ length );
If ($ len> $ max) $ str. = "...";
Return $ str;
}

Register a function
$ Smarty-> register_function ('len', "SmartyLen ");

Template call
{Len text = "test" length = "1"} // note that the text and length here are actually the parameters of the two parameter functions in the function, which must be named by the internal variable name of the function. consistent, actually, it is an array. In the function, use extract ($ params); to import the variables in the array to the current symbol table.

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.