Using PHPTAL for templates: PhptalView class

By Daniel Kvasnicka jr. (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.

Page 2: PhptalView source code

Comments 351

CakePHP Team Comments Author Comments
 

Bug

1 Relative Path Issues

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...
Posted May 20, 2007 by Micon Frink
 

Comment

2 Using Helpers

I may be stupid but I've not been able to get helpers working. Any ideas...?
Posted May 21, 2007 by Micon Frink
 

Comment

3 Helpers

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.
Posted May 24, 2007 by Daniel Kvasnicka jr.
 

Comment

4 Relative Path

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.
Posted May 24, 2007 by Daniel Kvasnicka jr.