Last Updated: March 08, 2016
·
7.04K
· nmalcolm

Getting stuck in with XHP

What is XHP?

XHP is a PHP extension which augments the syntax of the language such that XML document fragments become valid PHP expressions. This allows you to use PHP as a stricter templating engine and offers much more straightforward implementation of reusable components. XHP was developed in-house at Facebook and open sourced in 2010.

Read more here: https://github.com/facebook/xhp/

Why should I use XHP?

XHP is dynamic.
XHP is secure.
XHP forces validity.
XHP makes development simple.

XHP automatically escapes all input data making it near-fool proof to XSS vulnerabilities. Because XHP augments the syntax of the language you don't need to manually sanitize any input which passes through XHP. Nifty, eh?

XHP is also dynamic and contains reusable objects. This means that we don't have to constantly define the same HTML code (Think defining a variable in PHP).

Lastly, XHP forces valid HTML markup at compile time (and will fail completely if it's invalid). Invalid markup is a thing of the past with XHP.

Enough of the facts! How do I install it?

XHP has several requirements, one of them being a variant of Linux. It is possible to run XHP in a Windows environment but there is no official support/release for it. An unofficial DLL built for PHP 5.3 can be found here: http://code.google.com/p/pecl-win/downloads/detail?name=php_xhp-1.3.9.dll

XHP also has the following dependencies:

PHP 5.2.x or 5.3.x
gcc 4.0 or higher
g++ 4.0 or higher
flex 2.5.35 or higher
Bison 2.3 or higher
re2c 0.13.5 or higher

These can all be installed via a package manager such as yum or apt. I'd also recommend using Apache with XHP. I haven't tested with NGINX or lighttpd but there have been reports of issues with IIS so for those who are using XHP with Windows should tread carefully.

Although not documented in the official XHP documentation, you should have php-devel (RHEL derivatives) or php-dev (Debian derivatives) installed to run phpize as I have noted below. I'm downloading the source directly from Github so it might be wise to install git too.

To install XHP you should compile it from source. The following commands are for RHEL derivatives (CentOS for example). For Debian derivatives or other distros please consult your system manual.

cd /path/to/xhp/parent/
git clone git://github.com/facebook/xhp.git
cd xhp
phpize
./configure
make
make test
make install

If all went well XHP should now be installed on your system.

I didn't blow anything up. What's next?

Next you have to include the XHP library in your project. The XHP library is located at /path/to/xhp/php-lib/. Now, you can do this one of two ways.

1. Add the path to init.php to your autoprependfile directive in php.ini.

For example:

autoprependfile = /path/to/xhp/php-lib/init.php

2. Directly include init.php in your project.

For example:

require_once '../xhp/php-lib/init.php';

I prefer the former because it's set and forget.

Next you'll have to test if it works or not (which, if you're following this to the letter, it should). You can do that by adding the following to a view or template file of your project.

echo <span>Hello, World!</span>;

View the project from a web browser. It should output to the page "Hello, World!". If you receive a blank page, or an syntax error, go back and check you have successfully included XHP. Chances are you've either missed a step or made a mistake.

It works! Where do I go from here?

Here comes the fun part. The actual development.

A typical XHP template would look something like this:

<?php

$title = 'My Project';

$head =
    <head>
        <meta charset="utf-8" />
        <title>{$title}</title>
    </head>;

$content =
    <x:frag>
    Welcome to my project!
    </x:frag>;

$body = 
    <body>
        {$content}
    </body>;

echo
    <x:doctype>
        <html>
            {$head}
            {$body}
        </html>
    </x:doctype>;
?>

That would simply output:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My Project</title>
</head>
<body> Welcome to my project! </body>
</html>

If that looks like Russian to you, don't worry, I'll explain each section.

Firstly you'll notice <title>{$title}</title>. Now, in regular PHP that would output the variable $title. XHP works similarly in that way but in a slightly more complex manner. Instead of $title being a variable, it's being interpreted as a full PHP expression.

That allows you to do fun little things such as this:

Copyright © {date('Y')} My Project

You'll also probably be looking at <x:frag> and be thinking 'What the hell is that?!'. It's actually a very important part of the template. Think of it as an invisible <div> tag. It tells XHP where the code fragment starts and ends. This is not needed for the <head> and <body> tags as they both tell XHP themselves. This is also true for any other tag but this method is cleaner than having random <div> and <span> tags plastered everywhere.

Finally we echo the template out. The final part of the code is self explanatory. For those who are unsure of what <x:doctype> does, it simply add a HTML document type to the top of the page (As XHP can't handle exclamation marks). By default it's the HTML5 doctype but this can be changed in html.php in the php-lib directory.

That's as far as I'll go with getting stuck in with XHP. For more complex code and solutions, please see: https://github.com/facebook/xhp/wiki

Happy hacking.