cwncool Posted May 27, 2007 Share Posted May 27, 2007 I'm trying to learn some more OOP, and am trying a simple template system. These are the files I have right now: class.php <?php class template { var $template; function load($filepath) { $this->template = file_get_contents($filepath); } function replace($replace, $string) { $this->template = str_replace("#".$replace."#", $string, $this->template); } function display { return $this->template; } } ?> index.php <?php include 'class.php'; $template = new template; $template->load('index.html'); $template->replace('name', 'My name here'); $template->replace('dadname', 'My dads name here'); $template->display(); ?> index.html <html> <body> <h1>My name is: #name#</h1> <h2>My dad's name is: #dadname#</h2> </body> </html> It ends up just displaying this: My name is: #name# My dad's name is: #dadname# I guess that means it's obviously fetching the template data correctly, but it's not replacing and/or outputting correctly. Can someone see what the problem is in this situation? Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/ Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 Humm it looks OK, i have to ask.. you are opening index.php right ? and not just going to the folder as html files are normally defaults Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/#findComment-262410 Share on other sites More sharing options...
cwncool Posted May 27, 2007 Author Share Posted May 27, 2007 Oh, haha. Whoops. Anyway, that still doesn't solve my problem though. Now that I go to my index.php page, it just shows nothing. It just pulls up a blank page. Do you have an idea why that could be? Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/#findComment-262411 Share on other sites More sharing options...
448191 Posted May 27, 2007 Share Posted May 27, 2007 You don't echo. Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/#findComment-262501 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 very true echo 448191 either function display { echo $this->template; } OR echo $template->display(); Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/#findComment-262519 Share on other sites More sharing options...
448191 Posted May 27, 2007 Share Posted May 27, 2007 I'd prefer the first option, since 'display' implies that it outputs something. Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/#findComment-262523 Share on other sites More sharing options...
MadTechie Posted May 27, 2007 Share Posted May 27, 2007 As do i.. but in the past i have change a function name to suite its updated usage lol Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/#findComment-262540 Share on other sites More sharing options...
cwncool Posted May 27, 2007 Author Share Posted May 27, 2007 Okay. That makes sense. Sorry about that dumb mistake. I should've noticed that one easily. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/53120-solved-help-with-simple-template-class-im-working-on/#findComment-262662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.