Welcome to TheGillis.net

Consider this site a collection of random notes about a variety of topics. I hope this information helps you in some way.

18 January 2005 - 0:03PHPTAL Trick

In my last article I described a situation where a tal:condition attribute would not effectively determine the value of a member variable when it was NULL. I was treating the condition as a normal if statement where I had something similar to tal:condition=”object/property”. When property was set to null, an exception was thrown with the message “Path not found: property”. I came up with a solution that worked, but it appears that it’s a loop hole in PHPTAL. This is what I’ve found.

PROBLEM

It turns out, that inside the PHPTAL cache file, the condition statement is transformed as follows:

... tal:condition="item/url" ...

Into:

...
if (phptal_path($tpl->item, 'url')):  ;
...

It was this function, phptal_path() that was throwing the exception. After breaking down this function, there are two important things that are going on. On line 364, it accurately determines that item is a valid object. The next set of if statements determine the type of property or method and then return the value from it when it’s found. In our case, it does not recognize it as a property or method and falls through all if statements into the exception throw. This is where the solution becomes apparent. To determine if the value is a property, the isset() method is used. This is our problem because according to the PHP documentation, the isset() method evaluates false for NULL values.

CONCLUSION

The best way to describe our solution last time of using the -> accessor, is a loop hole. What is most likely occurring is the -> is not recognized as a valid character, so the contents are dumped to the if statement. This gives us:

... tal:condition="item->url" ...

Into:

...
if ($tpl->item->url):  ;
...

Since this does not appear to be supported as the way to access a property in PHPTAL, our only solution is to place a boolean flag in our object that determines if the url object is null (should not be used). Hope this helps. Any comments are welcome.

No Comments | Tags: Programming

Add a Comment