{$post}
Hey there.
Likeh1. Archived Repo
This project is a fork from Facebook's archive repo at https://github.com/facebookarchive/xhp-php5-extension
PHP7 support is provided as a community project and not by Facebook, nor is this fork endorsed by the company.
This project just contains the PHP extension and core tests. The class library (which is required for this extension to be useful) is available at https://github.com/phplang/xhp-lib .
HHVM users get XHP support by default in HackLang files, and should use the official HackLang XHP library at https://github.com/facebook/xhp-lib
h1. Introduction
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.
h1. Simple Example
bc. <?php $href = 'http://www.facebook.com'; echo Facebook;
Take note of the syntax on line 3, this is not a string. This is the major new syntax that XHP introduces to PHP.
Anything that's in {}'s is interpreted as a full PHP expression. This differs from {}'s in double-quoted strings; double-quoted strings can only contain variables.
You can define arbitrary elements that can be instantiated in PHP. Under the covers each element you create is an instance of a class. To define new elements you just define a new class. XHP comes with a set of predefined elements which implement most of HTML for you.
Important: Please be sure to include "init.php":http://github.com/facebook/xhp/blob/master/php-lib/init.php which is in the "php-lib":http://github.com/facebook/xhp/tree/master/php-lib/ directory of the repository. The XHP extension only handles adding the XML syntax, the actual elements are defined directly in PHP. Including the core XHP libraries in PHP code means you can customize XHP for your own applications, though it's recommended to avoid large forks which may cause incompatibilities as XHP evolves. All the examples you see on this page omit the required includes.
h1. Complex Structures
Note that XHP structures can be arbitrarily complex. This is a valid XHP program:
bc. <?php $post =
; One advantage that XHP has over string construction is that it enforces correct markup structure at compile time. That is, the expression @$foo =@ tag (it must be inside a form). XHP allows you to define a content model which documents must adhere to. This is done with t he @children@ keyword, which uses a syntax similar to regular expressions. Note, that unlike @attribute@, @children@ may only appear once inside any class. bc. @ and @
@ are identical. Text nodes that contain non-whitespace are trimmed on the left and right to at most 1 space. This is worth noting because you may want to do something like:
bc. {$title};
This will lead to non-desirable results as the space between the @:@ and @$title@ will be lost. In order to fix this try moving the space into the @@ tag. If you can't do this then just use @{' '}@ which will not be stripped.
h1. Best Practices
There are certain conventions that you should comply with while using XHP.
* Don't pollute the global XHP namespace with namespace-less elements. Most elements you define should use some namespace. Elements that use no namespace should not be "magic" at all. For instance,
bc. thing;
}
}
This element would be considered magic because when you print an @