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.

12 September 2004 - 18:24PHPTAL

When comparing PHP to JSP, I believe that JSP’s are the better solution in most situations. Although some may disagree, JSP and Servlets make it easier to scale your project vertically. With PHP you can add more pages to scale horizontally, but when scaling vertically, the crazy web of includes and the data types that are carried through all the files make a project quickly unruly. Since I am forced to develop in PHP on some system setups, I have recently learned about a new project PHPTAL. This allows your PHP project to get one more leg up on the vertical scalability pole by separating all of the business logic from the web interface. It also keeps the web interface tag-free and fully XML compliant.

Intro

Although PHPTAL is fairly complex, the main idea is that an XHTML file is written with test data entered in it, and then special attributes are added from the tal namespace to signal that certain HTML attributes or data should be replaced by PHP values. A very helpful example is shown on the PHPTAL documentation site.

So, we have our UI file with special tal namespace and our PHP file that sets up the data and parses the UI html. There are some pretty nice points to note here and some pitfalls:

  • The .html file must be a valid XML file. If it’s not there will be bad parse errors.
  • When accessing variables through the value/key system, value can either be an array with a key of “key” or it can be an object with a member variable of $key.
  • Although small, there is an overhead involved with the parsing.
  • When using the tal:repeat tag, it only uses one tag to repeat, but for testing the HTML page, you can use aditional test tags with the tal:replace=”

Tricks

I’ll be trying to post any nifty tricks and tips that I find out along the way that are not evident by reading the documentation. Some stuff such as installation and tal attribute details are not covered in the documentation. I strongly recommend going over the documentation since it is very helpful.

Install

Install is fairly easy, but the install document is blank. It’s pretty much just unzipping the archive and adding it to your include_path. The files for download can be found here. PHPTAL is not standalone and Types must be downloaded from the same page, and PEAR must also be installed in the same way. The include_path must be specified in your php.ini file and the root folders of each of those three folders must be placed here.

TAL:Replace

One of the first things that I found is that the syntax of the tal:replace statement is a little funny at first:

<tr tal:replace="item value">
      <td tal:content="item">Test Content</td>
</tr>

And in the php:

$value = array(2,4,6,8);
$template->set("value", $value);

The thing to note is that index is any string that you define, as long as it matches the string when you use it. The other thing that’s not well covered is the use of the special loop data such as:

<tr tal:replace="item value">
      <td tal:content="repeat/item/number">Test Content</td>
</tr>

This will show the current number in the loop, from 1 to count. The other special variables can be found here.

TAL:Omit-Tag

The other thing that I’ve noticed is that sometimes when using directives such as:

<span tal:replace="item value">
     <p tal:content="repeat/item/number">Test Content</p>
</span>

In this case the <span> tag must be used since the repeat is not contained in a specific tag. Although the span tag doesn’t do anything in this case on the test html page, there’s no need to have it in the final product. The problem is that sometimes the tal:repeat tag doesn’t remove the <span> tag from the final result, so you have to use the tal:omit-tag.

<span tal:replace="item value" tal:omit-tag="">
      <p tal:content="repeat/item/number">Test Content</p>
</span>

Although PHPTAL does a fairly good job of removing these uneeded <span> tags, there are a few instances such as in the tal:condition tag, so check the parsed output to make sure it’s correct.

UPDATE 9/23/04: The only time that the enclosed tag is kept on the previous examples is on the tal:condition tag and only when the tag evaluates to true. If the tal:condition evaluates to false, the entire tag is removed.

TAL:Define

There was one case where I wanted to alternate the color of rows of a table, but it’s not possible by just using the tal:repeat. Not only that but the bgcolor attribute is on the <tr> tag and therefore can’t be the basis for the tal:repeat. Here’s the solution I came up with:

<tableborder="1">
 <tr><td>ID</td><td>Name</td><td>Index</td></tr>

 <span tal:omit-tag="" tal:repeat="item users">
<tal:block condition="repeat/item/even">
 <span tal:define="rowcolor string:#888888" />
 </tal:block>
 <tal:block condition="not: repeat/item/even">
 <span tal:define="rowcolor string:#FFFFFF" />
 </tal:block>
<tr bgcolor="#888888" tal:attributes="bgcolor rowcolor">
 <td tal:content="item/id">TID1</td>
 <td tal:content="item/name">Test Name1</td>
 <td tal:content="repeat/item/index">Unknown</td>
 </tr>
 </span>
<tr bgcolor="#FFFFFF" tal:replace="">
 <td>TID2</td><td>Test Name2</td><td>Unknown</td>
 </tr>
 <tr bgcolor="#888888" tal:replace="">
 <td>TID3</td><td>Test Name3</td><td>Unknown</td>
 </tr>
 </table>

Ok, this looks a little complicated, but the real meat is in the tal:block’s. It checks to see if the line is even, if it is, it set’s rowcolor to “#888888″ and if it’s not it’s set to “#FFFFFF”. The tal:block tags are just special <span>’s and are not important.

Array Length

I have shown how to iterate through an array, I came across a situation where I needed to print the length of an array. Here’s how on the array “users”:

<p>There are <span tal:replace="string: ${users/length}" /></p>

Summary

These are just a few of the things I’ve found by running a few tests. Here’s my first test PHPTAL XHTML and here is the PHP that’s associated with it. Here is the final parsed page. A bunch of the examples are shown in there. I’ll keep posting different tips for this project. Feel free to post your own thoughs on this project.

No Comments | Tags: Programming

Add a Comment