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 :)

Link to comment
https://forums.phpfreaks.com/topic/295834-template-class-problem/
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)) {
?>

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.