3314 Posts in 890 Topics by 835 members
|Go to End|
9 March 2010 at 12:57pm
Over the last year or so, I've noticed many comments about the SilverStripe template language being unnecessary, or people having issues with it (trying to gain more flexibility).
It is actually fairly easy to use an alternate templating system, and I felt I would post a brief tutorial on how to use Zend_Layout and Zend_View with SilverStripe.
- Custom code in an "/application" subdirectory.
1. Setup Zend Framework
- Download and place Zend Framework to a directory (eg. /_lib/). It's important to prefix with an underscore so SilverStripe's ManifestBuilder skips it.
2. Include Zend Framework in include path
- In your "_config.php" file for your application, be sure to include ZF. Example:
$zend_lib_path = realpath( '..' . DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR . '_lib';
set_include_path( $zend_lib_path . PATH_SEPARATOR . get_include_path() );
3. Extend ContentController with a custom Page controller
- Usually, most people create a "Page" class that extends "SiteTree", along with a "Page_Controller" that extends "ContentController". This is pretty standard, however we want to override a particular method called "handleAction()" to perform our integration.
class Page_Controller extends ContentController
public function __construct( $dataRecord = null )
parent::__construct( $dataRecord );
public function index()
private function initLayout()
$view = new Zend_View();
$view->setScriptPath( '../application/_views/scripts' );
$layout = new Zend_Layout();
$layout->setView( $view );
$this->view = $view;
$this->layout = $layout;
public function handleAction( $request )
$this->action = str_replace( '-', '_', $request->param( 'Action' ) );
$this->requestParams = $request->requestVars();
if ( !$this->action )
$this->action = 'index';
$controller = $this->ClassName;
$action = $this->action;
// run & init are manually disabled, because they create infinite loops and other dodgy situations
if ( in_array( strtolower( $action ), array( 'run', 'init' ) ) || !$this->hasMethod( $action ) )
$this->$action( $request );
$this->layout->controller = $controller;
// Assign all ContentObject values to view
$this->view->object = $this->dataRecord;
// Default view handling
$this->layout->content = $this->view->render( $controller . DIRECTORY_SEPARATOR . $action . '.phtml' );
- Above you will see that we hijack SilverStripe's "handleAction" method to use Zend_Layout and Zend_View for template processing.
- To make things easy, we assign the current data record to a Zend_View object called "object".
- By default, the current controller action will attempt to load a view from /_views/scripts/[CONTROLLER]/[ACTION].phtml This is similar to how Zend Framework handles things by default. For instance, for the "index" action for "Page", it would load: /_views/scripts/Page/index.phtml
4. Start using Zend_Layout and Zend_View
- The basics are now all in place and you may setup your templates according to standard Zend_View conventions, using helpers and all.
- Extend this "Page" class and "Page_Controller" with your own custom pages to use the Zend templating.
- Note: Several core SilverStripe pages will not use this, so you will still need to use a "Page.ss" for some things. Eg. login screen, forgot password, etc.
Please post any questions you might have, as the above is brief and should provide enough guidance for experienced users.
9 March 2010 at 7:54pm
Thanks for this. It's always good to see how to integrate other systems into SilverStripe for more flexibility.
Personally I have also used Zend Framework professionally, although I wasn't too fond of Zend_View myself.
You're right however, there have been comments about the lack of flexibility in the SilverStripe template-engine, so these people now have an alternative. Never thought it would be that easy to implement ;-)
I suggest you put this up on http://www.ssbits.com, so that it doesn't get lost in these forums.
|Go to Top|