2 ms·
Dead simple View Controller/Templating in PHP
- gregjor 15y agoIt gets even simple without the unnecessary OOP. PHP arrays and extracting into the render() function scope work just as well with less cruft. <?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>