(转~改)smarty里使用php函数

Smarty模板引擎实践与应用
本文详细介绍了如何使用Smarty模板引擎进行网站开发,包括配置、变量赋值、模板语法及输出结果解析,通过实例展示了其在网页内容动态生成中的强大功能。

 smarty:

<?php

//引入Smarty类文件
include_once("Smarty/Smarty.class.php");

//实例化Smarty类
$smarty = new Smarty();

$smarty->caching=false;

//定义模版目录
$smarty->template_dir = "./templates";

//定义编译目录
$smarty->compile_dir = "./templates_c";

//定义缓存目录
$smarty->cache_dir = "./smarty_cache";

//定义模版解析的标签
$smarty->left_delimiter = "{";

$smarty->right_delimiter = "}";

?>

index.php:

<?php

//引入smarty配置文件
require './config/Smarty_conf.php';

//模版变量赋值

$smarty->assign('stringword','helloworld');

$smarty->assign('title','this is a test page');

$smarty->assign('content','鐢ㄦ潵娴嬭瘯');

$smarty->assign('str','he says');

$smarty->assign('datetime','1353654721');
//打开对应模版
$smarty->display('index.html');

?>

 

 

 

index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
    

<body>
 <p>{$stringword|strlen}</p>
<p>{$title|strpos:'is'}</p>
<p>{'gb2312'|iconv:'utf-8':$content}</p>
<p>{$str|str_pad:20:".":STR_PAD_LEFT}</p>
<p>{$datetime|date_format:"%Y-%m-%d %H:%M:%S"}</p>
</body>
</html>

 输出结果为:

10

2

用来测试

.............he says

2012-11-23 15:12:01

 

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

转载于:https://www.cnblogs.com/wenzichiqingwa/archive/2012/11/25/2787529.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值