Jump to:

3431 Posts in 1025 Topics by 864 members

Template Questions

SilverStripe Forums » Template Questions » [Tutorial] Using Zend_Layout and Zend_View with SilverStripe

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

Page: 1
Go to End
Author Topic: 3280 Views
  • Christopher_S
    Avatar
    Community Member
    4 Posts

    [Tutorial] Using Zend_Layout and Zend_View with SilverStripe Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: [Tutorial] Using Zend_Layout and Zend_View with SilverStripe Link to this post

    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 http://www.ssbits.com, so that it doesn't get lost in these forums.

    3280 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.