OPT for Zend Framework
From Invenzzia wiki
| OPT module for Zend Framework | |
| Project | Open Power Template 2 |
| Type | Framework Port |
| Author | Invenzzia Group |
| Website | http://www.invenzzia.org/ |
| License | New BSD License |
| Status | Development |
OPT port for Zend Framework is not only a simple extension of standard Zend_View class that maps their calls to OPT, but a bigger project that aims to replace all the parts of ZF that rely on the view layer. OPT provides its own view rendering solutions and to make a successful integration, entire ZF must be taught, how to use it.
Contents |
Current status
The port for Zend Framework can be obtained from our Subversion repository:
svn co http://svn.invenzzia.org/svn/opl4zf/trunk/
Note that this is a development version. The basic functionality is already implemented, but there is still some work to do.
Elements
All the classes that build the port are stored under the Invenzzia_ namespace. The elements that have been implemented are:
- The replacement for
Zend_Layoutthat uses similar techniques, but is developed to work withOpt_Viewclass. - Extension of
Zend_Formthat allows to render the ZF forms to OPT templates using components and sections. - Wrapper component for the Zend_Form elements.
- Extension of
Zend_Mailthat allows to render OPT views as the mail body. - Router integration and a wrapper to call Zend helpers directly in the templates.
Requirements
- Zend Framework 1.7
- OPL + OPT (the newest available version)
Installation
Download the ZF port source code from SVN repository, Zend Framework and OPL. Copy the files to the project directory structure in the following way:
/app
your application files
/lib
/Invenzzia
ZF Port files
/Opl
/Opl
OPL core files
/Opt
OPT files
/Zend
Zend Framework files
Basic usage
The SVN repository contains a sample application with the comments.
First, you need to configure the autoloaders:
require('Zend/Loader.php');
require('Opl/Opl/Base.php');
// It is recommended not to use include_path with OPL
Opl_Loader::setDirectory(ROOT_DIR.'lib/Opl/');
Opl_Loader::register();
Zend_Loader::registerAutoload('Zend_Loader');
The configuration of the front controller and the Invenzzia_Layout looks like this:
// Create the improved response.
$response = new Invenzzia_Controller_Response_Http;
$front->setResponse($response);
// Do not forget to disable the default view renderer or we'll get a mess.
$front->setParam('noViewRenderer', true);
$front->setParam('disableOutputBuffering', true);
// Note that OPT is initialized by Invenzzia_Layout, if you haven't done
// it manually.
$layout = Invenzzia_Layout::startMvc(array('compileMode' => Opt_Class::CM_REBUILD, 'stripWhitespaces'=> false));
$layout->setViewPaths(ROOT_DIR.'app/views/', ROOT_DIR.'cache/');
// Connect the layout to the Zend response.
$layout->setOutput($response);
// Set the layout: layout.tpl
$layout->setLayout('layout');
Invenzzia_Layout creates separate Opt_View objects for each dispatched action. By default, the view points to a template file controller/action.tpl in your template directory, but you can easily choose your own name with setTemplate() method. The views are assigned to one or more placeholders - by default it is called content. To assign the view to a different placeholder, use the following code somewhere in your action:
// Appending
$this->layout->appendView($this->view, 'anotherPlaceholder');
// Or prepending
$this->layout->prependView($this->view, 'anotherPlaceholder');
Invenzzia_Layout creates also a layout view that can be always obtained with:
$layout = Invenzzia_Layout::getMvcInstance();
$layout->getLayout();
You can assign there some layout-specific template variables etc. Moreover, Invenzzia_Layout registers there the placeholder view lists which are available as simple sections. You can render the views in a particular placeholder in this way:
<!-- dispatch "content" placeholder -->
<opt:show name="content">
<div id="content">
<h3>Content placeholder</h3>
<opt:section>
<opt:include from="content">
<p>Sorry, no template was found!</p>
</opt:include>
</opt:section>
</div>
</opt:show>
Please note that your controllers must extend Invenzzia_Controller_Action class in order to work properly.
Documentation
Currently, the code is documented with phpdoc, so a good IDE should inform you about everything. The detailed manual will be published in TypeFriendly and a tutorial will appear.
Future
The plans for the future:
- After releasing Open Power Forms the extension of
Zend_Formwill be abandoned. However, please note that this is more far than near future :).

