Using Naneau_View_Smarty with RC1

by Naneau

The new viewRenderer, introduced with the Zend Framework 1.0.0RC1 is a source of confusion for a lot of us. When I first upgraded I couldn’t figure out what on earth it was trying to do. Luckily, Matthew Weier O’Phinney has written an article over on devzone that explains the basics of it. I now understand the idea behind it and I am convinced that it can be handy. The migration guide also has some good tips.

Whether or not it should be enabled by default is another matter though. There have been quite a few debates over at #zftalk about how it breaks compatibility with pretty much everything. While this is to be expected in the days before 1.0, it would have been nice to not have to disable it to get your scripts to work.

At any rate. A while ago I wrote about using Smarty with the Zend Framework. While my implementation was solid and easy, it will not work with the viewRenderer. First of all, it relied on manually rendering the view, not using $this->render() from your controllers. That is now considered obsolete.

So, how do you get it to work then? Well, you have to tell viewRenderer to use Naneau_View_Smarty, and maybe tell it to search for files with a .tpl extension (that’s not required, but Smarty template files traditionally end in .tpl). In your bootstrap you can do this like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * Naneau_View_Smarty
 */

require_once 'Naneau/View/Smarty.php';

$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView(new Naneau_View_Smarty(array(
'compileDir' => COMPILE_DIR)
//replace COMPILE_DIR with a  directory your webserver has write access to
));
//make viewRenderer use Naneau_View_Smarty

$viewRenderer->setViewSuffix('tpl');
//make it search for .tpl files

Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
//add it to the action helper broker

That should do it for the setup. Now, I have also made a tiny change to Naneau_View_Helper, to allow viewRenderer to tell it where to look for templates. This was suggested by Ralph Schindler, and I’m happy he did, because it made the code a bit shorter. I scrapped the setScriptPath() method and updated the _run() to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    /**
     * fetch a template, echos the result,
     *
     * @see Zend_View_Abstract::render()
     * @param string $name the template
     * @return void
     */

    protected function _run()
    {
        $this->strictVars(true);

        $vars = get_object_vars($this);
        foreach ($vars as $key => $value) {
            if ('_' != substr($key, 0, 1)) {
                $this->_smarty->assign($key, $value);
            }
        }
        //assign variables to the template engine

        $this->_smarty->assign_by_ref('this', $this);
        //why 'this'?
        //to emulate standard zend view functionality
        //doesn't mess up smarty in any way

        $path = $this->getScriptPaths();
       
        $file = substr(func_get_arg(0), strlen($path[0]));
        //smarty needs a template_dir, and can only use templates,
        //found in that directory, so we have to strip it from the filename

        $this->_smarty->template_dir = $path[0];
        //set the template diretory as the first directory from the path

        echo $this->_smarty->fetch($file);
        //process the template (and filter the output)
    }

At any rate, download Naneau_View_Smarty 0.2 and check it out.