PHP code example of velliz / pte

1. Go to this page and download the library: Download velliz/pte 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/ */

    

velliz / pte example snippets



pte\Pte;

$pte = new Pte(true, true, true);
$pte->SetMaster('template/master.html');
$pte->SetHtml('template/view.html');

$pte->SetValue([
    'title' => 'Hello World',
    'users' => [
        ['name' => 'John', 'email' => '[email protected]'],
        ['name' => 'Jane', 'email' => '[email protected]']
    ]
]);

echo $pte->Output();

class Assets implements \pte\CustomRender
{
    public function Parse($data = null, $template = '', $templateBinary = false)
    {
        if ($this->fn === 'cdn') {
            return 'https://cdn.example.com/' . $this->param;
        }
        return '';
    }

    public function RegisterFunction($fnName, $paramArray)
    {
        $this->fn = $fnName;
        $this->param = $paramArray;
    }
}

// Usage in template: {!cdn(js/app.js)}
$pte->Output(new Assets());


use pte\Parts;

class Sidebar extends Parts
{
    public function Parse($data = null, $template = '', $templateBinary = false)
    {
        $this->pte->SetHtml('template/sidebar.html');
        $this->pte->SetValue($this->data);
        return $this->pte->Output(null, Pte::VIEW_HTML);
    }
}

// Usage: 'sidebar' => new Sidebar('sidebar', ['title' => 'Menu'])

// Load additional templates
$pte->Output($customRender, Pte::VIEW_HTML, [
    'template/footer.html',
    'template/sidebar.html'
]);

<!-- In template -->
{!view(sidebar.html)}

// HTML (default)
$pte->Output($custom, Pte::VIEW_HTML);

// JSON
$pte->Output(null, Pte::VIEW_JSON);

// JSON with numeric strings
$pte->Output(null, Pte::VIEW_JSON_NUMERIC);

// XML
$pte->Output(null, Pte::VIEW_XML);

// No caching, with master, with body
$pte = new Pte(false, true, true);

// With caching, with master, with body
$pte = new Pte(new PteCache(), true, true);

// Just body parsing, no master
$pte = new Pte(false, false, true);