Example sharing of how to call PHP built-in functions in smarty

黄舟
Release: 2023-03-14 09:42:01
Original
1449 people have browsed it

CleverCode found that calling phpbuilt-in functions in smarty can be achieved through |. |No spaces before and after. If it is an array, you need to add @ to find the count.

When there are 1 parameters:

{{'param1'|functionName}}
Copy after login

For example,

{{$tmpStr|
strlen
}}
Copy after login

When there are 2 parameters:

{{'param1'|functionName:'param2'}}
{$tmpStr|substr:'1'}
Copy after login

When there are multiple parameters:

{{'param1'|functionName:'param2':'param3'..}} 
{{$tmpStr|substr:'1':'2'}}
{{'a'|str_replace:'A':$tmpStr}}
Copy after login

1 marty determines whether it is empty

The following code The tmpStr allocated by php is empty or not allocated; then smarty outputs 0. This is calling PHP's built-in function strlen
php code:

$smarty->assign('tmpStr','');
smarty代码:
{{if $tmpStr|strlen > 0 }}
    1
{{else}}
    0
{{/if}}
Copy after login

2 marty seeks the length of the array


Get the array length through PHP's built-in functions is_array and count. The following code outputs 5
php code:

$smarty->assign('tmpArr',array(1,2,3,4,5));
Copy after login

smarty code:

{{if $tmpArr|is_array && $tmpArr|@count > 0}}    
{{$tmpArr|@count}}{{else}}    
0
{{/if}}
Copy after login

3 marty StringReplacement

The following code replaces b in tmpStr with c

$smarty->assign('tmpStr','abb');
{{'b'|str_replace:'c':$tmpStr}}
Copy after login

Output:

acc
Copy after login

4 marty intercept field

$smarty->assign('tmpStr','abb');
{{$tmpStr|substr:'1'}}
Copy after login

Output:

bb
Copy after login
{{$tmpStr|substr:'1':'1'}}
Copy after login

Output:

b
Copy after login

The above is the detailed content of Example sharing of how to call PHP built-in functions in smarty. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template