PHP code example of airtemplate / airtemplate

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

    

airtemplate / airtemplate example snippets







re 'path/to/src/Parser.php';
.php';
// replace 'ArrayLoader' by 'FilesystemLoader' or 'CacheLoader' as appropriate
re 'path/to/src/Builder.php';


class Chris {
    public $name  = "Chris";
    public $value = 10000;

    public function taxed_value() {
        return $this->value - ($this->value * 0.4);
    }

    public $in_ca = true;
}



use AirTemplate\Builder;
use AirTemplate\Loader\ArrayLoader;

$templates = [
    'canonical' => 'Hello {{name}}
You have just won {{value}} dollars!
',
    'in_ca' => 'Well, {{taxed_value|data:taxed_value}} dollars, after taxes.
'
];

$chris = new Chris;

$builder = new Builder(new ArrayLoader);
$engine = $builder->build($templates);

echo $engine->render('canonical', $chris);
if ($chris->in_ca == true) {
    echo $engine->render('in_ca', $chris);
}



use AirTemplate\Builder;
use AirTemplate\Loader\ArrayLoader;

$templates = [
    'canonical' => 'Hello {{name}}
You have just won {{value}} dollars!
{{in_ca|user:inCa}}',
    'in_ca' => 'Well, {{taxed_value|data:taxed_value}} dollars, after taxes.
'
];

function inCa($value, $field, $data)
{
    global $engine;
    if ($value == false) {
        return '';
    }
    return $engine->render('in_ca', $data);
}

$chris = new Chris;

$builder = new Builder(new ArrayLoader);
$engine = $builder->build($templates);

echo $engine->render('canonical', $chris);

$templates = [
	'canonical.tmpl',
    'in_ca.tmpl'
];

$builder = new Builder(new FilesystemLoader('./templates/mustache-canonical'));
$engine = $builder->build($templates);

$builder = new Builder(new FilesystemLoader('./templates/mustache-canonical'));
$engine = $builder->build('*.tmpl');

echo $engine->render("template-name", $data);
echo $engine->each("template-name", $data[, "separator"[, $rowGenerator]]);

$templates = [
	'list' => '<ul>
{{items}}
</ul>'
	'list-item' => '<li>{{item|esc}}</li>'
];

$builder = new Builder(new ArrayLoader);
$engine = $builder->build($templates);

echo $engine->render(
	'list',
	[
		'items' => $engine->each(
			'list-item',
			['one', 'two', 'three'],
			"\n"
		)
	]
);

$templates = [
	'list' => '<ul>
{{items|each("list-item", "\n")}}
</ul>'
	'list-item' => '<li>{{item|esc}}</li>'
];

$builder = new Builder(new ArrayLoader);
$engine = $builder->build($templates);

echo $engine->render(
	'list',
	[
		'items' => ['one', 'two', 'three']
	]
);

$templates = [
	'list-start' => '<ul>
'
	'list-end' => '</ul>
'
	'list-item' => '<li>{{item|esc}}</li>'
];

// receive list-items one-by-one and write it to the output
// this tiny closure acts as a co-routine to the each-method
$rowGenerator = function() {
	while (true) {
		echo yield;
	}
}

$builder = new Builder(new ArrayLoader);
$engine = $builder->build($templates);

echo $engine->render('list-start');
// echo items in the genarator function
$engine->each('list-item', ['one', 'two', 'three'], PHP_EOL, $rowGenerator());
echo $engine->render('list-end');
);

$options = [
	'splitPattern' => '/(\[@)|\]/',
	'fieldPrefix'  => '[@'
];
$loader = new ArrayLoader($options);
// or
$loader = new FilesystemLoader('path/to/templates', $options);

$data = [
	'key1' => [
		'key1.1' => [ 'hello' ],
		'key1.2' => [ 'world' ]
	],
	'key2' => [
		'key2.1' => 'abc'
	]
]

$template = [
	'relative' => '{{key1=key1.2}}',
	'absolute' => '{{key1=/key2.1}}'
]

class AppView
{
    public function showColors($value, $field, $data)
    {
        if (empty($value)) {
            return $this->view->render('no-colors', $data);
        }
        return $this->view->render('color-table', $value);
    }
}

$view = new AppView;

// The view object can be passed to the builder via constructor
$builder = new Builder(new ArrayLoader, $view);

// or it can be later set using the setApp method
$builder->setApp($view);

function getArticleCode($value, $field, &$data)
{
    // create a new new field
    $data['new_field'] = ...
    // return article-code
    return $data['type'] . '-' . $data['category1'];
}

// field definition: {{somefield|data:doubleSomething}}
class DataObj
{
    public function doubleSomething()
    {
        return $something * 2;
    }
}

$data = [
  ['name' => 'Bob', 'email' => '[email protected]'],
  ['name' => 'Mary', 'email' => '[email protected]'],
  ['name' => 'Jenny', 'email' => '[email protected]'],
];

$templates = [
	'table' => '<table>
<thead>
<tr>
{{thead|each("th", "\n")}}
</tr>
</thead>
<tbody>
{{tbody|each("tr", "\n")}}
</tbody>
</table>',
	'th' => '<th>{{item|esc}}</th>',
	'tr' => '<tr>
<td>{{name|esc}}</td>
<td><a href="mailto:{{email}}">{{email|esc}}</a></td>
</tr>'
];

// render the table
echo $engine->render(
	'table',
	[
		'thead' => ['Username', 'Email'],
		'tbody' => $data
	]
);

// Format the price field with 'sprintf'
// The price field is injected as second parameter, replacing the question mark (?)
{{price|sprintf("$%1.2f", ?)}}
// Convert value to a float and format it using the PHP function 'number_format'
{{value|float|number_format(?, 2, ".", " ")}}