Using PHPTAL for templates: PhptalView class

by dkvasnicka
I use PHPTAL quite a lot (believe it or not, I like XML ;) and I wanted to use it in Cake so I wrote a little PhptalView class extending Cake's View.
OK folks, it's pretty straightforward.

  • Make sure you use PHP 5. PHPTAL is working only with PHP 5 (well, there are some really old versions that work with PHP 4, but as the author of PHPTAL has said: "Please forget PHP4" :-)
  • Download PHPTAL from http://phptal.motion-twin.com/ and unpack it in /app/vendors. My class will look for PHPTAL-1.1.8 directory, as this is the last version of PHPTAL. It's easy to customize the dirname by changing the vendor() statement. It should be possible to use PHPTAL from PEAR too. Try installing PHPTAL into your PEAR and load it wit plain old require().
  • Download the snippet from CakeForge or from the second page of this article: http://cakeforge.org/snippet/detail.php?type=snippet&id=180
  • Place it in your /app/views directory
  • In your controller, set view to Phptal: or whatever PHPTAL syntax you use for printing out variables. Otherwise PHPTAL will escape your View's rendering output.
  • Assign variables as usual, through Cake's set() method.
  • Enjoy the immense elegance of PHPTAL :-)

You are encouraged to improve the class any way you want. This is just what I have baked for my needs.


Component Class:

<?php 
<?php
/**
 * Methods for displaying presentation data using PHPTAL (http://phptal.motion-twin.com/)
 *
 *
 * PHP 5 ONLY !!!!
 *
 * Copyright (c) 2007, Daniel KvasniÄ?ka
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright        Copyright (c) 2007, Daniel KvasniÄ?ka
 * @link            
 * @package        cake
 * @subpackage    cake.app.views
 * @version        0.1
 * @lastmodified    $Date: 2006-12-03 23:29:12 +0100 $
 * @license        http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Included libraries.
 */
vendor('PHPTAL-1.1.8'.DS.'PHPTAL');

/**
 * PHPTALView, the V in the MVC triad, made the PHPTAL way.
 *
 * Class holding methods for displaying presentation data.
 *
 * @package            cake
 * @subpackage        cake.app.views
 */
class PhptalView extends View {

    
/**
     * PHPTALView constructor
     *
     * @param  $controller instance of calling controller
     */
    
function __construct(&$controller) {
        
parent::__construct($controller);
        
        
$this->ext ".zpt";
        
$this->template = new PHPTAL();
    }

/**
 * Renders and returns PHPTAL template for given view with its array of data.
 *
 * @param string $___viewFn Filename of the view
 * @param array $___dataForView Data to include in rendered view
 * @return string Rendered output
 * @access protected
 */
    
function _render($___viewFn$___dataForView$loadHelpers true$cached false) {
        if (
$this->helpers != false && $loadHelpers === true) {
            
$loadedHelpers = array();
            
$loadedHelpers $this->_loadHelpers($loadedHelpers$this->helpers);

            foreach(
array_keys($loadedHelpers) as $helper) {
                
$replace strtolower(substr($helper01));
                
$camelBackedHelper preg_replace('/\\w/'$replace$helper1);

                ${
$camelBackedHelper} =& $loadedHelpers[$helper];

                if (isset(${
$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) {
                    foreach(${
$camelBackedHelper}->helpers as $subHelper) {
                        ${
$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
                    }
                }
                
$this->loaded[$camelBackedHelper] = (${$camelBackedHelper});
            }
        }
        
        
$this->template->setTemplate$___viewFn );
        
        foreach(
$___dataForView as $data => $value)
        {
            
$this->template->set($data$value);
        }        
        
        try {
            return 
$this->template->execute();
        } catch (
Exception $e){
            return 
"<pre>".$e->__toString()."</pre>";
        }
    }
}
?>
?>

1 | 2

Report

More on Components

Advertising

Comments

  • csotelo posted on 06/09/10 03:25:25 AM
    Hello
    I have gotten phptal work but, how about get working on theme mode, there are a way to do it, or some of your get it working php tal ona theme way. Thanks
  • frink posted on 05/21/07 12:52:53 AM
    I may be stupid but I've not been able to get helpers working. Any ideas...?
    • dkvasnicka posted on 05/24/07 07:12:58 AM
      I may be stupid but I've not been able to get helpers working. Any ideas...?
      I managed to get helpers working in layouts:

      <tal:block replace="structure php:html.css('style')" />
      However, they don't work in views, probably because Cake's default "templates" (= PHP code) are processed using includes and within the scope where helpers ale loaded. I guess Smarty people might have solved a similar problem. Maybe we could learn from them.
  • frink posted on 05/20/07 05:08:29 PM
    I had a relative path issues upon install. I resolved it by adding the following like to app/vendors/PHPTAL-1.1.8/PHPTAL.php on line 64:

    set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__));

    This will add PHPTAL-1.1.8 directory to the include path and fix the relative include incompatibility...
    • dkvasnicka posted on 05/24/07 07:17:55 AM
      I had a relative path issues upon install. I resolved it by adding the following like to app/vendors/PHPTAL-1.1.8/PHPTAL.php on line 64:

      set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__));

      This will add PHPTAL-1.1.8 directory to the include path and fix the relative include incompatibility...

      I had a similar problem and I solved it by moving PHPTAL.php and PHPTAL directory out of PHPTAL-1.1.8 dir.
login to post a comment.