KMK-ONLINE / xbt by kmklabs

XBT
17,270
0
98
Package Data
Maintainer Username: kmklabs
Package Create Date: 2016-12-26
Package Last Update: 2017-09-18
Language: PHP
License: MIT
Last Refreshed: 2024-11-21 03:00:32
Package Statistics
Total Downloads: 17,270
Monthly Downloads: 46
Daily Downloads: 3
Total Stars: 0
Total Watchers: 98
Total Forks: 1
Total Open Issues: 0

XHP Block Template

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 use path/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.