- 论坛徽章:
- 0
|
Zend Framework’s View class has very bad capability for extending. It contains template variables but does not allow to access them, it has array with different pathes (templates, filters), but does not allow to add another type or access them. Therefor only way to use Smarty with Zend Framework is to abandon Zend_View and manipulate Smarty object directly.
First we need to create Smarty object. I do it in index.php after including Zend.php:
- <?php
-
- require('Zend.php');
-
- include 'smarty/Smarty.class.php';
- $smarty = new Smarty();
- $smarty->debugging = false;
- $smarty->force_compile = true;
- $smarty->caching = false;
- $smarty->compile_check = true;
- $smarty->cache_lifetime = -1;
- $smarty->template_dir = 'resources/templates';
- $smarty->compile_dir = 'resources/templates_c';
- $smarty->plugins_dir = array(
- SMARTY_DIR . 'plugins',
- 'resources/plugins');
-
- ?>
复制代码
I don’t like global variables therefor I’ve added Smarty object into Zend Framework’s registry:
- <?php
-
- Zend::register('smarty', $smarty);
-
- ?>
复制代码
Using is pretty simple. Just initialize Smarty variables in your Controller class and display template:
- <?php
-
- class IndexController extends Zend_Controller_Action
- {
- function index()
- {
- $smarty = Zend::registry('smarty');
- $smarty->assign('title', 'Test');
- $smarty->display('index.tpl');
- }
-
- function noRoute()
- {
- $smarty = Zend::registry('smarty');
- $smarty->display('error404.tpl');
- }
- }
-
- ?>
复制代码
As you can see Smarty integration with Zend Framework is very simple task. Zend_View has ability to use your own filters and helper functions, but with Smarty you don’t need them because Smarty has its own plugins, filters, modifiers. Just forget about Zend_View and use best template engine for PHP in the world!
[ 本帖最后由 HonestQiao 于 2006-5-23 13:47 编辑 ] |
|