17 January 2005 - 23:30PHPTAL and PHP 5
I was going to post an article about how to upgrade to PHP 5, but after removing the PHP 4 port and installing the PHP 5 port all I needed to do was install the corresponding PHP 5 extensions. All of my sites, test code, everything worked. I was truly surprised. The only thing that did not work was my PHPTAL installation. It turns out that due to the differences in how objects and references are handled, PHPTAL version 0.7 no longer works with PHP 5. There is a new version specifically for PHP 5 which is the development version 1.0.0dev7. So I removed the last version and added the new version. It turns out that some stuff is different. This should explain some of the issues that I had to do in order to convert over.
PHP DIRECTIVE
This is probably the biggest change. Previously PHPTAL variables inside a php: directive were accessed with the following syntax: ${value}. This has been changed now to value. Here’s an example:
PHP 4:
<span tal:condition="php: ${object/value} > 5">
PHP 5:
<span tal:condition="php: object.value > 5">
TAL:CONDITION
The tal:condition attribute has changed from before. Unfortunately the inner workings of this tag is not exactly clear to me, but there is one situation that I ran into that does not appear as though it should work. I had an object inside a tal:repeat block where I was checking one of it’s members. I had the following tag:
PHP 4:
<a tal:condition="item/url" tal:attributes="href item/url" tal:content="item/number">Content</a>
For some reason this code just would not work. I was getting strange exception errors that would lead down into the cached PHP file. This file is not documented and very difficult to read. It also has a strange if statement structure that I’ve never seen before. Here’s an example of the if statement:
if (...): ; // Code endif ;
After looking at this, it appears as though item properties are accessed through object->member inside the php code. So the only code that appeared to work, was:
PHP 5:
<a tal:condition="item->url" tal:attributes="href item/url" tal:content="item/number">Content</a>
This actually generated correct code. The generation was:
if ($tpl->item->url): ; $_ATT_href = htmlentities(phptal_path($tpl->item, 'url'), ENT_COMPAT, 'UTF-8') ; ?><a href=" <?php echo $_ATT_href ?>"><?php echo htmlentities( phptal_path($tpl->item, 'number'), ENT_COMPAT, 'UTF-8' ) ?></a><?php endif ;
So as you can see, it’s not entirely consistent. The conditional uses the -> accessor, and the other accesses to the variable are done through the htmlentities() function. This also leads to inconsistent tal code. Hopefully this will be standardized at the next release.
WARNING - Another hazard to avoid is to always place the tal:condition first in the list of tal:attributes. It took me hours of digging to determine that my code was failing because in the above code, I placed the tal:attributes before the tal:condition.
TEMPLATE EXECUTION
The code the execute the template has also changed to reflect PHP’s new addition of exceptions. Here’s an example of a PHP 4 to PHP 5 change.
PHP 4:
require_once("PHPTAL.php");
require_once("PEAR.php");
$template = new PHPTAL("index.html");
$user = new User();
$template->set("title", "Test Page");
$template->set("user", $user);
$res = $template->execute();
if(PEAR::isError($res)) {
echo $res->toString(), "n";
}
else {
echo $res;
}
PHP 5:
require_once("PHPTAL.php");
try {
$template = new PHPTAL("index.html");
$user = new User();
$template->title = "Test Page";
$template->user = $user;
echo $template->execute();
}
catch(Exception $e) {
echo $e;
//print_r($e); // helpful for stack traces
}
CONCLUSION
I must say that even with these few irritations, I still like PHPTAL for the most part. I think I can live for the moment with this being inconsistent, but I would sugguest to stick to PHP 4 and just prepare for these future changes. I think that PHPTAL still holds a position in my list of development tools and I will continue to post updates as I find them.
No Comments | Tags: Programming