phplang / xhp

XHP extension for PHP
Other
47 stars 0 forks source link

h1. 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 =

{$post}

Hey there.

Like
; One advantage that XHP has over string construction is that it enforces correct markup structure at compile time. That is, the expression @$foo =

Header

;@ is not a valid expression, because you can not close an @h1@ tag with a @/h2@. When building large chunks of markup it can be difficult to be totally correct. With XHP the compiler now checks your work and will refuse to run until the markup is correct. h1. Dynamic Structures Sometimes it may be useful to create a bunch of elements and dynamically add them as children to an element. All XHP objects support the @appendChild@ method which behaves similarly to the same Javascript method. For example: bc. ; foreach ($items as $item) { $list->appendChild(
  • {$item}
  • ); } In the code, @
    '; bc. Hello {$_GET['name']}
    ; As you can see, using XHP makes safety the default rather than the exception. h1. Defining Elements All elements in XHP are just PHP classes. Even the basic HTML elements like div and span are classes. You define an element just like you do a class, except you use a leading colon to specify that you're creating an XHP element: bc. @. @:x:element@ is the core XHP class you should subclass from when defining an element. It will provide you all the methods you need like @appendChild@, and so on. As an @:x:element@ you must define only @render()@. @render()@ should always return more XHP elements. It's important to remember this rule: even elements you define yourself will return XHP elements. The only XHP elements that are allowed to return a string are elements which subclass from @:x:primitive@. The only elements which should subclass @:x:primitive@ are base elements that make HTML building blocks. XHP with the core HTML library is a viable replacement for strings of markup. You can also use the leading-colon syntax to use an XHP element where you would normally use a class name, for instance while referencing class constants or typehinting: bc. @ to appear directly inside of a @@ 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 @