Thread: design tips?
View Single Post
Old 04-26-2009, 08:35 PM   #2 (permalink)
Sam Granger
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

I use a Template Class - basically, I can have an index.html file with php include for header and footer inside, parse variables to it, have while loops if needed. I always design a page in html, once it's complete, slide header, index and footer up. Frames - I never use them, but iframes come in handy :)

Anyway, hope my class will help you:

PHP Code:
<?php

/**
 * @package PHPSauce Store
 * @author Sam Granger
 * @version 1.0
 * @copyright 2009, PHPSauce
 */

class tplClass {

    private 
$vars = array(),
            
$core;

    public function 
_construct($core) {
        
$this->core = &$core;
    }

    public function 
set($var ''$data NULL$reset false) {
        if (
$reset$this->vars = array();
        
$this->vars[$var] = $data;
    }

    public function 
set_vars($vars = array(), $reset false) {
        if (
$reset) {
            
$this->vars = (is_array($vars))? $vars: array();
        } else {
            if (
is_array($vars)) $this->vars array_merge($this->vars$vars);
        }
    }

    private function 
displayBlock($block '') {
        
$this->render($block '.html'VIEW_PATHtrue);
    }

    public function 
render($tpl_file ''$tpl_path null$direct_output false) {
        
extract($this->vars);
        
ob_start();

        if (include 
$tpl_path $tpl_file) {
            
$contents ob_get_contents();
        } else {
            return 
false;
        }

        
ob_end_clean();

        if (
$direct_output) echo $contents;

        return 
$contents;
    }
}

?>

PHP Code:
$connect = new dbClass(); // lets get some content from db!
$connect->dbConnectDBHOSTDBUSERDBPSWDDBNAME );

$template = new tplClass(); // start tplClass

$query 'SELECT * FROM products ORDER BY product_id DESC';
$result $connect->executeQuery($query);
while(
$fetchproducts mysqli_fetch_assoc($result)) {
    
$products[] = $fetchproducts;
}

$variables = array
(
    
'username' => $username,
    
'products' => $products
);

$template->set_vars($variables);
$template->render('idx.html'VIEW_PATHtrue); 
VIEW_PATH is defined in this example but you can have 'templates' instead to let the class know, where the file is.

idx.html is the template file.

Example:
HTML Code:
<?php
$this->displayBlock('header');
?>
	<h2>Welcome to PHPSauce.com</h2>
	<p>Slogan goes here!</p>
	<p>PHPSauce.com offers top of the range scripts for your website needs. Prices are very affordable.</p>
     <?php if (count($products) > 0) { foreach ($products as $product) { ?>
        <div class="product">
			<h2><a href="<?php echo 'product/' . $product['product_slug']; ?>"><?php echo $product['product_title']; ?></a></h2>
		</div>
    <?php } } else { ?>
    <div>No Products found</div>
    <?php } ?>

<?php
$this->displayBlock('footer'); // show the footer.html file in tpl dir
?>
Sam Granger is offline  
Reply With Quote