Global template


Posted on 1st December 2012 by Jeff Rogers in Mojavi Project


Component Driven Global Template System

PLEASE NOTE: the approach explained below has a few drawbacks, mainly that the execute method of action always need to be executed for the output to appear in a template. I’ve decided to use a different solution to the problem. We will be incorporating online surveys as new versions are created in order to keep the development community engaged in the entire process.

Like many of us I’ve been struggling with a global template approach to Mojavi 2.0 site development for a while. Most of the suggested solutions in the forum still require a fair bit of code duplication, so I tried to come up with my own idea.

I wanted a template “system” which lets me define one or more global templates which integrate the output of multiple actions into one single html page. Mojavi already has the basic tools for this: the ActionChain and Filter classes.

Used Classes

Here is the code to all classes used by my system


/** * This filter implements global templates * Used in combination with the AggregateAction class */class TemplateFilter extends Filter{  function execute (&$filterChain, &$controller, &$request, &$user)  {    static $loaded = false;    if(!$loaded) //only load once!    {      $loaded = true;      //set up main renderer and store it in the request instance      $mainRenderer =& new Renderer($controller, $request, $user);      $request->setAttributeByRef('mainRenderer',$mainRenderer);      //contuinue processing the filter chain and store the output      //in $body      ob_start();      $filterChain->execute($controller, $request, $user);      $body = ob_get_contents();      ob_end_clean();      //mainRendererTemplate will only be set if the execute method      //of the AggregateAction class was executed and there were      //additional actions to be rendererd      if($request->hasAttribute('mainRendererTemplate'))      {        //if the mainRenderer was used, add body to the renderer        //and output to client        $mainRenderer->setTemplate($request->getAttribute('mainRendererTemplate'));        $mainRenderer->setAttribute('body',$body);        $mainRenderer->setMode(RENDER_CLIENT);        $mainRenderer->execute($controller,$request,$user);      }      else //otherwise, just echo the html to the browser      {        echo $body;      }    }    else    {      $filterChain->execute($controller, $request, $user);    }  }}


/** * An easier way to add action chains to Actions */class AggregateAction extends Action{  var $actChain = null;  var $actNames = null;  /**   * Adds an action to the aggregate action chain   *   * @note params like ActionChain::register()   */  function addAction($name,$module,$action,$params = null)  {    if($this->actChain === null)    {      $this->actChain =& new ActionChain();      $this->actNames = array();    }    $this->actChain->register($name,$module,$action,$params);    $this->actNames[] = $name;  }  /**   * Default template name   *   * @return string the template name   */  function getMainRendererTemplate()  {    return 'main.php';  }  function execute(&$controller, &$request, &$user)  {    //only execute this if we render to client    if($controller->getRenderMode() == RENDER_VAR)      return;    $renderer =& $request->getAttribute('mainRenderer');    if(!($this->actChain === null))    {      $this->actChain->execute($controller, $request, $user);       foreach($this->actNames as $name)      {        $renderer->setAttribute($name, $this->actChain->fetchResult($name));      }      $request->setAttribute('mainRendererTemplate',$this->getMainRendererTemplate());    }  }}

MainTemplateAction.class.php (Example template action)

class MainTemplateAction extends AggregateAction{  /**   * Initialize is used to add actions to the action chain   *   * @note It has to return true, otherwise processing will not continue   */  function initialize(&$controller,&$request,&$user)  {    $this->addAction('login','Default','Login');    $this->addAction('nav','Default','MainNavigation');    $this->addAction('cart','Cart','Show');    return true;  }}

That’s all there is to it. The TemplateFilter has to be registered in the global filter chain and all my actions that might at some point be used to output the “body” into the main template extend MainTemplateAction.

The views don’t have to be modified, all the work of getting the renderer output into a global template is handled by the AggregateAction and TemplateFilter.

Setting the global template name in an Action with the getMainRendererTemplate() method is probably not very clean, but it all works like a charm for me and takes away a lot of the hassle of global templating with Mojavi.

The global renderer templates have to go into the YOURAPP/templates directory.

Please feel free to give comments on my approach in this forum topic.

Mojavi 2.0 migration


Posted on 4th October 2012 by Jeff Rogers in Mojavi Project


* File Formats


There are numerous file extension files that are avaiable to help in the migration.