Home | Download | Quick Start | Documentation | Speed Test | Forum

Rain Tpl

The easy template engine for PHP, RainTPL is the faster template engine.
  • Quick, loads precompiled template
  • Easy, 8 tags, 3 methods, 1 file/class
  • Useful, divides logic by graphic


Join the Google Group
rain google group
17 February 2011

Integrating Rain TPL into Zend

Using HTML templates instead of PHP templates has many advantages, the code is more readable, if you work in a team, designers will love you, and you'll have more time to focus on your PHP coding.

Integrate Rain in Zend, takes few minutes and a couples of copy / paste operations,
nothing more simple than this.



Let's start:


1. First of all you must have installed Zend Framework and you must have created your project. In my case I installed Rain on a "quickstart" project.


2. Create 2 new directories, application/cache/ and application/library/.


3. Copy the rain.tpl.class.php file into application/library/.


4. Create a Raintpl_View.php file, inside the application/library/ , here is the code:


Raintpl_View.php

require_once 'Zend/View/Abstract.php';

class Raintpl_View extends Zend_View_Abstract {

  private $_raintpl;

  public function __construct($data) {
    parent::__construct($data);

    require_once "rain.tpl.class.php";

    $this->_raintpl = new raintpl();
    raintpl::$tpl_dir = $data['tpl_dir'];
    raintpl::$cache_dir = $data['cache_dir'];
  }

  public function getEngine() {
    return $this->_raintpl;
  }

  public function __set($key, $val) {
    $this->_raintpl->assign($key, $val);
  }

  public function __get($key) {
      return isset( $this->_raintpl->$var[$key] ) ? $this->_raintpl->$var[$key] : null;
  }

  public function __isset($key) {
    return $this->_raintpl->get_template_vars($key) != null;
  }

  public function __unset($key) {
    $this->_raintpl->clear_assign($key);
  }

  public function assign($spec, $value=null) {
    if (is_array($spec)) {
      $this->_raintpl->assign($spec);
      return;
    }
    $this->_raintpl->assign($spec, $value);
  }

  public function clearVars() {
    $this->_raintpl->$var=array();
  }

  public function render($name,$return_string=false) {
      if( $ext = strrchr($name, '.') )
          $name = substr($name, 0, -strlen($ext)); 
    return $this->_raintpl->draw($name,$return_string);
  }

  public function _run() {

  }

}



5. Update application/config/application.ini, by adding few settings for RainTPL:

raintpl.tpl_dir   = "../application/views/scripts/"
raintpl.cache_dir    = "../application/cache/"




6. Open your application/Bootstrap.php file, and overwrite the method _initView(), as follow:

Bootstrap.php

 

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
   
    protected function _initView(){

        require_once 'library/Raintpl_View.php';
        $options = $this->getOption('raintpl');
        $view = new Raintpl_View($options);
        $viewRender = Zend_Controller_Action_HelperBroker::getStaticHelper(
        'ViewRenderer'
        );
        $viewRender->setView($view);
        $viewRender->setViewSuffix('html');
        Zend_Controller_Action_HelperBroker::addHelper($viewRender);
        return $view;

    }

}



7. Now you've to convert the PHP templates as HTML templates. You could just copy paste them and rename the extension to .html. The conversion is simple, in Zend you  access variables with $this->variable_name, instead in Rain you access to variables with {$variable_name}, so for example:

<?php echo $this->title; ?>   will become   {$title}

If, and foreach will become {if} and {loop} tags, functions become {function} tag. Take a look to the RainTPL documentation.



8. Now you can use the View in your controller, for example, you can edit the IndexController as follow:

public function indexAction(){
        Zend_Loader::loadClass('Zend_View');
        $view = new Zend_View();
        $view->setScriptPath( APPLICATION_PATH . '/views/script');
        echo $view->render('list.html');
}

Obviusly  you have to create a list.html template into application/views/scripts.


9. Done! Now test your application.



For any question please write into the raintpl google group.