php使用smarty实例

1、下载smarty将解压后的libs目录copy到项目目录下。

2、新建一个php文件,假如和libs目录同一级上。命名为smarty_test.php,然后增加两个目录一个为Templates文件夹,另一个为Templates_c目录,前者是以后模板文件要存放的目录,后者是smary编译后的文件存放目录。

3、在Templates目录下建立一个template.htm文件,输入以下代码:

 1  < html >
 2  < head >
 3  < style  type ="text/css" >
 4  .bold {
 5      font-weight : bold ;
 6      font-size : 12px ;
 7      padding : 10px ;
 8      width :  300px ;
 9      border : solid 1px blue ;
10      line-height : 20px ;
11  }
12  </ style >
13  </ head >
14  < body >
15  < div  class ="bold" > {{$test}} </ div >
16  </ body >
17  </ html >  


3、在smart_test.php中输入以下代码

 1  <? php
 2  include_once ( ' ./libs/Smarty.class.php ' );  //如果在php.ini文件中将include_path添加了smart的目录这里就直接写Smarty.class.php就可以了。
 3 
 4  $smarty   =   new  Smarty();
 5  $smarty   ->  template_dir  =   " ./Templates " ;      // 模板存放目录
 6  $smarty   ->  compile_dir  =   " ./Templates_c " ;      // 编译目录
 7  $smarty   ->  left_delimiter  =   " {{ " ;              // 左定界符
 8  $smarty   ->  right_delimiter  =   " }} " ;              // 右定界符
 9  $smarty   ->  assign( ' test ' , ' if success display this contents. ' );
10  $smarty   ->  display( ' template.htm ' );
11  ?>


保存,浏览smart_test.php会发现在htm文件中的{{test}}被替换成了“if success display this contents.“。

转载于:https://www.cnblogs.com/tonysuen/archive/2009/12/15/1624791.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值