smarty模板中使用php函数

一般情况下,最好不在模板中使用php函数,但有时没有其他更好的办法,但是smarty自带的变量调节函数不能够满足需求,

  控制器testtest.php

<?php
require 'libs/Smarty.class.php';
	$smarty=new Smarty;
	$smarty->compile_check=true;
	$smarty->debugging=true;
	$str1='testtesttesttest';
	$str2='this is a';
	$str3='鐢ㄦ潵娴嬭瘯';
	$str4='this is four';
	$smarty->assign('str1',$str1);
	$smarty->assign('str2',$str2);
	$smarty->assign('str3',$str3);
	$smarty->assign('str4',$str4);
	$smarty->display('testtest.tpl');
?>	

 视图部分,(模板中),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.HouseSeQv},当然也可以直接用smarty自带的replace变量调节器,{$arr.HouseSeQv|replace:$name,"<span style='color:#03C200;font-size:14px'>$name</span>"}

截图


smarty模板中使用php函数
 

你可能感兴趣的:(html,PHP)