Page 1 of 1

[SOLVED]ob_start issue in my template class

Posted: Fri Apr 10, 2009 4:42 am
by novice4eva
Dear friends, i have been working in a template class, a basic one with ob_start() and ob_get_clean(). Recently i found that my class wouldn't return the buffer content of a template file that has already been called. So this is my template class, you may look at fetch() function directly coz this is the function that does the core thing :

Code: Select all

 
<?php
/*
* Author:Deewendra G. Shrestha
* This class provides basic smarty type functionalities for PHP based template
* Last Modification Date : March 02, 2009 by Deewendra G. Shrestha
*/
class Template
{
    private $variables;
    private $errors;
    private $templatePath;
    private $templateDir;
    private $css;
    
    /*
    * Constructor for the class. You can assign default values here.
    * Please use upper case letters for all the variable assignments
    */
    public function Template()
    {
        $this->variables = array();
        $this->errors = array();
        $this->templateDir = CONST_ROOT_BASE.'Templates/'; 
        
        $this->assign("TPL_FRAMEWORKBASE", CONST_FRAMEWORK_BASE); 
        $this->assign("TPL_ROOTBASE", CONST_ROOT_BASE_URL); 
        $this->assign("TPL_CSSBASE", CONST_CSS_BASE_URL); 
        $this->assign("TPL_IMGBASE", CONST_IMG_BASE_URL); 
        $this->assign("ROOT_BASE", CONST_ROOT_BASE); 
        $this->css = array(
                array('NAME'=>'reset','TITLE'=>NULL,'TYPE'=>NULL),
                array('NAME'=>'Red/css','TITLE'=>NULL,'TYPE'=>NULL),
                array('NAME'=>'Red/menu','TITLE'=>NULL,'TYPE'=>NULL),
                array('NAME'=>'Blue/css','TITLE'=>'blueCss','TYPE'=>"ALT"),
                array('NAME'=>'Blue/menu','TITLE'=>'blueMenu','TYPE'=>"ALT"),
                );
    }
    
    /*
    * This function adds css style to the existing css style lists
    * Parameter:
            $cssArray = an array of pattern array('NAME'=>'Blue/menu','TITLE'=>'blueMenu','TYPE'=>"ALT")
    */
    public function addCss($cssArray)
    {
        $cssFile = CONST_CSS_BASE.$cssArray['NAME'].'.css';
        if(file_exists($cssFile))
            array_push($this->css,$cssArray);
        else
            array_push($this->errors,'Invalid css file name provided.');
    }
    
    /*
    * This function unsets or deletes the value assigned to the class variable "variables"
    * Parameter:
            $index = any string or array
    */
    public function destroy($index=NULL)
    {
        if(!is_null($index))
        {
            if(is_array($index))
            {
                $index = array_change_key_case($index,CASE_UPPER);
                foreach($index as $val)
                    if(isset($this->variables[$val]))
                        unset($this->variables[$val]);
            }
            else
            {
                $index = strtoupper($index);
                if(isset($this->variables[$index]))
                    unset($this->variables[$index]);
            }
        }
    }
    
    /*
    * assigns property to the template file, for standard compliant all the 
    * array index has been converted to upper case 
    * Please make sure that you use UPPER CASED variable names in the template files
    */
    public function assign($key=NULL,$val=NULL)
    {
        if(!is_null($key))
        {
            if(is_array($key)){
                $key = array_change_key_case($key,CASE_UPPER);
                $this->variables = array_merge($this->variables,$key);
            }
            else
                $this->variables[strtoupper($key)] = $val;
        }
        else
            array_push($this->errors,'Assignment done on null key.');
    }
    
    /*
    * This function sets the template file 
    * Parameters - 
    *   $template is the template file name
    *   $path accepts two values relative/absolute(questionable) to identify the type of path
    *   default value is "relative"
    * relative path would set the template path from the default template path(check the constructor to find out) 
    * absolute path would set the template path from the current working file 
    */
    public function setTemplate($template=NULL,$path="relative")
    {
        $path=is_null($path) || trim($path)==''?"relative":strtolower($path);
        $pathOptions = array("absolute","relative");
        if(!is_null($template))
        {
            if(in_array($path,$pathOptions))
            {
                switch($path)
                {
                    case "absolute":
                        $this->templatePath = $template;
                        break;
                    case "relative":
                        $this->templatePath = $this->templateDir.$template;
                        break;
                }
                if(file_exists($this->templatePath))
                    return true;
                else
                {
                    array_push($this->errors,'Invalid template file: '.$this->templatePath);
                    $this->templatePath = NULL;
                    return false;
                }   
            }
            else{
                array_push($this->errors,'Template path must be either relative or absolute.');
            }   
        }
        else
            array_push($this->errors,'Cannot assign null values for template paths.');
    }
    
    /*
    * This function returns the HTML equivalent of the template after PHP preocessing
    */
    public function fetch($template=NULL,$path=NULL)
    {
        if(!is_null($template))
            $this->setTemplate($template,$path);
        if(!is_null($this->templatePath))
        {
            $this->assign('TPL_CSS',$this->css);
            ob_start();
            foreach($this->variables as $key=>$val)
                $$key = $val;
            include_once($this->templatePath);
            return  ob_get_clean();
        }
        return false;
    }
    
    /*
    * This function prints the HTML equivalent of the template after PHP preocessing
    */
    public function display($template=NULL,$path=NULL)
    {
        echo $this->fetch($template,$path);
    }
    
    public function showErrors()
    {
        print_r($this->errors);
    }
    
    public function displayError($errs,$title='Errors')
    {
        $title=!is_null($title)?$title:'Errors';
        $this->assign('TPL_WIDTH','300');
        $this->assign('TPL_ERR_TITLE',$title);
        $this->assign('TPL_ERRORS',$errs);
        $content = $this->fetch('error.php');
        $this->destroy(array('TPL_WIDTH','TPL_ERR_TITLE','TPL_ERRORS'));
        return $content;
 
    }
}
?>
 
Now lets say i created a static HTML footer and named it 'footer.php'.

If i do :

Code: Select all

 
$objTemplate = new Template();
$objTemplate->setTemplate('footer.php','absolute');
$objTemplate->display();//THIS ONE OUTPUTS THE CONTENT
$objTemplate->display();//THIS ONE DOESN'T
 
Would you happen to know why it refuses to produce output for same file???

Thanks a lot in advance

Re: ob_start issue in my template class

Posted: Fri Apr 10, 2009 5:38 am
by requinix
Once you call ob_get_clean (in fetch()) there's nothing left in the buffer. That's what the "clean" means. When you call it again there's nothing left to get.

How about ob_get_contents instead?

Re: ob_start issue in my template class

Posted: Fri Apr 10, 2009 5:43 am
by novice4eva
Yup prior to ob_get_clean() i had used ob_get_contents() followed by ob_end_clean()....no luck with that too :cry:

Re: ob_start issue in my template class

Posted: Fri Apr 10, 2009 6:30 am
by requinix
Oh.

Code: Select all

include_once($this->templatePath);
You do know the definition of include_once, right? And how it differs from include?

And please end your output buffers when you're done using them.

Re: ob_start issue in my template class

Posted: Fri Apr 10, 2009 6:34 am
by novice4eva
OH SHOOT!! :mrgreen:
Thanks a trillion times....i would have never figured that out without ur help..

Thanks tasairis, thanks a lot