W
W
WebDev2014-09-10 19:27:53
Template Engines
WebDev, 2014-09-10 19:27:53

What's the point of using templates?

Is there any advantage to using templates? In particular blade for laravel? In addition to the "purity" of the code, which is highly controversial

Answer the question

In order to leave comments, you need to log in

9 answer(s)
S
Sergey, 2014-09-10
Protko @Fesor

The template engine is different for the template engine. But in general, it is necessary to single out common tasks. which template engines should decide for you. I didn’t work with blade and I don’t see the point in eating twig.
Security. This can probably be taken to the top. A typical pattern in php templates is <?= $someUserInput; ?>. Often this can be found in the output of inputs, when generating search errors (they say "nothing was found at the request $userInput. That is, we insert the connection of our js scripts into the input, if this is a search form, we share it with a "friend" and take his session. Well, or what other fun things you can do. But everything is very easy to solve. We put some function that will filter XSS injections on output by default, and will not do this only if we ask. If you write simply in php, disgusting functions appear, which you can just forget to call.And with templating engines, we write beautiful {{ someUserInput }} and we can sleep peacefully.It helps to
comply with the DRY principle. Modern templating tools (twig for example) give you the ability to split templates into blocks, reuse them multiple times, highlight macros, inherit templates... in short, anything. if only you could reuse pieces of html and not copy-paste them.
Limit the flight of the developer's imagination. It's not news that developers are lazy assholes. Especially the young ones. If they suddenly need some data from the database in the template, or data associated with the request, most will not take a steam bath and will put the necessary code directly into the template. Some also sin by smearing part of the business logic according to templates. I also met projects submitted to the support, where dudes in templates parsed answers from a third-party apish via xpath (which was used instead of a database. That is, this matter was spread throughout the project). Refactoring in case of a change in the apishka will be a pain.
A good template engine should make it so difficult for you to write shitty code in templates that you would not want to do it and think how it can be done normally. As a result, we have clean templates that do not know anything about the business logic of the application and only know about the display logic, which is what we achieve by generally separating the logic from the presentation. It also makes life easier for the coder (if it exists separately) or for you in the future with a support.
On the other hand, the same twig allows you to expand the syntax of the template engine within the project, write extensions, in a word, do a lot of fun and necessary things that will reduce the time it takes to support templates in the future.
Since, in fact, we don’t pay anything for all these nice things (the template engine must compile all this into native php, so there simply won’t be an overhead), why not use it?

A
Alexander, 2014-09-10
@aspetek

Look at what it's about. Mixing layout and logic is still bad, and there are many reasons for that.
On the other hand, any template engines with specific markup are very inconvenient in my opinion.
I use this method myself:

function template() {
    if (!is_null($this->template) && is_file("template/{$this->template}.tpl.php")) {
      extract($this->data, EXTR_SKIP);
      ob_start();
      require "template/{$this->template}.tpl.php";
      $content = ob_get_clean();
      return $content;
    } else {
      throw new Exception('Не указан шаблон: '.$this->template);
    }
  }

In my opinion, the most simple and convenient option.

E
Evansive, 2014-09-10
@Evansive

Template engines are used to get a more beautiful and understandable result.
Specify what you mean. Your question is ambiguous - "why use templating engines, I combine logic and presentation and I need norms?", or do you not want to use special templating syntax in the view?
Template engines allow you to separate PHP code from presentation, which is why it looks nice.

H
HangGlider, 2014-09-10
@HangGlider

The order of using template constructs can be easily explained to the workbench.
What would translate your PSD immediately into a template and animate it with data transferred from the controller.
Of course, you can teach it to work with PHP, but the unnecessary flexibility of the language will only complicate understanding. Some workbenches see trying to get them hooked on PHP as shifting some of the work of the backend developer onto their shoulders :)

P
Pavel Solovyov, 2014-09-10
@pavel_salauyou

in short - template engines support template inheritance, not all web designers know php, so it's easier for them to deal with pure html, html entity conversion is enabled by default, which saves you from xss injection.

R
Roquie, 2014-09-10
@Roquie

Read about features like Twig. Mb penetrate. An interesting thing. Although when using Volt (read Twig) there are many goodies and syntactic sugar, as well as heaps of additional features for working with templates, but on small projects their use is questioned.
Many people confuse the concept of template engines. There are like Twig and Smarty with their own syntax, work with layouts. And there are ordinary PHP ones with short_tags ( @aspetek wrote an example), where there is no "language" of its own, but the logic is also divided.

V
Vyacheslav Plisko, 2014-09-10
@AmdY

Blade can be called a templating engine with a stretch, it makes a couple of simple substitutions for a more convenient syntax, while in the template you can open tesh

D
Dmitry Evgrafovich, 2014-11-01
@Tantacula

There has not yet been mentioned about such a template engine as liquid. In addition to aesthetic functions, it has a very important feature: it gives access to changing the view (that is, of the same template) and you can not be afraid that someone bad will insert malicious code into the template and hack the server.

G
GarrySeldon, 2019-07-10
@GarrySeldon

Template engines, in my opinion, are absolutely not a necessary thing, but at the same time, templates (representation), of course, are needed. What are the advantages of using template engines:
1. More "beautiful" and concise code. Instead of <?= $var ?> we write {{$var}}. To be honest, I don't see much difference here. If you need concise code, it is better to use widgets like in Yii1/2. Transferred the data, made the necessary settings and you have a ready-made table with sorting and all the things, and not a whole canvas of code.
2. Disciplines the developer and does not allow him to make SQL queries from the template. A very dubious argument, if the hands are crooked, you can always do something wrong.
That's where the pros ended, let's move on to the cons:
1. A lot of resources are wasted. PHP needs to open, read and parse the template. With regular templates, it's a little easier there.
2. In order not to do this resource-intensive operation, the template engine caches the results of its activity every time, and here we get the second minus - we have to constantly clear the cache. At the same time, the cache does not cancel queries to the database and other computational processes that are performed in the model.
3. You need to learn additional syntax.
4. Additional complexity creates additional problems. I repeatedly met sites where sometimes the data was not cached correctly and the site did not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question