PHP Smarty 用法

有没有为ASP.NET提供的母板页感到兴奋?但是转过头PHP有没有母版呢?smarty模板引擎为你解决了问题。

首先,我们要到smarty的官方网站上下载smarty(http://www.smarty.net/download)下载完后,我们解压里面的lib文件夹到网站根目录下,更名为smarty(具体名称自己喜欢改什么就改什么)。smarty的部署工作就完成啦!

下面便是如何使用smarty母板引擎了(具体smarty高深的我再此不提),新建四个文件夹到网站根目录:cache、configs、templates、templates_c,把你的模板文件命名为master.tpl,保存至templates文件夹下。这是我的master.tpl文件。

<html>
<head>
<title><{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>

<{$title}>便是我们留下的页面标题写的地方。

<{$content}>便是我们留下的页面正文写的地方。

下面我们来看看test.php。

<?php
	include_once('./smarty/Smarty.class.php');
	$smarty=new Smarty();
	$smarty->debuging = true;
	$smarty->caching = false;
	$smarty->template_dir = "./templates/";
	$smarty->compile_dir = "./templates_c/";
	$smarty->config_dir = "./configs/";
	$smarty->cache_dir = "./cache/";
	$smarty->cache_lifetime = 120;
	$smarty->left_delimiter = "<{";
	//<{便是smarty要编译时使用的开始符号"<{"$title}>
	$smarty->right_delimiter = "}>";
	//}>便是smarty要编译时使用的结束符号<{$title"}>";
	$smarty->assign("title", "主页");
	$smarty->assign("content");
	//assign是修改模板文件中的smarty变量,比如我们在模板文件中有一个$title,便可以在assign中修改。
	$smarty->display("master.tpl");
	//display是导入模板文件。
?>
好,我们的第一个使用smarty制作模板的PHP网页就做好啦!赶快看自己的成果吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值