Summing up Smarty and Zend View
November 8, 2007 – 1:34 pm Tags: PHP, Zend FrameworkI’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.
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.
RSS feed:Subscribe!












9 Responses to “Summing up Smarty and Zend View”
Hi,
How do I change the ViewSuffix to .tpl using your class?
-or-
How do I intergrate your SmartyAdvanced class with Naneau’s class?
By Jens Chr on Nov 17, 2007
I assume you mean changing the view suffix with the layout renderer…
You can just use the setViewSuffix method, for example $viewHelper->setViewSuffix(’tpl’)
Integrating SmartyAdvanced with Naneau’s class should be pretty simple. I don’t have the source code here right now, but I recall he simply has a line in it which says new Smarty(). Replace that with new SmartyAdvanced() and you should be all set.
By Jani Hartikainen on Nov 17, 2007
I tried to change the classname to “SmartyAdvanced in Naneau’s class, but I get the following error:
Fatal error: Call to a member function getHelper() on a non-object in /lib/Smarty/SmartyAdvanced.php on line 23
By Jens Chr on Nov 17, 2007
Ah yes, you will need to use the setZendView method in the SmartyAdvanced class before it can call any view helpers.
Add something like this to the construct method in naneau’s view class and it should work I think:
$smarty->setZendView($this);
By Jani Hartikainen on Nov 18, 2007
I’m not using the layout renderer. I’m using this instead:
public function dispatchLoopShutdown()
{
$response = $this->getResponse();
$view = Zend_Controller_Action_HelperBroker::getStaticHelper(’viewRenderer’)->view;
$response->prepend(’header’,$view->render(’header.tpl’));
$response->append(’footer’,$view->render(’footer.tpl’));
}
By Jens Chr on Nov 18, 2007
Thank you, it worked:)
By Jens Chr on Nov 18, 2007
Wow. This is good stuff.
I’ve been beating my head against trying to find a way to implement Smarty in ZF for about a week until I stumbled on this.
One question tho, I noticed that in SmartyAdvanced_Compiler{} that I needed to re-set the helper path in the constructor?
public function __construct() {
parent::__construct();
$this->_zendView = new Zend_View();
$this->_zendView->addHelperPath(’blah’);
}
Before it would find my helpers. now I’m stuck with ‘Call to a member function getHelper() on a non-object ‘ like what’s mentioned above. But I do set the view right after the factory creates the object:
$view = $viewFactory->createView();
$view->addHelperPath(’blah’);
$viewHelper = new Zend_Controller_Action_Helper_ViewRenderer();
$viewHelper->setView($view);
Shoud work i think, but it’s not. Any suggestions?
By Jeff on Mar 1, 2008
Thanks,
It is working. But If move or rename the folder of View Controlle, It is not working
Vijay
By Outsourcing India on Dec 1, 2008
Hi,
Thanks for this Layout integration. But how to disable the layout when i dont want use the layout ?
With Zend_Layout, you can do this $this->_helper->layout->disableLayout(); in barAction() for exemple but with your integration its not possible !
So, how to do that ?
thanks
By Anonymous on Dec 2, 2008