Zend Framework: Using Smarty as template engine

Posted by Dmytro Shteflyuk on under PHP

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
Zend::register('smarty', $smarty);

Using is pretty simple. Just initialize Smarty variables in your Controller class and display template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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!

Books recommended

54 Responses to this entry

Subscribe to comments with RSS

zendrej
said on March 10th, 2009 at 12:56 · Permalink

I could not find ‘zend.php’ and failed to include this. I would be thankful if anyone can help me

The below index.phtml is not rendering values too

1
<?=$this->val?>

Regards,
rej

said on March 17th, 2009 at 19:29 · Permalink

This is a little outdated…

It works… but instead of using Zend::register(‘smarty’, $smarty) you should use Zend_Registry::set(‘smarty’, $smarty) and instead of using Zend::registry(‘smarty’) you should use Zend_Register::get(‘smarty’). And ofcourse you must: require_once ‘Zend/Registry.php’ in the bootstrap.

said on March 17th, 2009 at 19:33 · Permalink

I’m against this method of incorporating smarty into ZF. This method assumes you will be rendering a smarty view with each action.

Also… this will produce an error with current versions on ZF that automatically render a view at the end of an action. With this method you must call $this->_helper->viewRenderer->setNoRender(true) in every action to disable zend from attempting to render a default view after the smarty display.

More comments: 1 2

Comments are closed

Comments for this entry are closed for a while. If you have anything to say – use a contact form. Thank you for your patience.