Team LiB
Previous Section Next Section

Hack 73. Generate XUL Using PHP Libraries

There's more than one way to create XUL content. Here's how to do so using PHP libraries.

We have already seen a number of ways to create XUL content for Mozilla [Hack #72] . The first and the simplest is just manually written, static XML. The second is to use XUL templates and XML-RDF to generate XUL based on assertions in RDF. The third is to use JavaScript in conjunction with the Mozilla Document Object Model (DOM) and create the XUL elements programmatically.

The fourth and final way is the subject of this hack: to generate XUL procedurally on a remote host and stream the content like a web page into Mozilla. This hack uses PHP to create the XUL content.

6.17.1. Finding PEAR Libraries for XUL

PEAR (http://pear.php.net) is a collection of reusable PHP libraries. One such PEAR library is XML_XUL, which allows remote generation of Mozilla's XUL using PHP's XML DOM. You can find the PHP XUL Libraries at http://pear.php.net/package/XML_XUL

While it is great to see XUL getting such widespread support, generating XUL in this fashion is a big design choice. In reality, all the programmer is doing is creating an extra unnecessary layer on top of XUL, which blurs the distinction between the XUL user interface (UI) and its data. If you view your network-enabled service as a data service (for example, a stock market feed), don't do this. If you view your network-enabled service as remote application delivery or as a media-oriented content-delivery solution (like Oracle's HTMLDB), this is the way to go.

XUL doesn't have the clear-cut semantic split that XML usually has of content versus presentation (XUL by definition is presentation!). Maintain a clear distinction between content (the RDF data) and presentation (XUL, CSS, and JavaScript) even if both are generated server-side. That will provide maximum flexibility.

6.17.2. Motivation for Server-Side UI Generation

Imagine a web application in which sales staff can update customer details, but other staff members can only view those details. We might use these PEAR/PHP libraries to procedurally generate <textbox> elements for our sales staff to edit the information, but create only <label> elements for everyone else.

Although we could create a one-size-fits-all XUL page, perhaps using templates, we might not want to do that. We might be worried about security. In that case, we don't want technically able end users lifting the lid on our application using their favorite tools. To get around this, we'll have to manufacture separate XUL for each type of employee on the server. Even if users are smart, they'll only be able to hack into the user interface that their security level lets them see. So let's generate code that way.

6.17.3. Getting Oriented with XML_XUL

Assuming PHP and XUL is your bag, let's look at some examples of using this technology. First, make sure you have downloaded and installed the XML_XUL libraries from http://pear.php.net/package/XML_XUL/download. PHP Tools (http://www.phptools.de) also has some good examples, showing both the PHP code and the resultant XUL. Each XUL element is described as a PHP class that inherits from the base PHP class element. Here's a brief outline of the setup required.

First, you need to include the base XUL package:

<?PHP  
 require_once 'XML/XUL.php';

Then, create the root XML document:

$doc = &XML_XUL::createDocument( );

Add in the base stylesheet:

$doc->addStylesheet('chrome://global/skin/');

Create a <window> element and add it into the base XML document doc:

$win = &$doc->createElement('Window', array('title'=>'Firefox Hacks'));
$doc->addRoot($win);

All elements are created by calling the createElement() method on the document root. This will return a new DOM node; then, with the exception of the root node, you'll need to remember to call the appendChild() method to add the new node into the correct location in the document tree. This is pretty standard DOM programming and will be familiar to anyone who has used the DOM before. Notice how the attributes on the XUL elements are handled as an associated PHP array.

Our special security system might extract the user's security from a bit of server data and generate code to match:

if ( user_is_sales( ) )
  $field = &$doc->createElement('label', array('value'=>$customer));
else
  $field = &$doc->createElement('label', array('value'=>$customer));
$doc->getElementById('customer-list')->appendElement($customer);

The final step, after creating the document node by node, is to return it to Mozilla for rendering, by calling the XML documents send() method:

 $doc->send(  );
?>

While you could end up with a lot of PHP code to accomplish something fairly trivial, the great part about working with the DOM and its nodes is that the XML tags are automatically closedgreat for those of us who have poor memories!

6.17.4. Weighing It Up

The XUL PHP libraries are certainly neat and easy to use. If the XUL/RDF template model is too eclectic, convoluted, and overly complex for your needs, then server-generated content might be the way to go. In both cases, you can mix static XUL content with server-generated code; it's just a matter of how much.

Neil Stansbury

    Team LiB
    Previous Section Next Section