Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the real advantages of templating engines over just using PHP?

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)
like image 898
Matteo Riva Avatar asked Feb 25 '10 14:02

Matteo Riva


People also ask

What is an advantage to using a templating engine?

A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.

What is PHP templating engines?

A PHP template engine is a concise syntax used by front-end developers to display data prepared by back-end developers.

Are templating engines necessary?

You actually dont need them, but they have a lot of features that makes your pages more dynamic..

When should you use a templating engine?

Template engines are used when you want to rapidly build web applications that are split into different components. Templates also enable fast rendering of the server-side data that needs to be passed to the application. For example, you might want to have components such as body, navigation, footer, dashboard, etc.


2 Answers

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"> <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/">you</a>.     {% 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.

like image 113
The Pixel Developer Avatar answered Sep 19 '22 06:09

The Pixel Developer


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.

like image 43
Dominic Rodger Avatar answered Sep 21 '22 06:09

Dominic Rodger