当前位置:Gxlcms > PHP教程 > php内置函数如何在smarty中调用的实例分享

php内置函数如何在smarty中调用的实例分享

时间:2021-07-01 10:21:17 帮助过:14人阅读

CleverCode发现smarty中调用php内置函数可以通过|实现。|前后没有空格。如果是数组求count需要加上@。

1个参数时候:

  1. {{'param1'|functionName}}

例如

  1. {{$tmpStr|
  2. strlen
  3. }}

2个参数时候:

  1. {{'param1'|functionName:'param2'}}
  2. {$tmpStr|substr:'1'}

多个参数时候:

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

1 marty判断是否为空

如下代码php分配给tmpStr为空或没分配;那么smarty中输出0。这个是调用了php的内置函数strlen
php代码:

  1. $smarty->assign('tmpStr','');
  2. smarty代码:
  3. {{if $tmpStr|strlen > 0 }}
  4. 1
  5. {{else}}
  6. 0
  7. {{/if}}

2 marty求数组长度


通过php内置的函数is_array和count求得数组长度,如下的代码输出5
php代码:

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

smarty代码:

  1. {{if $tmpArr|is_array && $tmpArr|@count > 0}}
  2. {{$tmpArr|@count}}{{else}}
  3. 0
  4. {{/if}}

3 marty字符串替换

如下的代码是将tmpStr中的b替换为c

  1. $smarty->assign('tmpStr','abb');
  2. {{'b'|str_replace:'c':$tmpStr}}

输出:

  1. acc

4 marty截取字段

  1. $smarty->assign('tmpStr','abb');
  2. {{$tmpStr|substr:'1'}}

输出:

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

输出:

  1. b

以上就是php内置函数如何在smarty中调用的实例分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行