Dead6re Posted July 9, 2008 Share Posted July 9, 2008 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 https://forums.phpfreaks.com/topic/113953-file_get_contents-problem/ Share on other sites More sharing options...
rmbarnes82 Posted July 9, 2008 Share Posted July 9, 2008 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 https://forums.phpfreaks.com/topic/113953-file_get_contents-problem/#findComment-585636 Share on other sites More sharing options...
Dead6re Posted July 9, 2008 Author Share Posted July 9, 2008 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 https://forums.phpfreaks.com/topic/113953-file_get_contents-problem/#findComment-585640 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.