Jump to content

Archived

This topic is now archived and is closed to further replies.

GrooN

What’s wrong with Template Engines?

Recommended Posts

Recently I’ve noticed that wherever you go, whatever project you take a look at, 50% of them don’t take the path between PHP and HTML serious. They embed PHP directly in their HTML code.

 

Back when PHP was a little squishy language, it was merely considered a kind of template engine, and it was common practice to combine PHP and HTML in a single file. Now PHP has developed to a powerful scripting language, and it is now capable of performing even difficult computations with decent performance. This development of PHP results in more complex algorithms and thereby more code, and mixing this with HTML can produce a whole bunch of spaghetti code, and it is therefore considered bad practice. Because of this it is more usual to use what is known as template engines, whose goal is to make the transformation from PHP to HTML as smooth as possible.

 

The basic principle is to make an even simpler scripting language (than PHP) to mix with the HTML, and thereby increase the readability, and also give designers who do not know of PHP a chance to design with as much ease as possible.

 

Out on the template engine market, there are so many tasty engines to choose between (such as Smarty, PHPTAL, Rain TPL, XTemplate etc.). And even with a so big market, I still see serious projects which do not harvest the power this gives, why is this, can’t people get on? – are they stuck in the good old days?

 

TLDR; why doesn’t people use template engines?

 

(And... If you've understood the importance of Template Engines, which is your favorite?)

Share this post


Link to post
Share on other sites

There are ways of separating your business logic from your markup that don't involve the overhead of a template engine. The mvc pattern comes to mind.

 

Why throw some other syntax variant into the mix when you can achieve the same results using a little PHP?

Share this post


Link to post
Share on other sites

True there is definitely a performance barrier, but using the PHP inside the HTML can easily become unmanageable. I see your argument with the MVC pattern, adjusting the variables in the controller before sending them to the viewer. But still the benefit of a simpler formatting gotta kill the performance issue.

Anyway it would be acceptable on a smaller project, but I just can't help seeing the pros overcoming the cons in projects of larger scale.

 

What is your personal preference Thorpe?

Share this post


Link to post
Share on other sites

My preference is to not introduce some other syntax for no real benefit. If your designers need to learn some template engine syntax why not have them learn some basic PHP? You generally don't need more than simple loops and conditionals.

 

At my work we have massive scale applications and no issue with mixing minimal PHP and markup. It's easier and quicker. I really just don't see the point in adding another layer.

Share this post


Link to post
Share on other sites

Interesting, I just wanted to see what opinions people had and so far I've got one, so I am very satisfied.

 

To be honest I cannot myself come with any counter arguments other than keeping the HTML and PHP apart, which, if your designer is able to perform simple loops and conditionals, is rather irrelevant, as long as you keep it at a minimum. The thing that really scares me, is when people throw all their algorithms and computations inside the same document as the HTML code itself.

Share this post


Link to post
Share on other sites
The thingthatreallyscares me,iswhen people throw all their algorithms and computations inside the same document as the HTML code itself.

 

That is just generally poor coding unless it's a very simple script. Unfortunately however, I don't believe poor coding skills can be improved by simply throwing tools at the problem and hoping for the best.

Share this post


Link to post
Share on other sites

True, it is generally poor coding, and tools probably won't help... It's just sad then.

Share this post


Link to post
Share on other sites
But still the benefit of a simpler formatting gotta kill the performance issue.

 

Give me an example where templating looks/feels a lot better than straight PHP. 95% of the time I'd say PHP would be on par or you're trying to do too much in your views.

Share this post


Link to post
Share on other sites

A syntax looking/feeling better than another syntax depends on the person, otherwise why would there be so many different programming languages?

Anyway, it would be incorrect to state that I only use templates myself, in fact I am a loyal CodeIgniter user, which as you may know use the MVC design pattern. The purpose of this thread was simply to provoke people a little, and get some opinions toward Template Engines.

Share this post


Link to post
Share on other sites

I also just use plain PHP for template purposes.  As far as control structures such as loops and conditionals go it's native format is not much different than any other template engine I've encountered, and is far more flexible if needed.  I have a class and small collection of additional functions that I used to make certain things easier/shorter inside the template, such as:

 

function sel($value, $options){
if (is_array($options)){ 
	foreach ($options as $optval){
		if ($value == $optval){
			return 'selected="selected"';
		}
	}
}
else if ($value == $options){
	return 'selected="selected"';
}

return '';
}

 

Which would be used like so:

<select name="state">
<?php foreach ($StateList as $state): ?>
	<option value="<?=$state['abbr']?>" <?=sel($state['abbr'], $Defaults['state'])?>><?=$state['name']?></option>
<?php endforeach; ?>
</select>

 

 

The same thing in say smarty would look like this

<select name="state">
{foreach from=$StateList item=state}
	<option value="{$state.abbr}" {if $state.name == $Defaults.state} selected="selected" {/if}>{$state.name}</option>
{/foreach}
</select>

 

It's really not that much different from the plain PHP method.

Share this post


Link to post
Share on other sites

PHP is a template engine itself.  All that other engines do is add syntactical sugar on top of it.  Unnecessary, IMO.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.