What is a template engine?

Asked

Viewed 432 times

7

Before long, I learned about the PHP template engine. By the way, I understood what it is, but I have doubts.

It is a "programming language?" I put in quotes put read this on a website:

Purpose

At first, a template language or engine is used to, between other things, streamline the work of who will do the visual part of a web project.

What is a template engine really? What is it for? Is it written in the above quote or is it something else?

Can be created in other languages or only in PHP?

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

  • 1

    Template engine applies to any language. It uses language resources so calling programming language is a strong means the most appropriate would be "a simplified/improved notation". In that question has a very basic example of template engine and other considerations.

1 answer

10


I don’t like the word motor engine in this context, I prefer mechanism.

PHP is already a feedback mechanism (since it’s for translating). It takes a text that in thesis is an HTML and allows inserting some external things that will be defined at the time of its execution.

So it’s a little strange to have other mechanisms for use with PHP. Okay, I even understand that people wanted something more flexible and especially with easier syntax.

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

In general they operate based on a convention that a certain character or string specifies that there is not a text but is something that should get a result that will be inserted in the text.

One’s way of operating is something he 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 give ease of writing a text in the more or less natural way as a person would and allow customization of snippets of it.

The alternative is to have to program everything and use the text as literal string 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 user more lay, because these mechanisms can be used in several contexts.

Even in Microsoft Word or other vendors has a feedback mechanism that interprets the text and can fill the gaps left in the text, which interestingly almost no one knows of the existence of this, often reinvented the wheel in your applications.

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

It doesn’t even have to be text, but it’s much more common and simpler to accomplish.

For web can facilitate the creation of pages by making the flow closer to how it would be done if the page had no dynamic content.

How to create a mechanism is something very broad, although a very simple can be done even with Regex. Just say the pattern you will find in the text that is what should be replaced, pick up the content that is inside, interpret, in the simplest case may be just one if, switch or a array associative* with keywords to 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 of that response in the OS.

I put in the Github for future reference.

Has another question there with another example.

  • 1

    Only the first sentence is worth +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.