Home » Php » php – What is a templating language?

php – What is a templating language?

Posted by: admin April 23, 2020 Leave a comment


I was reading somewhere that PHP is a templating language. What is exactly a templating language? What makes PHP one? What are the other templating languages?

How to&Answers:

The premise behind a template language is that the language is “embedded” within some other master document. Specifically, on your average document, the total size of the document is mostly document source than template language.

Consider two contrived examples:

print "This is a simple document. It has three lines."
print "On the second line is my name: " + firstName
print "This is the third line."


This is a simple document. It has three lines.
On the second line is my name: $firstName
This is the third line.

You can see in the first example, the language wraps the document text. In the second example, the document text is the most prevalent, with just a little bit of code.

Some template languages are full blown general purpose languages, such as PHP, ASP.NET, and Java’s JSP. Others are more limited designed specifically for templating, such as Velocity and FreeMarker (both utilities for Java).

Many word processors, such as Microsoft Word, have their own templating capabilities, commonly referred to as “Mail Merge”.


A templateing language basically is a language which allows defining placeholders that should later on be replaced for the purpose of implementing designs. Obviously modern template languages not only support placeholders, but also loops and conditions which are often necessary for designing a web page. Some even support more advanced but still useful techniques like template inheritance, macros and sandboxing.

About PHP: As PHP itself might be interpolated into HTML it can be used as a template language. On the other hand PHP is pretty verbose, especially if short tags are not enabled.

A fast and feature rich template engine is Twig. I would recommend it over smarty. It offers more features (most notably template inheritance) and is faster.


PHP isn’t necessarily a templating language, but it can kind of pass that test due to the way it is interpreted.

When the PHP file is read, only code within PHP block tags (<?php and ?>) is looked at, the rest is passed to the output. So a .html file could be processed by the PHP interpreter, and nothing would happen, the HTML would be output.

If some of the areas of the HTML file had PHP tags and code, it would be interpreted and likely output some data in those locations. This could be considered a template. Usually it’s the idea of having an output layer, and then having dynamic content that goes with it. You could have a product page template, and then based on some input variable, populate just a single products details/images/etc into the template.

There are actual template engines for PHP (Smarty for one.. http://www.smarty.net/) that you can take a look at.

I’ve never been a fan of them, but a lot of people find success with it.


You might read on Smarty 3.


eg. it has template inheritance now