Using PHPTAL for templates: PhptalView class
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.
You are encouraged to improve the class any way you want. This is just what I have baked for my needs.
- 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.
Comments
Bug
1 Relative Path Issues
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...
Comment
2 Using Helpers
Comment
3 Helpers
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.
Comment
4 Relative Path
I had a similar problem and I solved it by moving PHPTAL.php and PHPTAL directory out of PHPTAL-1.1.8 dir.