Jump to content

Trying to get this template engine working properly


scottybwoy

Recommended Posts

Hi all,

 

I'm working with with this basic framework tutorial but try as I might, I can't seem to get the templating system working properly.  I'm only trying the basics first.  Here's what I have.

 

Initialisation page :

<?php

require_once 'central.php';

class Page extends SystemBase {
function init() {
	$this->page->set('title', 'I Say Union, You Say Onion');
	$this->page->set("right_bar", include_once(LOGIN_PAGE));
}
}

$p = new Page();

?>

This calls the base class, this is just the constructor and the print function :

<?php
class SystemBase {
    /*Local Atributes**********************************************/
    var $db   = null;        // pointer to DB object
    var $user = null;        // pointer to user object
    var $page = null;        // pointer to a page template
    var $form = null;        // pointer to form object

    var $template = '';      // path to page template

    /*Constructor/Destructor***************************************/
    function __construct($template='home.php') {
        //prevent instantionation of this class (force abstract)
        if (!is_subclass_of($this,'SystemBase')) {
		trigger_error('Base instantiation from non subclass',E_USER_ERROR);
            return NULL;
        }
        
        //initialize attributes
        if (!defined('NO_DB')) {
        	$this->db = new db();
        	$this->user = new User($this->db);
        }

        $this->page = new Template(TEMPLATE_PATH);

        $this->template = $template;

        //run the page using abstract methods
        $this->init();
        $this->authenticate();

        if (!is_null($this->form)) {
        	$this->handleFormEvents();
        }

        if (!defined('NO_PRINT')) {
        	$this->printPage();
        }

        $this->destroy();
    }

    function printPage() {      //abstract
        if (!is_null($this->form)) {
        	$forms = $this->form;

        	foreach ($forms as $form => $v) {
        		$content = $forms->$form->DisplayForm();
            	$this->page->set($form, $content);
        	}
        }

        echo $this->page->fetch($this->template);
    }
}
?>

And finally the template class, just the two main methods I'm using :

<?php
class Template { 
  var $vars; // Holds all the template variables
  var $path; // Path to the templates 

  function __construct($path = null) { 
    $this->path = $path; 
    $this->vars = array(); 
  } 

  function set($name, $value) { 
    $this->vars[$name] = $value; 
  } 

  function fetch($file) { 
    extract($this->vars);     // Extract the vars to local namespace 
    ob_start();          // Start output buffering 
    include($this->path . $file); // Include the file 
    $contents = ob_get_contents(); // Get the contents of the buffer 
    ob_end_clean();        // End buffering and discard 
    return $contents;       // Return the contents 
  } 
} 
?>

 

In my home.php template I have a placeholder <?=$right_bar?>.  And want the login script to go in there, but it doesn't want to.  What I have on my screen instead is the login.php script followed by the home.php page, without any of the placeholders replaced.  Can anyone see why that is?  Is the systemBase class __constructor written wrong and should not call the the printPage method unless no page is set?

 

Hope this makes sense, thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.