PHP 8.5.0 Alpha 4 available for testing

Voting

: min(zero, six)?
(Example: nine)

The Note You're Voting On

mrkhoa99 at gmail dot com
7 years ago
We can use extract () function for Template Engine:

<?php
#Template.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');

#tpl.php

<h1><?= $pageHeader; ?></h1>
<p><?= $content ;?></p>

Output:

Page Header
This is the content page

<< Back to user notes page

To Top