How to call php built-in functions in smarty

怪我咯
Release: 2023-03-11 17:20:01
Original
1463 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 is 1 parameter:
{{'param1'|functionName}}
For example,
{{$tmpStr|strlen}}

When there are 2 parameters:
{{'param1'|functionName:'param2'}}
{$tmpStr|substr:'1'}

When there are multiple parameters:
{{'param1'|functionName:'param2':'param3'..}}
{{$tmpStr|substr:'1':' 2'}}

{{'a'|str_replace:'A':$tmpStr}}


1 Marty determines whether it is empty


The following code php assigns to tmpStr if it is empty or not assigned; then 0 is output in smarty. This calls PHP's built-in function strlen
php code:
$smarty->assign('tmpStr','');

smarty code:
{{if $tmpStr|strlen > 0 }}
1
{{else}}
0
{{/if}}

##2 Marty finds the length of the array

Find the length of the array 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));


smarty code:

{{if $tmpArr|is_array && $tmpArr|@count > 0}}

{{$tmpArr|@count}}
{{else}}
0

{{/if}}


##3 marty

StringReplace with the following code is to replace b in tmpStr with c

$smarty->assign('tmpStr','abb');

{{'b'|str_replace:'c':$tmpStr}}


Output: acc


4 marty interception field

$smarty->assign('tmpStr', 'abb');

{{$tmpStr|substr:'1'}}
Output:bb

{{$tmpStr|substr:'1':'1'} }
Output: b

The above is the detailed content 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!