Template Class, with support for binding object contexts to the template.
Requires PHP 5.4 for Closure $this support
Put this in a file named composer.json
:
{
"repositories": [
{"type": "vcs", "url": "git://gist.github.com/1121233.git"}
],
"require": {
"chh/simple-template": "*"
}
}
Get Composer and install:
wget http://getcomposer.org/composer.phar
php composer.phar install
Hello World <?= $name ?>
Here is the output of some method: <?= $this->foo() ?>
Here is the value of a context property: <?= $this->bar ?>
<?php
require "Template.php";
use CHH\Template;
class Context
{
var $bar = "Some Context Property";
function foo()
{
return "Context::foo()";
}
}
$template = new Template('/hello_world.phtml');
echo $template->render(new Context, ['name' => 'Jim']);
This yields:
Hello World Jim
Here is the output of some method: Context::foo()
Here is the value of a context property: Some Context Property
Specifically the
protected $bar
of the context. I love the idea of transferring the entire context object instead of manually assigning context vars, but re-assigning $this and giving access to protected vars and methods seems like it should be avoided.