Thread: PHP Template
View Single Post
Old 08-12-2009, 02:14 PM   #5 (permalink)
Sirupsen
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Default

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.
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote