What is a template engine?

5

Little by little, I learned about the template engine in PHP. By the way, I understood what it is, but I have doubts.

Is it a "programming language?" I put quotation marks on a website:

  

Purpose

     

At first, a template language or engine is used to, among   other things, speed up the work of those who will do the visual part of   a Web project.

• What really is a template engine? What's the use? Is it what is written in the quote above or is it something else?

• Can be created in other languages or just in PHP?

Could you give examples, if possible how to create one in PHP? I have no idea how to do it.

    
asked by anonymous 17.07.2017 / 16:03

1 answer

7

I do not like the word engine for engine in this context, I prefer mechanism.

PHP is already a feedback mechanism (since it is to translate). It takes a text that in thesis is an HTML and allows to insert some external things that will be defined at the moment of its execution.

So it's a bit strange to have other mechanisms for use with PHP. Yeah, I even understand that people wanted something more flexible and mostly with easier syntax.

The template engine is the software that takes text and substitutes parts of this text for some information to be defined, either by a code or by a pattern that will be defined by some rule fixed.

In general, they operate based on a convention that a particular character or string specifies that it is not a text, but rather that it should get a result that will be inserted into the text.

The way you operate from each one is something that it defines.

It is possible that it loads a programming language into it, but it is not the programming language.

The function of this mechanism is to make it easier to write a text in the more or less natural way a person would do and allow the customization of snippets of it. The alternative is to have to program everything and use the text as string literals in the code, which is not bad for a programmer, but can be tragic for a person who has another function, such as a designer or even another more lazy user, even because these mechanisms can be used in different contexts. Even in Microsoft Word or other vendors it has a template mechanism that interprets the text and can fill in the gaps left in the text, which curiously almost nobody knows of the existence of this, often reinvented the wheel in their applications.

So it's not that it can be used in other languages, it can be used on anything that requires text to be generated on a fixed basis but has gaps to be populated dynamically.

It does not really need to be text, but it's much more common and simpler to do.

For web-facilitating page creation, you can make the stream as close as you would if the page did not have dynamic content.

How to create a mechanism is something very broad, although a simple good can be done even with regex . Just say the pattern you will find in the text that is what should be replaced, get the content that is in there, interpret, in the simplest case it can be only a if , switch or an array associative * with keywords that should be replaced.

$template=file_get_contents('template.php');
$re = "/%(\w*?)%/"; 
$subst = "<?php $1(); ?>"; 
$result = preg_replace($re, $subst, $template);
file_put_contents('template.php',$result);

Example taken from this response in the SO .

I placed GitHub for future reference .

You have another question there with another example .

    
17.07.2017 / 16:24