Call the method of the php built in function in smarty

  • 2020-05-30 19:41:34
  • OfStack

For those of you who don't 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 prototype:
echo functionName('param1','param2','param3');
Example:
{'1234567'|substr:'1':'2'}

This has to do with the order of the arguments to the function

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

I'm going to write a function call in php without registering a modifier.

Step 1: discover that arrays can go wrong.

An array array is assigned to Smarty, assuming that the delimiters of Smarty are '{' and '}'.

As you can see from many sources, when you want the length of an array in Smarty, you can use the |count method call after the array. That is, the length of array is obtained by {array|count}. But today, when I was writing the template, I found that instead of getting the length of the array, I got a string Array that was returned. That is, it only returns the result of {array}, not the length of its array.
Check the smarty\plugins folder and find that there are no count related methods, that is, count directly calls methods in php.

Later, through the data on the network, I found that I could add @ in front of count to get the length of the array correctly. Take a step further and look at the source code of Smarty. When Smarty processes the method names behind the property modifier, it will do special processing to the @ in front. Hence the judgment: when a function defined in php is called from the property modifier in Smarty, it can be represented by adding at sign.
1. When testing the method of type array, it was found that it would be wrong to not add @ symbol. For example, to call the count method on an array to find the length of the array, you can call {array |@count}, and to call the end method on an array to get the last data set of the array, you can call {array |@end}.
2. When testing the related functions of the string, it is found that the function can be normally called with or without @.
3. Others have not been seriously tested.

Calling complex php functions in smarty is discouraged, because Smarty is designed to separate code from templates and not deviate from their original intent.

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

php function prototype:
echo functionName('param1','param2','param3');

Example:
{'1234567'|substr:'1':'2'}
The next oddity has to do with the order of the arguments to the function
{'a'|str_replace:'A':'abcd'}

Smarty calls custom functions
Calling a custom function requires registration with register_function()
Here's an example of a common string cut

Function as follows


<?php
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 characters are in Chinese 
           {
               $length++;
               $i++;
      $len--;
            }
}
$str=substr($text,0,$length);
if($len>$max)$str.="...";
Return $str;
} 

The registration function
$smarty- > register_function('len',"SmartyLen");

The template calls
{len text=" under test "length="1"} // note that text and length are actually two parameters in the function. Imports variables from the array into the current symbol table.


Related articles: