Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a templating language?

Tags:

php

templating

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?

like image 569
Nandini Bhaduri Avatar asked Oct 26 '10 18:10

Nandini Bhaduri


People also ask

What are templating languages used for?

A templating 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.

Is HTML a template language?

The HTML Template Language is easy to learn and its features are purposely limited to ensure that it stays simple and straight-forward. It also has powerful mechanisms for structuring the markup and invoking logic, while always enforcing strict separation of concerns between markup and logic.

Is JavaScript a templating language?

JavaScript templating refers to the client side data binding method implemented with the JavaScript language. This approach became popular thanks to JavaScript's increased use, its increase in client processing capabilities, and the trend to outsource computations to the client's web browser.


2 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." 

vs

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".

like image 58
Will Hartung Avatar answered Oct 04 '22 16:10

Will Hartung


A templating 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.

like image 43
NikiC Avatar answered Oct 04 '22 17:10

NikiC