smartyquickstart中文

本文详细介绍了在Windows环境下安装和配置Smarty模板引擎的过程,包括环境准备、文件配置及示例代码展示。

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.youkuaiyun.com/mayongzhan - 马永占,myz,mayongzhan

官方的quickstart windows版的是英文的,刚试了下,顺便翻译了一下,并改了点东西,测试能正常运行。
红色字部分是每个人不同的,需要注意。
---------------------------------------------------------------------
Windows下安装 Smarty
前提:服务器和php已经安装好并可以正常运行。
下载 Smarty - http://smarty.php.net
解压文件, 重命名 Smarty.x.x.x 为 smarty (出于方便,建议不要把文件放在服务器目录下)
例如: d:\smarty
运行 phpinfo.php 找到 php.ini 文件的位置
编辑 php.ini的 include_path ,加入smarty的libs的路径。
例如: include_path = ".;d:\smarty\libs"
重启 IIS/Apache
在服务器目录下创建下面两个文件夹:
(wwwroot)/smarty/templates(模板以后放在这里)
(wwwroot)/smarty/configs
在非服务器目录下创建下面两个文件夹:
d:/smarty/templates_c
d:/smarty/cache(这个我目前根据本文的测试,没有用)
设置系统,保证这四个文件是可写的。
在 (wwwroot) 建立 index.php 然后建立 (wwwroot)/smarty/templates/index.tpl ,代码如下:
index.php:
<?php
// load Smarty library
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = ' d:/inetpub/wwwroot/ smarty/templates';
$smarty->config_dir = ' d:/inetpub/wwwroot/ smarty/config';
$smarty->cache_dir = 'd:/smarty/cache';
$smarty->compile_dir = 'd:/smarty/templates_c';
$smarty->assign('name','fish boy!');
$smarty->display('index.tpl');
?>
index.tpl
<html>
<body>
Hello, {$name}!
</body>
</html>
现在在浏览器中打开 index.php
上面是一个简单的例子,下面建立一个使用class的例子:
smarty_connect.php:
<?php
// load Smarty library
require('Smarty.class.php');
class smarty_connect extends Smarty
{
function smarty_connect()
{
// Class Constructor.
// These automatically get set with each new instance.
$this->Smarty();
$this->template_dir = ' d:/inetpub/wwwroot/ smarty/templates';
$this->config_dir = ' d:/inetpub/wwwroot/ smarty/config';
$this->compile_dir = 'd:/smarty/templates_c';
$this->cache_dir = 'd:/smarty/cache';
$this->assign('app_name', 'Intranet');
}
}
?>
index.php:
<?php
require('smarty_connect.php');
$smarty = new smarty_connect;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
?>
index.tpl:
<html>
<body>
Hello, {$name}!
</body>
</html>
如果出现错误,请察看php.ini文件是否有错误,include是否正确。
注:我自己是把 config templates templates_c 都放到服务器目录下了,而且没有建立 cache 文件夹,程序正常运行。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值