Smarty学习之安装篇

来源:互联网 发布:centos 安装twisted 编辑:程序博客网 时间:2024/06/04 19:40

1) 下载smarty安装包,文档 www.smarty.net

2) 将安装包中的lib文件夹放在C:/PHP/PEAR/Smarty/下

3) 在网站根目录下创建templates, templates_c, configs, cache这几个文件夹

4) 在网站根目录下创建setup.php, 配置好smarty

 

setup.php

<?php
require_once('C:/PHP/PEAR/Smarty/libs/Smarty.class.php');

class MySmarty extends Smarty{
function MySmarty(){
   $this->Smarty();
   $this->template_dir = 'templates';
   $this->compile_dir = 'templates_c';
   $this->config_dir = 'configs';
   $this->cache_dir = 'cache';
}
}
?>

5) 在网站根目录下创建index.php, 为模板index.tpl中的变量赋值,并使index.tpl显示

index.php

<?php
require_once('setup.php');
$smarty = new MySmarty();
$smarty->assign('name', 'Mary Hahabao');
$smarty->display('index.tpl');
?>

6) 在templates目录下创建模板文件index.tpl

 

templates/index.tpl

{* This is index page. *}
Hello {$name}, welcome to Smarty!

 

7) 在浏览器中运行index.php, 显示

Hello Mary Hahabao, welcome to Smarty!

成功~

原创粉丝点击