We can use extract () function for Template Engine:
<?php
class Template
{
protected $viewVars;
public function renderPage($tpl)
{
ob_start();
extract($this->viewVars, EXTR_SKIP);
include $tpl;
return ob_end_flush();
}
public function assign($arr)
{
foreach ($arr as $key => $value) {
$this->viewVars[$key] = $value;
}
return $this;
}
}
$template = new Template();
$template->assign(
[ 'pageHeader' => 'Page Header', 'content' => 'This is the content page']
);
$template->renderPage('tpl.php');
<h1><?= $pageHeader; ?></h1>
<p><?= $content ;?></p>
Output:
Page Header
This is the content page