views:

111

answers:

5

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

+3  A: 

Honestly, if I had to whip up my own template engine in PHP, I'd just use PHP. Just restrict yourself to only using variable interpolation and loops within the "templates".

Writing your own template engine is a tricky thing. You're inventing an entirely new language, after all, albeit a simple one. For it to be very powerful (i.e., beyond simple string replacement), you'd probably have to write an actual stack-based parser rather than rely on str_replace.

You could do this with regular expressions, much as you could parse HTML with regular expressions, but it won't be very reliable, easy to read, easy to debug, or easy to extend.

Eevee
+3  A: 

How about use php itself?

<div><?php echo $username ?></div>

<ul>
    <?php foreach($users as $user): ?>
    <li><?php echo $user->username ?></li> 
    <?php endforeach ?>
</ul>

*note: Funky endforeach syntax explained here.

Crescent Fresh
A: 

Well, most of the answer to your question is preg_replace_callback().

chaos
+1  A: 

You mention that you don't want to use a third-party engine like Smarty, but you are basically setting out to do the exact same thing that Smarty is doing.

Something that people often overlook when talking about templating in PHP is that PHP is a templating language. There's no reason you can't just use regular old PHP inside your templates. In fact, that's what it was originally designed for.

If you really want to build your own templating engine, then you're going to have to sit down and design a syntax for looping and whatever other templating structures you'll want to use. Something like [do 5 times] { loop code }. Make up whatever you want. Then you'll have to parse it with PHP, and modify the template to replace it with the looped structure.

It's a big job... using normal PHP would be much easier.

zombat
much easier for the developer and much faster for the user than php code "compiling" the template.
p4bl0
A: 

I agree with everyone in this discussion - I have never understood the use of proprietary languages for templating in PHP - you know PHP so just use that.

If you are disciplined (e.g. follow MVC architectural pattern) in keeping a clear difference between PHP scripts that do things, and PHP that display things, there won't be any problems.

A couple of ways that can help are calling your PHP template files .phtml, and using alternative PHP syntax which is a little easier to maintain in templateswhich contain both HTML and PHP:

<?php if ( $a == $b ) : ?>
  <strong>True</strong>
<?php else: ?>
  <strong>False</strong>
<?php endif; ?>
JonB