Summing up Smarty and Zend View

Tags:

I’ve posted a couple of posts related to integrating Smarty with the Zend Framework:

  • Making a Smarty view class based on Zend_View
  • Integrating Zend View Helpers into Smarty

I’ve also posted some Zend_View related posts:

  • Using layouts with the Zend ViewRenderer helper
  • Creating a factory-object for building views

Let’s wrap up these in a summary of using them and what else could be done regarding these and the view solution in general.

Views

Let’s first look at the view factory and the layout helper, as they are more useful all-around objects.

The view factory

The view factory is something that can be used to simplify view object creation. It’s useful if you need to create views by hand, for example in view helpers or in the Smarty class which integrates view helpers.

Download the ViewFactory class

It defines some simple methods for creating views:

  • CU_ViewFactory::getInstance() – Returns an instance of the ViewFactory
  • CU_ViewFactory::setConfig(Array $config) – Sets the configuration array used for creating the view. The array is passed to the View class’ constructor.
  • CU_ViewFactory::setViewClass($viewClass) – Sets the name of the View class. It could be Zend_View or SmartyView, for example.
  • CU_ViewFactory::createView() – Returns a new view object

With this class it’s a matter of writing a single line to create a new view after you’ve initialized the factory, and the initialization is very simple:

$viewFactory = CU_ViewFactory::getInstance();
$viewFactory->setViewClass('SmartyView')
                  ->setConfig(array(
                    'compileDir' => './template_c',
                    'helperPath' => './application/views/helpers',
                    'pluginDir' => './plugins',
                    'smartyClass' => 'SmartyAdvanced'
                    ));
 
$view = $viewFactory->createView();

So rather than sprinkling View initialization around your code, you can simply create your configuration in a single place and after that it’s just using createView. You can find some more information in my post Design Patterns: The Factory

Layout ViewRenderer

This was a class I made to get support for simple layouts. So what’s a layout? A layout, in view-terms, is the code which goes “around” your page’s content. For example, the header, the menubar and the footer.

Normally, you would need to include a header and a footer in the view scripts, which violates the DRY principle. So, instead of including a header and a footer, you could have a layout which would just include the content. This is what the ViewRenderer_Layout class does.

Now that Ralph Schindler and Matthew Weier O’Phinney have proposed the Zend_Layout component for the Zend Framework, my class may not be useful anymore in a while. Zend_Layout has the functionality my class provided and some other useful features such as sub requests.

My class on the other hand is very lightweight and easy to use, requiring very little code to use. If you just want to have simple layout support (or want to see how to extend the viewrenderer!), check it out!

Download the ViewRenderer layout class

Its usage is very simple:

$viewHelper = new CU_ViewRenderer_Layout($view);
$viewHelper->setLayoutFileName('layout.phtml');
 
Zend_Controller_Action_HelperBroker::addHelper($viewHelper);

The layout file is just a view script, in your view scripts directory. The Layout renderer will automatically assign the name of the view script to render to the variable $LAYOUT_CONTENT, so you can use it to include the content inside the layout.

Smarty integration

Then, we have the Smarty integration classes.

SmartyView

View engines in the Zend Framework need to be based on the Zend_View_Interface class, and the Zend_View itself is based on Zend_View_Abstract. If you want to use a custom engine, such as Smarty, in ZF, you’ll need to create a new class which is based on either of these.

My SmartyView class does this for you. It’s a simple wrapper for Smarty, to make it work like a Zend_View object does.

Download the SmartyView class

Using the class is very simple. You can for example use the View factory and use setViewClass(‘SmartyView’) to have it use SmartyView. If you’re using the ViewRenderer helpers, you’ll need to pass an instance of the class for the ViewRenderer, or it will use the default view, Zend_View.

There’s also an another good Zend Smarty implementation at Naneau’s blog. The implementation differs slightly from mine, but I don’t really know which one is better/faster.

Smarty and view helpers

I have seen one or two examples of using view helpers in Smarty. Both involved using a Smarty plugin to call the view helpers, so it wasn’t exactly nice, at least in my opinion. The syntax wasn’t very nice for example.

My solution for integrating View helpers in Smarty is a lot better: It extends the base-Smarty and Smarty_Compiler classes to create a simple and easy to use syntax for calling helpers from the templates.

Rather than using {helper helper=helperName param1=”array(something)”}, my code allows you to use syntax like {helperName a.foo=’hello’ a.bar=’world’}. See the post about the solution for more information.

What else?

As far as I know, with this the Smarty-integration is very good. There aren’t any features of Zend_View that you can’t use in a typical simple Smarty-like way.

In general, the Zend_View class is very good. It’s simple to use and easily extendable to support other template engines. With the up and coming Zend_Layout class, we can hope that the Zend Framework gets a high quality solution for doing complex views and layouts. Some other PHP frameworks already come with a solution for that, so it would be about time for Zend to get one of its own.