PHP VS Template Engine

Pure PHP/HTML views VS template engines views

"Depends" is the answer to all your questions.

What is "faster"? Execution time? Development time? Maintenance? Memory overhead? A mixture of them? A template engine is usually trading in some performance (speed, memory) for better development and maintenance.

If you are talking about purely dynamic templating (meaning: template evaluated on every request) PHP will outrun any template engine. This is a nobrainer, really. If you're taking caching into account, a template engine like Smarty may help. Caching is nothing you couldn't implement yourself in plain PHP, though. With Smarty it's just been done for you (and on a far more sophisticated level than you possibly would).

If you are using a framework, say Symfony, it might be wise to use Twig, as Twig and Symfony are tightly integrated. Sure you can use Smarty or plain PHP. The question here is: is it practicable?

Caching makes sense when building sites from datasources like a database or remote APIs. What you are really saving (in a sense of reducing) here are database calls, intensive calculations, etc. Check if you have any time-intensive functions running to build your site. If so, use caching (if you can).

Knowing development/maintenance/convenience/performance trade-offs, I would (always) recommend using a template engine. Being a Smarty developer, I'll, of course, suggest using Smarty. That is unless you're using Symfony, then you might be better of with Twig. Or some other framework featuring some other template engine.

Please ignore posts like Smarty vs. Twig, as they only compare a very limited view of the engines. Don't trust benchmarks you haven't faked yourself™.

In general, though, Smarty 3.1 is a bit faster than Twig. Twig is doing a lot of stuff at runtime (being the time when a template is executed) that Smarty does on compile time (being the time when a template is prepared for execution). Twig is not really pissing away speed here. Twig needs to do certain stuff at runtime by design. They traded a bit of performance for a bit of "convenience" (Accessing arrays and objects with the same notation, for example).

Why should I use templating system in PHP?

Yes, as you said, if you don't force yourself to use a templating engine inside PHP ( the templating engine ) it becomes easy to slip and stop separating concerns.

However, the same people who have problems separating concerns end up generating HTML and feeding it to smarty, or executing PHP code in Smarty, so Smarty's hardly solving your concern separation problem.

See also:

  • PHP as a template language or some other templating script
  • What is the best way to insert HTML via PHP

Why use a templating engine with a framework?

I have two really good reasons I can think of for doing this...

  1. Creating markup that is repeated throughout the site in a consistent format is easier, plus you can update it later without a lot of grepping.
  2. If this is for a real company, the people determining content aren't likely to be really familiar with HTML, much less PHP. Having a simple template language that keeps styles looking the right way and generates valid markup without much code knowledge is really handy.

PHP Template Engine and CMS

When you're talking about a template engine, all this means is subdividing PHP which will be reused into separate files and then including them in other files where they are needed. This is useful when you have headers and footers on your website that will remain the same (or almost the same) site-wide.

Content management systems are really great when you're making websites for a person with little or no programming knowledge. Personally, I use WordPress; I find it's the easiest CMS to learn and also has the largest community of developers. Other CMS's are Drupal, and Joomla! I prefer not to use Drupal or Joomla! because I'm not as comfortable, but I suppose it's up to the situation provided and up to the developer's desire.

Using template engine twig in cakephp?

Visit another question "Pure PHP/HTML views VS template engines views" for some insight into the templating engine vs php topic.

However, since you reference CakePHP, I would recommend against using a templating engine unless you require it. In my opinion, if you can markup a page in Twig or Smarty, you should be able to code a view page in CakePHP.

A big benefit of some templating engines is the built in caching functionality but you can get much greater flexibility by using CacheHelper.



Related Topics



Leave a reply



Submit