Smarty使用手记

简介: Smarty是Php下的模板引擎技术,和apache的java 模板技术velocity原理一样,只是velocity适用于java 语言,而Smarty适用于php语言。 首先从http://www.smarty.net/下载smarty,目前最新版本是3.1rc1。
Smarty是Php下的模板引擎技术,和apache的java 模板技术velocity原理一样,只是velocity适用于java 语言,而Smarty适用于php语言。
首先从 http://www.smarty.net/下载smarty,目前最新版本是3.1rc1。我们将其中的libs目录解压到web默认目录。smarty的配置还是很简单的。
首先需要建立四个目录,templates,templates_c,cache和configs。
接着编写一个测试用php,我暂且命名为smartytest.php
  1. ?php
  2. require('libs/smarty.class.php');
  3. $smarty = new Smarty();
  4. $smarty->setTemplateDir('templates');
  5. $smarty->setCompileDir('templates_c');
  6. $smarty->setCacheDir('cache');
  7. $smarty->setConfigDir('configs');
  8. $smarty->assign('name','Ned');
  9. $smarty->display('smartytest.tpl');
  10. ?>
接着编辑需要的模板文件,模板文件需要放在templates下,暂且命名为smartytest.tpl。
  1. html>
  2.     head>
  3.     title>Smarty Test/title>
  4.     /head>
  5.     body>
  6.     Hello,{$name}!
  7.     /body>
  8. /html>

这样我们浏览smartytest.php。将会看到页面输出Hello,Ned!。

如果PHP安装有问题,可以使用$smarty->testInstall();进行测试。

注意在Linux下,需要将template_c和cache目录的用户和组修改为nobody,并置权限为rwxrwxrx。

Smarty的官方文档参见http://www.smarty.net/documentation,最新版本有日文版文档,但没有中文的。

目录
相关文章
|
PHP 前端开发 网络架构
|
PHP 搜索推荐 数据库
|
PHP 前端开发
**【ci框架】PHP的CI框架集成Smarty的最佳方式
因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足。 本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子 http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345。
1028 0
|
缓存 前端开发 PHP
EcShop之路-Smarty
来源:http://www.cnblogs.com/dequan-lee/archive/2009/08/06/1540621.html 目前市面上有很多针对PHP的MVC模板,但Smarty无疑是在功能和速度上处于绝对领先地位的。
904 0
|
PHP Apache 缓存
yaf(5) smarty
2013年4月6日 13:41:37 参考: http://www.oschina.net/question/812776_71817 http://yaf.laruence.com/manual/yaf.
876 0