首先下载cakephp和smarty,架好cakephp,关于怎么架设cakephp环境可以参考这里。
然后在vendors目录里建立smarty目录,这里需要说一下cakephp根目录和app目录里的vendors的区别(熟悉cakephp的同学可以跳过),首先vendors目录是用来存放第三方函数库的,因为cakephp有三种安装模式,如果用的是高级模式,就是让多个app共享一个cake库,而且每个app应用都要使用smarty,就可以把smarty放到cakephp根目录的vendors里,反之就可以放到app的vendors目录里。
把下载回来的smarty压缩包里的libs目录放到vendors\smarty目录里。
在app\controllers\components目录里建立smarty.php文件,内容如下: App::import('vendor', 'smarty', array('file' =>
'Smarty.class.php'));
class SmartyComponent extends Smarty { var $controller; var $template_dir; var $compile_dir; var $cache_dir; function __construct() { parent::__construct(); $this->template_dir = VIEWS; $this->compile_dir = TMP . 'smarty' . DS . 'compile' . DS; $this->cache_dir = TMP . 'smarty' . DS . 'cache' . DS; $this->left_delimiter = '< {'; $this->right_delimiter = '}>'; } }
在app/tmp目录下建立smarty目录,并在smarty目录里分别建立cache和compile目录,记得设置权限。
好了,到这里集成基本完成,剩下的就是怎么使用了,这里是一个简单controller的小例子:
class KeywordsController extends AppController { var $name = 'Keywords'; var $components = array('smarty'); //调用smarty function index() { $this->smarty->caching = true; $this->smarty->cache_lifetime = 3600*5; $this->smarty->assign('keywords', $this->Keyword->find('all')); $this->smarty->display('keywords/index.tpl'); //指定view目录中的smarty模版 } function render() {} //防止cakephp设用自己的view来显示页面 }
近期评论