Smarty使用方法

一、配置smarty

1、到官网下载smarty压缩包:Smarty 2.6.26,解压并重命名为smarty;

2、将smarty拷贝到安装目录d盘;

3、配置php,编辑php.ini,找到include_path去掉注释,增加对smarty的lib的路径D:\smarty\libs,结果为:

include_path = ".;c:\php\includes;D:\smarty\libs"

注意php.ini中有2个include_path,我是win7系统所以选择的是; Windows: "\path1;\path2"下的include_path

二、使用smarty

1、在项目文件夹下新建4个文件夹:

cache:存放缓存

config:存放一些配置信息

templates:用来存放模版文件,可以是.tpl文件也可以是.html文件

templates_c:存放编译文件

2、在templates文件夹下建立模板index.tpl,代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>html5</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
	
欢迎[{$name}]光临smarty

</body>
</html>

3、在根目录下建立index.php文件输入代码:

<?php
include('Smarty.class.php');//引入smarty类文件,因为配置过php.ini所以这里直接用Smarty.class.php即可
$smarty=new smarty;//建立smarty对象
$smarty->template_dir="templates";//设置模板的路径
$smarty->compile_dir="templates_c";//设置存放编译文件的路径
$smarty->config_dir="config";//设置配置信息的路径
$smarty->cache_dir="cache";//设置存放缓存的路径
$smarty->caching = false;//设置缓存关闭
$smarty->left_delimiter="[{";//定义模板中插入代码的左边界
$smarty->right_delimiter="}]";//定义右边界,这里的左右边界和模板里的代码片段左右边界对应
?>

再输入代码:

<?php
$name="Tom";//声明并赋值变量
$smarty->assign("name",$name);//把变量赋值给标签
$smarty->display("index.tpl");//显示模板
?>

预览index.php便可看到效果:

欢迎Tom光临smarty

转载于:https://my.oschina.net/710409599/blog/306381

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值