views:

32

answers:

1

For some years that I've been using my own PHP template engine, which is not really "my own" as I saw it on a tutorial many years ago. However, I've refactored most of the code making it simpler and easier to use. I rarely make a PHP project without it.

It's very basic and the class only has 3 methods, load, assign and render. The load loads the template file (usually HTML) and saves it as a string variable. The assign allows to assign references in the HTML to variables in the form of {reference}. The render parses the template file and replaces the references with the variables. That's basically it. Very basic, very simple and a time saver for me.

I like this template engine because I hate to mix PHP with HTML and whatever. And so I'm not very fond of PHP itself as template engine (like WordPress templates), although very powerful for such thing. I don't like the idea of saving my files as .php files and then include/require them with a bunch of mixed PHP code, I like to keep it simple.

However, has rudimentary as this template engine is, it doesn't allow for conditions and loops, two very important things that sometimes are needed. So far I've been working around that issue by separating template files and then do all the conditions/loops in the controller. For instance, I have the main template file which has a <ul> list where the items come from a database, I'll just have a separate template file with one line of code for the <li> item. Do the loop in the controller and render the as many <li>'s as needed.

This was an introduction so you understand where I come from. Now to the real question...

I've been thinking and experimenting alternatives to this method and start using PHP in the HTML template files with as little code as I can. For instance, like this:

<ul>
  <?php foreach($array as $val): ?>
  <li><?php echo $val; ?></li>
  <?php endforeach; ?>
</ul>

And the in the controller something like this:

// assuming the data comes from a 
$array = array('Item 1', 'Item 2', 'Item 3'); database...

ob_start();
eval(' ?>'.$TPL->render('main').'<php ');
echo ob_get_clean();
  • First point I want to make is that this presents a problem for me. It kinda defeats the purpose of this template engine. There I am, calling echo $val where I should have a reference to be replaced. But wouldn't it be completely idiotic to have a reference to be replaced in the template rendering when I have the $val variable right there waiting to be used? But I also don't like the idea of using lots of PHP code in my template files, I would prefer to avoid as much as I could without compromising the whole thing too much. However, I don't see how could I have a reference in the place of the echo and have it replaced by $val for each loop. What your take on this?
  • Secondly, I would like your opinion and [more] issues that you think this solution might have. Or if there's some thing wrong in the code or how could it be improved, etc... I know I could use an extra method to do all the output buffering and eval to simplify the template rendering, but I'm looking for other stuff I may have overlooked or completely forgot about or simply missing the knowledge, most likely.

Maybe I'm thinking too much into this, maybe I should just forget about the whole thing and keep doing this as I've been doing for the past few years. It worked very nicely for me and simplified all my projects. In the end, I'm basically looking for a way to simplify conditions and loops in my template files without requiring extra template files, some with only a couple of simple lines of code.

A: 

I believe the way other languages address this is by offering server side markup, like the asp repeater control and I found this question here too which suggests you stick with your solution and gives a -1 to person who suggests rolling their own custom template tags to achieve the same thing... maybe it just doesn't get much simpler and building it is over-design.

Gabriel
Rolling my own has always been out of the question ;)
Nazgulled