Jump to content

file_get_contents problem


Dead6re

Recommended Posts

Trying to build a basic template system that can include files recursively and it keeps crashing...

 

class lib_Template {
var $Contents;
var $Assign;

function lib_Template($File = './Templates/default.tpl') {
	if (@file_exists($File)) {
		$this->Contents = @file_get_contents($File, 'FILE_TEXT') or $this->ThrowError("Unable to load template file ($File) contents.");
	} else {
		$this->ThrowError("Template file ($File) does not exist.");
	}

	$this->Assign = array();
}

function Add_Tag($TagName, $String) {
	$this->Assign[$TagName] = $String;
}

function Add_Tags($Tags) {
	foreach ($Tags as $TagName => $String) {
		$this->Assign[$TagName] = $String;
	}
}

function LoadFile($File) {
	$Buffer = file_get_contents('./Templates/'.$File);

	if (count($this->Assign) > 0) {
		foreach ($this->Assign as $Tag => $Data) {
			$Data = (file_exists('./Templates/'.$Data)) ? $this->LoadFile($Data) : $Data;
			$Buffer = str_replace('{'.$Tag.'}', $Data, $Buffer);
		}
	}
}

function ReplaceTags() {
	if (count($this->Assign) > 0) {
		foreach ($this->Assign as $Tag => $Data) {
			$Data = (file_exists('./Templates/'.$Data)) ? $this->LoadFile($Data) : $Data;
			$this->Contents = str_replace('{'.$Tag.'}', $Data, $this->Contents);
		}
	}
}

function Display() {
	$this->ReplaceTags();

	echo $this->Contents;
}

function ThrowError($ErrorMsg) {
	$s_Error = 'lib_Template: '.$ErrorMsg;
	throw new Exception($s_Error);
}
};

 

Basically, if you create three template files such as default.tpl, menu.tpl message.tpl with the contents

default.tpl -> {menu}

menu.tpl -> {message}

message.tpl -> {username}

 

Init the template class:

$g_Template = new lib_Template();
$g_Files = array('menu' => 'menu.tpl', 'message' => 'message.tpl', 'username' => 'Dead6re');
$g_Template->Add_Tags($g_Files);

$g_Template->Display();

Link to comment
Share on other sites

Hi,

 

When is crashes do you get a 'stack overflow' error by any chance? If so that means that you are infinitely recursing somewhere, probably here:

 

$Data = (file_exists('./Templates/'.$Data)) ? $this->LoadFile($Data) : $Data;

 

When using recursion, you must first determine your base case. This is a condition, which when met means you will stop recursing. If you don't have a base case, or the base case is never met, your program will infinitely recurse causing a crash.

 

Robin

Link to comment
Share on other sites

Hey,

 

It isn't causing a infinite loop so I don't have to worry about a 'stack overflow'. The problem seems to be with file_get_contents, using the latest version so it seems like it may be a PHP bug. I just wanted to make sure.

 

See my replication steps which are clearly not a infinite loop.

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.