block functions区块函数插件的定义与使用
在插件目录plugins里新建文件 block.插件名.php文件(如 block.插件名.php)
例:block.test2.php
<?php function smarty_block_test2($params,$content) { $replace=$params['replace']; $maxnum=$params['maxnum']; if($replace=='true'){ $content=str_replace(',',',',$content); $content=str_replace('。','.',$content); } $content=substr($content,0,$maxnum); return $content; } ?>
在tpl中新建content.html
{test2 replace='true' maxnum=29}
{$str}
{/test2}
index.php
<?php require('../smarty/Smarty.class.php'); $smarty = new Smarty(); //五配置 $smarty->left_delimiter = "{"; $smarty->right_delimiter = "}"; $smarty->template_dir ="tpl"; $smarty->compile_dir ="template_c"; $smarty->cache_dir ="cache"; //开启缓存的另两个配置 $smarty->caching =true; $smarty->cache_lifetime =120; $smarty->assign('str','Hello,my name is Han Meimei。How are you?'); $smarty->display('content.html'); ?>
运行结果:
Hello,my name is Han Meimei.