参考文章:http://blog.csdn.net/kunshan_shenbin/article/details/6212529
cakephp版本:1.3.14
smarty版本:3.1.7
步骤如下:
1. 在app/vendors下新建smarty文件夹,然后把下载下来的smarty包的libs下的文件拷贝进去。
2. 在app目录下新建app_controller.php,代码如下:
class AppController extends Controller {
var $view = 'Smarty';
}
3. 在app/views下新建smarty.php,代码如下:
App::import('Vendor', 'Smarty', array('file' => 'smarty'.DS.'Smarty.class.php'));
class SmartyView extends View {
function __construct (&$controller) {
parent::__construct($controller);
$this->Smarty = &new Smarty();
$this->ext= '.tpl';
$this->Smarty->compile_dir = TMP.'smarty'.DS.'compile'.DS;
$this->Smarty->cache_dir = TMP.'smarty'.DS.'cache'.DS;
$this->Smarty->template_dir = VIEWS.DS;
}
function _render($___viewFn, $___data_for_view, $___play_safe = true, $loadHelpers = true)
{
foreach($___data_for_view as $data => $value)
{
if(!is_object($data))
{
$this->Smarty->assign($data, $value);
}
}
$this->Smarty->assignByRef('this', $this);
return $this->Smarty->fetch($___viewFn);
}
}
4. 在app/views/layouts下新建default.tpl文件,代码如下:
Layout:
<br />
<br />
{$content_for_layout}
接下来我们可以使用smarty来编写测试代码:
在app/controllers下新建tests_controller.php,代码如下:
class TestsController extends AppController {
//var $view = 'Smarty';
var $uses = null;
function index() {
$name = 'Shen Bin';
$date = date("Y-m-d H:i", time());
$this->set(compact('date', 'name'));
}
}
在app/views/tests下新建index.ctp,代码如下:
{$date}
<hr />
Hello, {$name}!