Code: Select all
<?php
define ('STYLE', 'default');
class Template {
private $Url, $Vars;
public function __construct($Page) {
$this->Url = 'styles/' . STYLE . '/template/' . $Page . '.tpl';
}
public function AssignVal($Variable, $Value = null) {
if(is_array($Variable)) {
foreach($Variable as $Key => $Value) {
$this->Vars[$Key] = $Value;
}
} else {
$this->Vars[$Variable] = $Value;
}
}
public function Output() {
if(count($this->Vars) > 0) {
extract($this->Vars);
}
if (file_exists($this->Url)) {
include($this->Url);
}
}
}
$Template = new Template('index');
$Template->AssignVal(array(
'Title' => 'Templating Test!',
'Username' => 'Neil'
));
$Template->Output();
Anyway. The above code is a templating class I have written. I defined the constant "STYLE" at the top of the script, before the template class; usually you will do this in a configuration file, or store it in your database. I just put it in the template file for this example.
The template class itself takes 1 argument: this is the name of the HTML file you want to display to the user. All of my template files are given the file extension .tpl instead of .html, but they are just HTML files inside. So the template class takes the name of the HTML file, and gets the destination for it on the server, then stores this in the private variable Url, like so:
Code: Select all
$this->Url = 'styles/' . STYLE . '/template/' . $Page . '.tpl';
The next function in the class is where things get a little complex.
Code: Select all
public function AssignVal($Variable, $Value = null) {
if(is_array($Variable)) {
foreach($Variable as $Key => $Value) {
$this->Vars[$Key] = $Value;
}
} else {
$this->Vars[$Variable] = $Value;
}
}
Now, AssignVal can take either an array of values, or a single value, and puts them into the private array $this->Vars. So now our template can (almost) see our variables! Yay!
Now for the final function, Output. This is where it all comes together.
Code: Select all
public function Output() {
if(count($this->Vars) > 0) {
extract($this->Vars);
}
if (file_exists($this->Url)) {
include($this->Url);
}
}
Code: Select all
$this->Vars['Username'] becomes $Username
$this->Vars['Title'] becomes $Title
Now we can write a HTML template. Here is the example template I wrote up for this tutorial:
Code: Select all
<html>
<head>
<title>
<?php echo $Title ?>
</title>
</head>
<body>
Hello, <?php echo $Username ?>!
</body>
</html>
Code: Select all
<html>
<head>
<title>
<?php echo $Title ?>
</title>
</head>
<body>
<?php if ($LoggedIn == true): ?>
Hello, <?php echo $Username ?>!
<?php endif; ?>
</body>
</html>
PHP CODE:
Code: Select all
$Template = new Template('index');
$Template->AssignVal(array(
'Title' => 'Templating Test!',
'Username' => 'Neil'
));
$Template->Output();
Code: Select all
<html>
<head>
<title>
<?php echo $Title ?>
</title>
</head>
<body>
Hello, <?php echo $Username ?>!
</body>
</html>
As you can see, it changed the title of the page to Templating Test, and outputted "Neil" as the username, as those are the values we assigned to the templating class. ^_^
I hope this tutorial is somewhat useful... I just realized how ridiculously long it is, so I'm not sure if I feel up to going back and proof reading it, haha. I have used this templating method for a text based MMO, so it does work for that. Just make templates for you different locations: eg. bank.tpl, fight.tpl, and so on.
Feel free to use/modify/redistribute my source code. Post a reply if you have any questions, but hopefully I explained everything well enough.