Forum Moderators: coopster

Message Too Old, No Replies

Templates using php?

         

megaman732

6:35 pm on Aug 20, 2009 (gmt 0)

10+ Year Member



Whats the best way to create my own template engine using php, to transfer html templates into actual websites, and replacing the placeholders with the actual data... well, let me solve my own question...

class Template{

$private $output = '';

public function Load_Template($template){
ob_start();
include($template);
$this->output = ob_get_clean();
}

public function Replace($data){
$this->output = str_replace(array_keys($data), array_values($data), $this->output);
}
public function Display($add_footer = true){
echo $this->output;
}
}

This would work for a simple Template like...

<div>{username}</div>

But what would be the best way to do it with loops in my template. Lets say something like

<ul>
<li>{username}</li>//Loop this line for each user
</ul>

Also, I dont want to use a third party engine like smarty, I would just like to know how to do it myself. Thank you

venga

12:03 pm on Aug 21, 2009 (gmt 0)

10+ Year Member



i personally feel including template fil at the end of the page is easier way than replacing variables....

and one more question,if you really have decided to do in this way...what may be problem with using smarty or any similar packages...
they just do what you are trying to do in much professional way...with easy learning curve and support...

Marcia

2:42 pm on Aug 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are several threads in the forum library that'll help you do your own template:

[webmasterworld.com...]