Jump to content

[SOLVED] Help with simple template class i'm working on...


cwncool

Recommended Posts

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?

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.