PHP Simple Template System

Status
Not open for further replies.

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
Okay so first we need the class file.

PHP:
<?
class iTemplate {
public $template;
function load($filepath) {
     $this->template = file_get_contents($filepath);
}
function replace($var, $content) {
     $this->template = str_replace("[$var]", $content, $this->template);
}
function publish() {
     eval("?>".$this->template."<?");
}
}
?>
Save that as template.class.php

And now the file people access
PHP:
<?php
include "template.class.php";
$template = new iTemplate;
$template->load("template.html"); //change the the name of the template page
$template->replace("title", "iTes0 Template System"); //change to what you want the title to be
$template->replace("site", "DevBest"); //this is just an example you can change both values
$template->replace("by", "iTes0"); //as is this
$template->publish();
?>

To add more variables just add
PHP:
$template->replace("VARIABLE", "REPLACEMENT");

Now for the template itself.
HTML:
<html>
<head>
<title>[title]</title>
</head>
<body>
<h3>Hello there [site] members!</h3>
<p>This is a simple PHP template tutorial</p>
<p>This was created by [by]</p>
</body>
</html>

Save that as template.html or whatever you want, just be sure to name it in the load();


That is all, leave feedback :p
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Nice tutorial/script man. I can see a lot more scripts using templates, however, I won't be!
 

Beast

Posting Freak
Sep 15, 2011
625
163
Seems nice, id share my templete system a bit more advanced but I have a project using it so I dont need people usin mah stuff.
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
Thanks m0nsta. :p

And I have a more advanced one, but I am going to start posting tutorials for people new to coding.
 

DaLightz

See ya'll in the afterlife.
May 19, 2012
1,136
262
How would I change it from [] to {}?

<?
class iTemplate {
public
$template;
function
load($filepath) {
$this->template = file_get_contents($filepath);
}
function
replace($var, $content) {
$this->template = str_replace("[$var]", $content, $this->template);
}
function
publish() {
eval(
"?>".$this->template."<?");
}
}

?>
 

TesoMayn

Boredom, it vexes me.
Oct 30, 2011
1,482
1,482
Actually this is a template system I used a while ago, that I did myself.

Seeing as template systems are common, there is going to be hundreds of similar systems on the web. :p

===EDIT===

This is from that site:

PHP:
public function set($key, $value) {
    $this->values[$key] = $value;
}
 
public function output() {
    if (!file_exists($this->file)) {
        return "Error loading template file ($this->file).<br />";
    }
    $output = file_get_contents($this->file);
 
    foreach ($this->values as $key => $value) {
        $tagToReplace = "[@$key]";
        $output = str_replace($tagToReplace, $value, $output);
    }
 
    return $output;
}

Where in my script is this?
 

GarettM

Posting Freak
Aug 5, 2010
833
136
If i use this do You want me to put ur name in a Copyright.TXT id put it in the CMS but im only using a small part of the code >_>
 
Status
Not open for further replies.

Users who are viewing this thread

Top