Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[Tutorial] Using Zend_Layout and Zend_View with SilverStripe

Go to End

2 Posts   3910 Views


Community Member, 4 Posts

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.

require_once 'Zend/Layout.php';
require_once 'Zend/View.php';

class Page_Controller extends ContentController
	protected $layout;
	protected $view;

	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' );
		echo $this->layout->render();

- 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 "" for some things. Eg. login screen, forgot password, etc.

To conclude

Please post any questions you might have, as the above is brief and should provide enough guidance for experienced users.


Community Member, 904 Posts

9 March 2010 at 7:54pm

Hi there

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, so that it doesn't get lost in these forums.