I believe a lot of friends do not know, you can call PHP's built-in function in the Smarty template, let's take a look at its usage.
Template writing:
{' param1 ' |functionname: ' param2 ': ' Param3 '}
PHP function Prototypes:
echo functionname (' param1 ', ' param2 ', ' param3 ');
Instance:
{' 1234567 ' |substr: ' 1 ': ' 2 '}
the following is related to the parameter order of the function
{' A ' |str_replace: ' A ': ' ABCD '}
Directly to, write a function call directly in PHP, do not need to register the modifier.
A further study: The discovery of an array would be an error.
An array of arrays is assigned to smarty, assuming that the smarty delimiter is ' {' and '} '.
As you can see from a lot of data, when you ask for the length of an array in smarty, you use the method call that is added |count after the array. That is, the length of the array is obtained by {array|count}. But today when I write a template, I find that this does not get the length of the array, but just a return string array. This means that only the result of {array} is returned, and the length of its array is not returned.
Looking at the Smarty\plugins folder, there is no related method for count, that is, Count is a direct call to the method in PHP.
Later, through the data on the network, it is found that you can add @ in front of Count to get the length of the array correctly. Further look at the source code of the smarty, found that smarty on the property regulator behind the method name processing, the front plus @ will be special treatment. So make a decision: When you invoke a function defined in PHP in a property conditioner in Smarty, you can express it by adding @.
1, when the type of the array of methods to test, found that no @ symbol will be error. 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 {array| @end} when you call the end method to get the last set of data for the arrays.
2, in the string of related functions to test, found that add not add @ can be normal call.
3, the other has not been carefully tested.
It is not encouraged to call complex PHP functions in Smarty, because Smarty's intention is to achieve the separation of code and template, do not deviate from the original design.
Template writing:
{' param1 ' |functionname: ' param2 ': ' Param3 '}
PHP function Prototypes:
echo functionname (' param1 ', ' param2 ', ' param3 ');
Instance:
{' 1234567 ' |substr: ' 1 ': ' 2 '}
The following is a strange and a function of the parameter order has a relationship
{' A ' |str_replace: ' A ': ' ABCD '}
Smarty Calling Custom Functions
Calling a custom function requires the use of register_function () to register
Here's an example of a common string cut
function as follows
Copy the Code code 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)//character is Chinese
{
$length + +;
$i + +;
$len--;
}
}
$str =substr ($text, 0, $length);
if ($len > $max) $str. = "...";
Return $str;
}
Registration function
$smarty->register_function (' Len ', ' Smartylen ');
Template invocation
{len text= "test" length= "1"}//Note here the text and length is actually the function of the parameters of the 2 parameters must be the function of the internal variable name consistent, actually passed back is the number of groups, in the function with extract ($params); Imports the variables in the array into the current symbol table.
http://www.bkjia.com/PHPjc/326639.html www.bkjia.com true http://www.bkjia.com/PHPjc/326639.html techarticle I believe a lot of friends do not know, you can call PHP's built-in function in the Smarty template, let's take a look at its usage. Template writing: {' param1 ' |functionname: ' param2 ': ' param3 '} ...