Jump to content

Template class problem


tsz

Recommended Posts

Soo I try to build an simple and basic template engine which displays content from template files.

 

here is the class:



class template {


public $filename;
public $assigned_vars = array();


public function assign($key, $value)
{
$this->assigned_vars[$key] = $value;
}


public function display($filename)
{
if(file_exists($filename))
{
$output = file_get_contents($filename);
foreach($this->assigned_vars as $key => $value)
{
$output = preg_replace('/{'.$key.'}/', $value, $output);
}
return $output;
}
else{
return "Missing template error";
}
}


    
}

yet when I tries to execute the class its doesn't return anything.

i execute it like this:

$tp->display(ROOT_PATH.'home.php');

consider that I have defined ROOT_PATH already and $tp as new template class.

 

what have i done wrong?

help plz :)

Edited by tsz
Link to comment
Share on other sites

Are all PHP errors and warnings enabled? Are they being shown? Note that you can add the following to the top of your script during the debugging process:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Also, have you tried adding some debugging statements to your class to know if things are even being called? For example, you could try something like this:

<?php
public function display($filename) {
 
     print '<div>In display() method</div>';
 
 
     if(file_exists($filename)) {
?>
Link to comment
Share on other sites

 

Are all PHP errors and warnings enabled? Are they being shown? Note that you can add the following to the top of your script during the debugging process:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Also, have you tried adding some debugging statements to your class to know if things are even being called? For example, you could try something like this:

<?php
public function display($filename) {
 
     print '<div>In display() method</div>';
 
 
     if(file_exists($filename)) {
?>

 

I have tried too add the error reporting script. no related warnings to the class(only to another, unrelevent class).

your second suggestion doesn't output anything either.

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.