08-12-2009, 02:14 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: May 2009
Posts: 53
Thanks: 2
|
Thanks a lot! :)
Would it be a bad habbit to use shortcuts here:
PHP Code:
<?php foreach( $posts as $post ): ?> <strong><?=$post['header']?></strong> <p> <?=$post['body']?> </p> <?php endforeach; ?>
Edit: I took your class and mixed it with mine, and I'm pretty happy with the result, tell me if I can optimize/improve it further:
PHP Code:
<?php require_once("bootstrap.php");
final class Template { private $dir; private $data; private function __clone() {}
public function __construct($dir='templates/') { if(is_dir($dir)) $this->dir = $dir; else throw new Exception('Directory does not exists @ Template'); } private function __fetch($file) { extract($this->data); require($file); } public function __set($variable, $data) { $this->data[$variable] = $data; }
public function display($template) { $template = rtrim($template, '.php') . '.php'; if(file_exists($this->dir . $template)) $this->__fetch($this->dir . $template); else throw new Exception('File: ' . $template . ' does not exist in ' . $this->dir); } } ?>
Last edited by Sirupsen : 08-12-2009 at 03:46 PM.
|
|
|