kmklabs / xbt
XBT
Installs: 17 274
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 98
Forks: 1
Open Issues: 0
Requires
- php: >=5.5.9
- ext-xhp: *
- illuminate/view: ~5.1
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 5.1.*
This package is not auto-updated.
Last update: 2024-11-23 20:48:11 UTC
README
Intro
This is a simple library to compile an XHP template expression into an ordinary Hack class. We do this because we need to have a template inheritance and inclusion in XHP.
Features
- Template block/inclusion
- Accessing outer template from within
- Template inheritance, like
<xbt:template extends="path.dot.base">
which will usepath/dot/base.xbt.php
- Accessing parent blocks with
<xbt:parent />
Example
Suppose that we have this xbt
template, named template.xbt.php
:
<xbt:template doctype="true">
<html>
<body>
<h1>This is a header</h1>
<xbt:block name="main">
<p>This is inside a block</p>
<xbt:block name="submain">
<p>This is a block inside a block</p>
</xbt:block>
</xbt:block>
<p>This is a simple footer</p>
</body>
</html>
</xbt:template>
And have a view file named welcome.xbt.php
:
<xbt:template base="template">
<xbt:block name="main">
<p>Welcome Home</p>
</xbt:block>
</xbt:template>
Load the file, and using this library, we can convert the template into an XHP class
$contents = file_get_contents("template.xbt.php");
$tokenizer = new Tokenizer($contents);
$tokenStream = $tokenizer->tokenize();
$parser = new Parser($tokenStream);
$result = $parser->parse();
$result
should be like something like this:
<?php
class __xbt_4bc492834a61e30d17d158c6a052837584b1db90 extends App\Publishing\Lib\Xbt\Runtime
{
public function render()
{
return <x:frag>
<h1>This is a header</h1>
{$this->block_main()}
<p>This is a simple footer</p>
</x:frag>;
}
public function block_main()
{
return <x:frag>
<p>This is inside a block</p>
{$this->block_submain()}
</x:frag>;
}
public function block_submain()
{
return <x:frag>
<p>This is a block inside a block</p>
</x:frag>;
}
}
Laravel Binding and Config
Include the Xbt\LaravelServiceProvider
to your project. Compiled xhp classes are in
config('view.compiled')
, and compiled laravel views from that classes
are in config('view.xbt_cache')
.
Process
File -> Tokenizer -> TokenStream -> Parser -> Node -> Xhp
Tokenizer
will tokenize the template into a TokenStream
. We pass the
TokenStream
into the Parser
, and call the method parse
to get the
Template
node, the highest level representation node for a given template.
Node
is the abstraction of xhp class in php/hack. The parsing process will
turn a template file into a Node
before writing into files. Method compile
in Template
will output XHP code.