note 116527 added to closure.bindto
| From: | NezarFadle | Date: | Mon, 19 Jan 2015 10:40:07 +0000 |
| Subject: | note 116527 added to closure.bindto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201453@lists.php.net to get a copy of this message | ||
We can use the concept of bindTo to write a very small Template Engine:
#############
index.php
############
<?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>
----
Server IP: 72.52.91.14
Probable Submitter: 175.143.52.161
----
Manual Page -- http://php.net/manual/en/closure.bindto.php
Edit -- https://master.php.net/note/edit/116527
Del: integrated -- https://master.php.net/note/delete/116527/integrated
Del: useless -- https://master.php.net/note/delete/116527/useless
Del: bad code -- https://master.php.net/note/delete/116527/bad+code
Del: spam -- https://master.php.net/note/delete/116527/spam
Del: non-english -- https://master.php.net/note/delete/116527/non-english
Del: in docs -- https://master.php.net/note/delete/116527/in+docs
Del: other reasons-- https://master.php.net/note/delete/116527
Reject -- https://master.php.net/note/reject/116527
Search -- https://master.php.net/manage/user-notes.php