Just got Zend Framework 1.0.1 and Smarty to work together!!!
ain't that easy...
here's what i did:
- get familiar with zend framework (zf) MVC. sure is complicated!!
- look at http://devzone.zend.com/node/view/id/120 and it also has some nice comments.
- look at http://www.kitpages.fr/zf_integrerSmarty.php (google can translate this and the code is very readable)
- bear with: http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer
- i got a zf tutorial up and running:
http://akrabat.com/zend-auth-tutorial/ - now, the zf / smarty integration
- Create smarty directory under my root with cache, config, and template_c dirs ( same level as app, library, …)
- Add smarty lib to include path: . PATH _SEPARATOR . './library/smarty/'
- Create a file and load smarty lib code from http://framework.zend.com/manual/en/zend.view.scripts.html#zend.view.scripts.templates: require_once('zend_view_smarty.php');
- Follow: http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrendererExample 7.10. Changing the path specifications. see my bootstrap code below
- Controller: render normally
- Both auto-rendering and explicitly calling $this->render(); worked.
- View: create smarty .tpl files in same place zend views would go.
- In .tpl, use {$var_name} to get vars that are set.
- tried it and it works!
- Still need to figure out how to call my own php functions in smarty templates (ie: view helpers)
here's the code in my bootstrap:
// Different view implementation
//$view = new ZF_Smarty();
$smarty_config = array(
'compile_dir' => './smarty/templates_c/',
'config_dir' => './smarty/configs/',
'cache_dir' => './smarty/cache/',
'debugging' => true,
);
$view = new Zend_View_Smarty('./application/views/scripts/', $smarty_config);
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewRenderer->setViewBasePathSpec('./application/views/scripts/')
->setViewScriptPathSpec(':controller'.DIRECTORY_SEPARATOR.':action.:suffix')
->setViewScriptPathNoControllerSpec(':action.:suffix')
->setViewSuffix('tpl');
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
- i still got a warning from a zend framework lib. i patched my version of zend lib that has my Zend\Controller\Action\Helper\ViewRenderer.php so a warning does not appear:
- $currentPaths is assumed to be an array by the foreach loop. but this variable can be a string also, in which case the foreach loop emits a warning
- in initView(), add:
$currentPaths = array_merge( (array) $currentPaths); - if this was helpful to you, leave a comment (especially if you find any issues with this approach!)
- oops, i just found a couple of posts that use a similar process: http://naneau.nl/2007/05/31/using-naneau_view_smarty-with-rc1 and http://my.opera.com/zomg/blog/2007/07/31/smarty-zend-view-take-three (not sure which version of zf this uses). well, it's good to have confirmation!
- 9/7/07: found another smarty integration eg: http://framework.zend.com/wiki/display/ZFUSER/Kevin+Vaughan
- Oh, did you want unit tests with your Zend MVC?? That will be extra... check out http://www.nabble.com/How-to-use-PHPUnit-in-a-MVC-project-tf3965725s16154.html#a12396382 yeah, works with smarty too!
- 10/15/07: what is the performance of zend framework with smarty? I modified a version of this smarty setup and found it to be about the same as http://www.nabble.com/Re%3A-ZF-performance-advice-p13185506s16154.html That is, it generates pages in about 0.4 sec with some spikes higher.
- 11/22/07: see this bug fix: http://framework.zend.com/issues/browse/ZF-1901