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.
Assumptions:
- 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 );
$this->initLayout();
}
public function index()
{
}
private function initLayout()
{
$view = new Zend_View();
$view->setScriptPath( '../application/_views/scripts' );
$layout = new Zend_Layout();
$layout->setLayoutPath('../application/_layouts/scripts');
$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 ) )
{
return;
}
$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();
exit;
}
}
- 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.
To conclude
Please post any questions you might have, as the above is brief and should provide enough guidance for experienced users.