views:

209

answers:

8

I develop my web applications using only PHP for the view files and I don't feel limited in any way, but I hear there's a consistent number of developers advocating "external" templating engines. So what do template engines offer that simple PHP lacks?

I'm looking for practical things, so I exclude the following:

  • babysitting bad developers (i.e. use a template engine because it forces you to not mix code into presentation)
  • conciseness of syntax (I have mappings in Vim for things like <?php echo $stuff; ?>, using curly braces wouldn't make any difference)
  • easier syntax for non programmers (I develop alone so that's not an issue)
+6  A: 

Separation of concerns.

This sort of things comes as standard when using a MVC/MTV approach - where the presentation of data is necessarily separated from the display of data, but not if you're using plain ol' PHP.

Using a template engine makes it easy to separate out what's being displayed from how it's being displayed.

I suppose you could argue that this falls under "babysitting bad developers", since a good developer ought to do this anyway, but in my view, a template engine makes it easier for good developers too.

Dominic Rodger
This sounds like the baby sitting argument to me.
EvilRyry
separation of concerns is a good thing at all levels; but it doesn't necessarily means to have each 'concern' in a different file. That's only important if you have different teams working on the design and on the logic.
Javier
@EvilRyry: It's more about engineering than baby sitting. Code *does* stuff, templates *display* stuff. The two are separate and unrelated. One should be able to change look without changing behavior. Really good PHP programmers might be able to do this, but a template engine makes it easier because you no longer have to sweat the details -- just hand data to the template engine.
S.Lott
@EvilRyry - totally agree with what @S.Lott said. I consider myself a reasonably competent developer, but unless the architecture enforces good habits (such as separating out display of data from fetching of data), I'll get lazy, and muddle the two up. In other words, I for one *need* baby-sitting.
Dominic Rodger
@Javier - it's not completely necessary, but it sure is helpful.
Dominic Rodger
I agree with both Dominic and S.Lott, I disregarded this reason in my question not because it isn't valid (it very much is) but because it's not a concern in my case.
kemp
+1  A: 

If you'd like to develop applications that can be customized with a lot of different templates and layouts while keeping the design separeted from the logic, e.g. for different customers, you may want to consider using a template system.

But if your applications just need one template and never change the layout a lot, then stick with what works for you, why change? :)

Select0r
+1  A: 

Some templating engines can compile templates leading to highly optimized transforms. Take for example the XslCompiledTransform in .NET.

ChaosPandion
+5  A: 

Easy switching between views

With the current state of the web, I need to provide my information in different formats:

  • A static html page
  • A dynamically loaded view on another HTML page
  • A JSON object of the data
  • An XML feed of the data, used in my flash part

The information for these formats can be equal, only the view differs. Using some sort of template engine, I can quickly switch between these views.

Scharrels
+1 - Think PC version and mobile version. This also makes it easier to style your pages differently for holidays or for special sales.
ChaosPandion
Agree on "using some sort of template engine", doesn't answer why an external engine instead of PHP.
kemp
+2  A: 

With templates you can also delegate the resposibilitys of the presentation to designers. The designers can create templates, and the developers can work in the logic. It is also easier to keep the presentation consistent.

leChuck
in my experience, this is the only valid reason. when working solo, it's easier to separate 'model logic' in its own file, and put 'view logic' at the top of each page's PHP, with the bottom for 'template PHP' code, with just one-liner variable and looping inserts.
Javier
+3  A: 

New Syntax


Some people wont agree, but since I've been using Twig the "for ... else" feels right. It might not be a lot, but it keeps my templates that little bit cleaner.

{% for row in articles %}
 Display articles ...
{% else %}
 No articles.
{% endfor %}

Automatic Escaping


You can have the template engine automatically escape any output. This is great as you no longer have to repeat htmlspecialchars ... everywhere. Twig does this nicely.

{% autoescape on %}
  Everything will be automatically escaped in this block
{% endautoescape %}

Template Inheritance


Another feature I like is the ability to extend base templates. Here's a basic example

base.html template

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
  {% block head %}
    <link rel="stylesheet" href="style.css" />
    <title>{% block title %}{% endblock %} - My Webpage</title>
  {% endblock %}
</head>
<body>
  <div id="content">{% block content %}{% endblock %}</div>
  <div id="footer">
    {% block footer %}
      &copy; Copyright 2009 by <a href="http://domain.invalid/"&gt;you&lt;/a&gt;.
    {% endblock %}
  </div>
</body>

child.html template

{% extends "base.html" %}

{% block title %}Index{% endblock %}
{% block head %}
  {% parent %}
  <style type="text/css">
    .important { color: #336699; }
  </style>
{% endblock %}
{% block content %}
  <h1>Index</h1>
  <p class="important">
    Welcome on my awesome homepage.
  </p>
{% endblock %}

A child template can override blocks for page specific styles, content, etc ... You can also notice the use of {% parent %} which grabs the parents content so you don't lose it all while overriding.

I recommend you give Twig a go. Extremely useful.

The Pixel Developer
Is it just me, or does that look a *lot* like Django templates?
Dominic Rodger
Might be, I'll have to see what Fabien has to say.
The Pixel Developer
A: 

Your non-answers look like real answers but phrased in a very condescending manner. For example:

babysitting bad developers (i.e. use a template engine because it forces you to not mix code into presentation)

I would call this an application of the Rule of Least Power. It makes your template much more useful for all users, not just "bad developers".

Restrictions are what make programming languages. PHP doesn't have an inline-assembly feature, and it's not because Rasmus thought you are all "babies".

Ken
My question, my rules :)
kemp
A: 
  1. I use my own "template" engine, quite basic stuff, assign value to [key] and the sorts.
  2. When I first looked for a template engine I found smarty, but it had so many security problems I ended up writing what I needed myself.
  3. You ask why ? because it has many features that can make your coding faster (stuff what you didn't think of and stuff you can delegate to the template system instead of your code)
  4. The majority of coders out there have chosen a template system and when working in a team you need to keep a standard.
DCC