Dead simple View Controller/Templating in PHP
joecortopassi.blogspot.com
joecortopassi.blogspot.com
<?php
// even simpler templating example
// a view is described by a template file
$template = "template_example.php";
$vars = array("title" => "Waffles");
$vars["items"] = array(
"foo" => "bar",
"waffle" => "iron",
"apple" => "pie"
);
render($template, $vars);
// view rendering function
function render($template, $vars)
{
extract($vars);
require($template);
}
?>
The template_example.php file: <html>
<head>
<title><?php echo $title; ?></title>
<head>
<body>
<h1><?php echo $title; ?></h1>
<ul>
<?php foreach( $items as $k => $v ): ?>
<li><?php echo $k . ' - ' . $v; ?></li>
<?php endforeach; ?>
</body>
</html>