下面是对项目下各目录的说明(eclipse中)
然后简单测试smarty
先写一个简单的模板文件在templates下新建一个test.tpl(后缀名可任意取,建议使用tpl)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>我的第一个smarty程序</title> </head> <body> {$helloworld} </body> </html> |
先不管{$helloworld}是说明意思,接着建立一个自己的Smarty类
<?php require 'smarty/Smarty.class.php'; class MySmarty extends Smarty{ function MySmarty(){ $this->template_dir = "smarty/templates/"; $this->compile_dir = 'smarty/templates_c/'; $this->config_dir ='smarty/configs/'; $this->cache_dir ='smarty/cache/'; } } ?> |
require 'smarty/Smarty.class.php';,这样我们才能调用Smarty的方法.然后我们新建一个php文件test.php,路径任意
<?php //引入我的MySmarty类 require_once 'MySmarty.php'; //实例化MySmarty类 $smarty = new MySmarty(); //为模板里的$helloworld变量赋值,赋值为我的第一个smarty模板文件 $smarty->assign('helloworld',"我的第一个smarty模板文件"); //显示模板文件 $smarty->display('test.tpl'); ?> |
然后右键运行,便可以看到模板里的$helloworld会被替换成我的第一个smarty模板文件