Download the PHP
package tj09/xhp-lib without Composer
On this page you can find all versions of the php package
tj09/xhp-lib. It is possible to download/install
these versions without Composer. Possible dependencies are resolved
automatically.
After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.
Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.
In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories.
In this case some credentials are needed to access such packages.
Please use the auth.json textarea to insert credentials, if a package is coming from a private repository.
You can look here for more information.
Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
To use Composer is sometimes complicated. Especially for beginners.
Composer needs much resources. Sometimes they are not available on a simple webspace.
If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
XHP augments the syntax of PHP 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.
This repository contains the class library, which is required to use XHP. You
will also want the PHP extension, available from
https://github.com/TJ09/xhp-php-extension
h1. Installation
"Composer":https://getcomposer.org/ is the recommended installation method. To add XHP to your project, add the following to your @composer.json@ then re-run composer:
"require": {
"tj09/xhp-lib": "1.x"
}
h1. Simple Example
bc.
In the code, @
@ creates a ul with no children. Then we dynamically append children to it for each item in the @$items@ list.
h1. Escaping
An interesting feature of XHP is the idea of automatic escaping. In vanilla PHP if you want to render input from the user you must manually escape it. This practice is error-prone and has been proven over time to be an untenable solution. It increases code complexity and still leads to security vulnerabilities by careless programming. However, since XHP has context-specific about the page structure it can automatically escape data. The following two examples are identical, and both are "safe".
bc.
After we define @fb:thing@ we can instantiate it with the expression @@. @: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.
h1. Defining Attributes
Most elements will take some number of attributes which affect its behavior. You define attributes in an element with the @attribute@ keyword.
bc.
Here we define two attributes, @title@ and @type@. @title@ is of type @string@ and the default value is @"No Title"@. type is of type @enum@ and has two valid values -- @"cool"@ and @"lame"@. Valid types are @bool@, @int@, @array@, @string@, and @var@. You can also specify a class or element name as a type. Finally, you can put @required after the name to specify that this attribute is required for the element to render.
Note that when you extend another element you will always inherit its attributes. However, any attributes you specify with the same name will override your parent's attributes.
You can also steal another element's attributes by specifying only a tag name in a definition. The declaration @attribute :div@ says that this element may accept any attribute that a div element could accept.
h1. Defining Element Structure
All elements have some kind of structure that they must follow. For instance, in HTML5 it is illegal for an @@ 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 the @children@ keyword, which uses a syntax similar to regular expressions. Note, that unlike @attribute@, @children@ may only appear once inside any class.
bc.
A children declaration supports the following postfix operators:
* ? : Zero or one instance
* * : Zero or more instances
* + : One or more instances
If no operator is specified then the declaration must be matched exactly one time. You may also use the @,@ or @|@ operator to combine multiple declarations into one. The @,@ operator specifies a list of declarations that must appear in order, and a @|@ specifies a list of declarations, of which one must match. For instance if you are defining a page layout your children declaration may look like:
bc. children (:fb:left-column?, :fb:content, :fb:right-column?);
This specifies an optional left column followed by a required content and an optional right column.
You can also use the special declarations @any@ or @empty@ to specify that your element can accept any elements, or no elements. If you don't specify a children declaration your parent class's declaration will be inherited. @:x:element@'s children declaration is @children any;@.
Note: The algorithm which checks adherence to a children declaration is greedy with no backtracking. For most children declarations this will make no difference, but if you're defining a complex children declaration, you should know how it works. Basically, this declaration is impossible to satisfy: @children (:fb:thing*, :fb:thing);@. The @*@ postfix operator is greedy and will capture all @fb:thing@'s. After that it's impossible to match another @fb:thing@.
h1. Element Categories
A lot of times you may want to accept all children of a particular group, but enumerating the group starts to become unsustainable. When this happens you can define an element group and specify in your child declaration that your element is a member. Then you can reference that group in a children declaration using the % prefix.
bc. class :fb:thing1 extends :x:element {
category %fb:thing;
}
class :fb:thing2 extends :x:element {
category %fb:thing;
}
class :fb:thing-container extends :x:element {
children (%fb:thing)*;
}
h1. Whitespace
In XHP, text nodes that contain only whitespace are removed. The expressions @
@ 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.
This element would be considered magic because when you print an @@ it actually returns a div.
h1. External Resources
Below are a list of external resources about XHP:
"Code Before the Horse":http://codebeforethehorse.tumblr.com - Basic XHP introduction, examples, and lessons learned from Facebook written by one of their UI Engineers.
Composer command for our command line client (download client)This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free.Standard composer command
The package tj09/xhp-lib contains the following files
Loading the files please wait ...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.