note 116526 deleted from closure.bindto by tularis
| From: | tularis@php.net | Date: | Mon, 19 Jan 2015 12:11:10 +0000 |
| Subject: | note 116526 deleted from closure.bindto by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201456@lists.php.net to get a copy of this message | ||
Note Submitter: Nezar Fadle
----
We can use the concept of bindTo to write a very small Template Engine:
#############
index.php
#############
class Article{
private $title = "This is an article";
}
class Post{
private $title = "This is a post";
}
class Template{
function render($context, $tpl){
$closure = function($tpl){
ob_start();
include $tpl;
return ob_end_flush();
};
$closure = $closure->bindTo($context, $context);
$closure($tpl);
}
}
$art = new Article();
$post = new Post();
$template = new Template();
$template->render($art, 'tpl.php');
$template->render($post, 'tpl.php');
#############
tpl.php
############
We can use the concept of bindTo to write a very small Template Engine:
#############
index.php
#############
class Article{
private $title = "This is an article";
}
class Post{
private $title = "This is a post";
}
class Template{
function render($context, $tpl){
$closure = function($tpl){
ob_start();
include $tpl;
return ob_end_flush();
};
$closure = $closure->bindTo($context, $context);
$closure($tpl);
}
}
$art = new Article();
$post = new Post();
$template = new Template();
$template->render($art, 'tpl.php');
$template->render($post, 'tpl.php');
#############
tpl.php
############
<h1><?php echo $this->title;?></h1>