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.
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.
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:
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);
$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.