PHP code example of tj09 / xhp-lib

1. Go to this page and download the library: Download tj09/xhp-lib library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tj09 / xhp-lib example snippets


$href = 'http://www.github.com';
echo <a href={$href}>Github</a>;

Take note of the syntax on line 3, this is not a string. This is the major new syntax that XHP introduces.

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.

h1. Complex Structures

Note that XHP structures can be arbitrarily complex. This is a valid XHP program:

bc. 
$post =
  <div class="post">
    <h2>{$post}</h2>
    <p><span>Hey there.</span></p>
    <a href={$like_link}>Like</a>
  </div>;

One advantage that XHP has over string construction is that it enforces correct markup structure at compile time. That is, the expression @$foo = <h1>Header</h2>;@ 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. 
$list = <ul />;
foreach ($items as $item) {
  $list->appendChild(<li>{$item}</li>);

echo '<div>Hello '.htmlspecialchars($_GET['name']).'</div>';

bc. 
echo <div>Hello {$_GET['name']}</div>;

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. 
class :fb:thing extends :x:element {
  ...

echo :fb:thing::someConstant;

bc. 
function giveMeAThing(:fb:thing $thing) {

class :fb:thing extends :x:element {
  attribute
    string title = "No Title",
    enum { "cool", "lame" } type;

class :fb:thing-container extends :x:element {
  children (:fb:thing1 | :fb:thing2)*;

$node = <div><label>Title:</label> <span>{$title}</span></div>;

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 @<label />@ 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. 
class :fb:thing extends :x:element {
  protected function render() {
    return <div class="thing">thing</div>;
  }