smarty中使用php函数的方法

WBOY
发布: 2016-07-25 08:55:29
原创
1027 人浏览过
  1. /**
  2. * smarty模板引擎中使用php函数
  3. * by bbs.it-home.org
  4. */
  5. require 'libs/Smarty.class.php';
  6. $smarty=new Smarty;
  7. $smarty->compile_check=true;
  8. $smarty->debugging=true;
  9. $str1='testtesttesttest';
  10. $str2='this is a';
  11. $str3='脚本 学堂 bbs.it-home.org';
  12. $str4='this is four';
  13. $smarty->assign('str1',$str1);
  14. $smarty->assign('str2',$str2);
  15. $smarty->assign('str3',$str3);
  16. $smarty->assign('str4',$str4);
  17. $smarty->display('testtest.tpl');
  18. ?>
  19. {$str1|strlen}

  20. {$str2|strpos:'is'}

  21. {'utf-8'|iconv:'gb2312':$str3}

  22. {$str4|str_pad:20:"-=":STR_PAD_LEFT}

复制代码

输出结果: 16 2

测试: -=-=-=-=this is four

说明: 用四个变量,分别处理1,2,3,4个不能的参数,特此记录一下,模板中调用变量时,当只有一个参数是,就直接{$str1|函数名},当有函数有两个参数时,{第一个参数|函数名:第二个参数},当有三个参数时,{第一个参数|函数名:第二个参数:第三个参数},,当有4,5,,,参数时,以此类推,,,,,,,,,,,,

在编程时,用到了这个:

  1. {$name|str_ireplace:"$name":$arr
复制代码

顺带分享下,有兴趣的朋友可以琢磨下。



来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!