Okay, I'm writing a script that's SUPPOSED to act as a Template System Parser; however, the script has decided that it'd rather do nothing at all, and I can't seem to find anything wrong with it. I don't want to hear "Just put echos in the code", either; I already did that. The fact is it SHOULD be working, but it just isn't.
<?php
class TemplateNode
{
var $nodename;
var $numvariables = 0;
var $variables = array();
var $childnodes = array();
var $parentnode;
var $filecode;
var $finishedcode = NULL;
var $startmarker = '\\{';
var $endmarker = '\\}';
var $startiteration = '\\{\\*';
var $enditeration = '\\*\\}';
function TemplateNode($name, $parentnode, $parentfilecode)
{
$this->nodename = $name;
$this->parentnode = &$parentnode;
if($name == 'ROOT')
{
$this->filecode = $parentfilecode;
}else{
$splitpoint = $this->startiteration . $name . $this->endmarker;
$splitpoint2 = $this->startmarker . $name . $this->enditeration;
$finalsplit = $splitpoint . '(.*)' . $splitpoint2;
ereg($finalsplit, $parentfilecode, $regs);
$this->filecode = $regs[1];
}
}
function addChildNode($childnode)
{
$this->childnodes[] = &$childnode;
}
function addVariable($variablename, $variablevalue = NULL)
{
$location = NULL;
$count = count($this->variables);
for($i = 0; $i < count($this->variables); $i++)
{
$variable = $this->variables[$i][0];
if(ereg($variablename, $this->variables[$i][0]))
{
$location = $i;
}
}
if($location != NULL)
{
$this->variables[$location][1] = $variablevalue . $this->variables[$location][1];
return true;
}
$this->variables[$this->numvariables] = array($variablename, $variablevalue);
$this->numvariables++;
return true;
}
function replaceParentCode()
{
$test = $this->startiteration . $this->nodename . $this->endmarker;
if(ereg($test, $this->parentnode->filecode) && $this->nodename != 'ROOT')
{
$replace = $this->startiteration . $this->nodename . $this->endmarker . ereg_replace('\*', '\\*', ereg_replace('\}', '\\}', ereg_replace('\{', '\\{', $this->filecode))) . $this->startmarker . $this->nodename . $this->enditeration;
$needle = $this->startmarker . $this->nodename . $this->endmarker;
$this->parentnode->filecode = ereg_replace($replace, $needle, $this->parentnode->filecode);
}
}
function parseCode()
{
$this->finishedcode = $this->filecode;
for($i = 0; $i < count($this->variables); $i++)
{
$needle = $this->startmarker . $this->variables[$i][0] . $this->endmarker;
echo "$needle<br>This is the needle.<br>";
ereg_replace('\\*', '\*', ereg_replace('\\}', '\}', ereg_replace('\\{', '\{', $needle)));
$this->finishedcode = ereg_replace($needle, $this->variables[$i][1], $this->finishedcode);
}
}
}
class template
{
var $nodes = array();
function addFile($fileid, $filename)
{
$file_contents = file($filename);
$string = implode($file_contents, "");
$staff = NULL;
$this->nodes[$fileid][0] = &new TemplateNode('ROOT', $staff, $string);
}
function addNode($fileid, $loopname, $parentnumber = 0)
{
$node = new TemplateNode($loopname, &$this->nodes[$fileid][$parentnumber], $this->nodes[$fileid][$parentnumber]->filecode);
$this->nodes[$fileid][$parentnumber]->addChildNode(&$node);
array_push($this->nodes[$fileid], &$node);
return count($this->nodes[$fileid]) - 1;
}
function addVariables($fileid, $variablename, $variablevalues, $parentnumber = 0)
{
$input_variables = explode(",", $variablename);
while(list(,$value) = each($input_variables))
{
$variables[] = $value;
}
for($i = 0; $i < count($variables); $i++)
{
$this->nodes[$fileid][$parentnumber]->addVariable($variables[$i], $variablevalues[$i]);
}
}
function parseFile($fileid)
{
$length = count($this->nodes[$fileid]) - 1;
for($i = $length; $i > 0; $i--)
{
$node = &$this->nodes[$fileid][$i];
$node->replaceParentCode();
$node->parseCode();
$node->parentnode->addVariable($node->nodename, $node->finishedcode);
$this->nodes[$fileid][$i] = NULL;
}
$this->nodes[$fileid][0]->parseCode();
}
function printCode($fileids)
{
if(is_array($fileids))
{
for($i = 0; $i < count($fileids); $i++)
{
$finalcode .= $this->nodes[$fileids[$i]][0]->finishedcode;
}
}else{
$finalcode = $this->nodes[$fileids][0]->finishedcode;
}
echo "$finalcode";
}
}
?>