Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I work on a CakePHP app and the views consist of raw html with embedded php echo statements, which over time has gotten rather messy. Before I go in and rewrite the code, I'm wondering if it makes sense to rewrite the views with a template engine, such as Twig or Smarty, but I don't have experience with template engines. What are all the advantages of using a template engine?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

Yes, definitely worth it.

While PHP is sort of a template language, the syntax is, as you have observed, clumsy and does not help readability much. Short tags alleviate the verbosity a bit, but as soon as conditionals and iteration come into play, things do get messy.

Also, PHP does not take care of encoding output for you; if you do the following:

<a href="<?= $_GET['some_url'] ?>">Click here!</a>

...then you have yourself a big fat XSS vulnerability; a proper template engine defaults to HTML-encoding any variables you interpolate, unless explicitly overridden. This means you can't accidentally run into this common security pitfall.

With these two in mind, compare:

<ul>
    <?php foreach ($items as $item): ?>
    <li><a href="<?= htmlspecialchars($item['url']) ?>"><?= htmlspecialchars($item['title']) ?></a></li>
    <?php endforeach; ?>
</ul>

vs.:

<ul>
    {%foreach items as item%}
    <li><a href="{item.url}">{item.title}</a></li>
    {%end%}
</ul>

Another advantage, more a psychological one than technical, is that a template engine enforces (or at least strongly suggests) that you keep you presentation code separated from application logic.

One thing to keep in mind, though; PHP was not intended for writing parsers and such, and so template engines written in pure PHP can be quite slow. Some of them are implemented in a more suitable language and exposed as PHP modules; you may also find others that can compile to raw PHP, which, once compiled, can be called directly.

share|improve this answer
    
Is the syntax in your example from a particular template engine? –  dandrews Sep 18 '12 at 22:08
    
@dandrews: It's a hypothetical language, but it's close enough to existing ones. –  tdammers Sep 19 '12 at 5:21
1  
Comparing bad plain PHP and good template code is not fair. If you validate input in the application logic, use the alternative syntax and create safe output functions with short names, plain PHP would look much cleaner. It might not be as good as template syntax, but the difference would be smaller. I agree with the psychological point anyway. –  lortabac Sep 19 '12 at 9:36
1  
@lortabac: none of my examples do any input validation, because that's outside the scope of templates. A shorter output function name (e.g. aliasing htmlspecialchars() to h()) reduces typing effort, but doesn't take the security/convenience problem away: it's still equally easy to forget the html-encoding. I've added the alternate syntax to my example, but IMO it doesn't do an awful lot to increase readability. –  tdammers Sep 19 '12 at 13:38
1  
@Mahn: html-encoding belongs in the view, not the controller. The controller should not care at all what the output format is; it could be HTML, JSON, plain text, EBCDIC-encoded INTERCAL source code, it shouldn't matter. The controller hands raw data to the view, and the view takes care of massaging it into the correct format for presentation. HTML-encoding is not data processing, it's presentation logic, and it does belong in the view. –  tdammers Sep 22 '12 at 9:43

I have been using MVC design pattern for a few years.

MVC allows you to separate your business logic (Model) from your user interactions (controller) and templates (views). I've worked with Zend_View for a long time, and I wouldn't use a template engine with it. http://framework.zend.com/manual/1.12/en/zend.view.html

share|improve this answer

Im my opinion, a template engine is only worthwhile if your project requires the ability for users to create custom themes. You spent a lot of years learning PHP right? Unless you want other "non php" people to edit the theme then just use PHP in the layouts. A templating engine takes time to learn, and adds an additional overhead to your script.

share|improve this answer
1  
I know Python far better than any templating language, but outputting HTML (for instance) from Python is awful using just print without a lot of declarative support code (allowing things like with tag('p'): text('Hello world'), and that's actually much prettier than any real output code I ever wrote), and even then it doesn't close to the clarity and clutter-freedom of a good templating language. Have you ever used a good templating language? It's a bliss! –  delnan Sep 18 '12 at 18:41
1  
"Takes time to learn" is not, in itself, a good reason to not consider using something. Furthermore, I would be interested in what your profiler reports say about the "additional overhead" you claim exists. –  user16764 Sep 18 '12 at 19:56
    
i think he meant "additional overhead" as far as maintenance is concerned. If you have to train new employees to use the template even tho they already know PHP, that's overhead. But I still don't necessarily agree with this answer as it currently stands. –  David Peterman Sep 19 '12 at 15:33
    
User16764 - I have no idea what my profiler says, as I don't use templating engines unless the project requires non coders to be able to edit layouts. Logic would say though that any additional scripts require extra processing power/time even if it is only a small amount. I use a strict MVC coding pattern, usually under codeigniter so the only PHP that ends up in my views is if/else statements and loops. With the use of alternative syntax the code in views amounts to the same if not less than template syntax and I didn't have to waste productive money earning hours learning something else –  Liam O'Neill Sep 19 '12 at 20:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.