教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 smarty中使用php函数的方法

smarty中使用php函数的方法

发布时间:2017-07-05   编辑:jiaochengji.com
本文介绍下,在smarty模板引擎中使用php函数的方法,分享一个示例代码,供大家学习参考。

本节内容:
如何在smarty中使用php函数?

例子:
 

复制代码 代码示例:
<?php
/**
* smarty模板引擎中使用php函数
* by www.jbxue.com
*/
require 'libs/Smarty.class.php';  
    $smarty=new Smarty;  
    $smarty->compile_check=true;  
    $smarty->debugging=true;  
    $str1='testtesttesttest';  
    $str2='this is a';  
    $str3='脚本 学堂 www.jbxue.com';  
    $str4='this is four';  
    $smarty->assign('str1',$str1);  
    $smarty->assign('str2',$str2);  
    $smarty->assign('str3',$str3);  
    $smarty->assign('str4',$str4);  
    $smarty->display('testtest.tpl');  
?>    
<html>
<head>
</head>
<body>
<p>{$str1|strlen}</p>
<p>{$str2|strpos:'is'}</p>
<p>{'utf-8'|iconv:'gb2312':$str3}</p>
<p>{$str4|str_pad:20:"-=":STR_PAD_LEFT}</p>
</body>
</html>

输出结果:
16
2

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

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

在编程时,用到了这个:
 

复制代码 代码示例:
{$name|str_ireplace:"<span style='color:#03C200;font-size:14px'>$name</span>":$arr

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

您可能感兴趣的文章:
smarty获得当前url示例代码
php smarty缓存应用与控制
什么是smarty及教程
smarty中使用php函数的方法
有关smarty的基本设置
smarty 模板if else使用实例与入门教程
有关smarty缓存的应用
php Smarty的缓存操作
smarty入门教程六
smarty结合xajax检测用户名

[关闭]
~ ~