Pure PHP / HTML Views

I would like to know which approach is faster, using pure PHP in HTML files or using template engines such as Smarty, Twig, ... What I would especially like to know is: what is analyzed faster, for example, Smarty cache faster than using pure php? Which of the template engines is the fastest? I'm going to rewrite a simple application where speed comes first.

+9
source share
5 answers

"Depends" is the answer to all your questions.

What is faster? Time of completion? Development time? Service? Memory overhead? Their mixture? The template engine usually trades with some performance (speed, memory) for better development and maintenance.

If you are talking about purely dynamic templates (meaning: a template is evaluated on every request), PHP will overtake any template engine. This is actually a noobrainer. If you take into account caching, a template engine such as Smarty can help. Caching is not something that you could not implement in plain PHP. With Smarty, this was done for you (and on a much more complex level than you could).

If you use a framework of, say, Symfony, it would be wise to use Twig, since Twig and Symfony are tightly integrated. Of course, you can use Smarty or regular PHP. The question here is: is this possible?

Caching makes sense when creating sites from data sources, such as a database or remote APIs. What you really save (in the sense of reduction) are database calls, intensive computing, etc. Check if you have any time-consuming features to build your site. If so, use caching (if you can).

Knowing the trade-offs between development / maintenance / usability / performance, I would (always) recommend using a template engine. As a Smarty developer, I certainly suggest using Smarty. That is, if you are not using Symfony, then you might be better off with Twig. Or some other framework with another template engine.

Please ignore messages like Smarty vs. Twig , as they compare only a very limited view of engines. Do not trust the tests that you yourself did not pretend to be β„’.

In general, Smarty 3.1 is slightly faster than Twig. Twig does a lot of things at runtime (the time when the template runs) that Smarty does at compile time (the time when the template gets ready to run). The twig here does not really offend speed. Twig has to do certain things during project execution. They spent a little performance for the sake of "convenience" (for example, access to arrays and objects with the same designation).

+22
source

Let me separate the paths associated with this subject separately:

1. Get the logic out of the presentation - don't put the "code" in your HTML

Anyone who says this and then tells you to go with the template is contradictory:

  • PHP is an interpreted language - it becomes C executable code.
  • Template Template Syntax Interpreted in PHP

They must stop lying to themselves. Their "template syntax" is a programming language built on top of another, which, in turn, is built on top of another language - it is inefficient, redundant and strange.

In addition, I do not see how the very existence of variables in which each template engine hangs is not considered logic. Their existence, content and implementation depend on the logical backend.

And which of these template systems with if / else statements and for loops? This is the very essence of logic - the very concepts that most programming languages ​​use. They require variable data that can be generated or exist only through some form of computation.

You cannot serve dynamic content without mixing presentation with logic. It's impossible.


2.1 This is safer ...

So, you do not trust your boyfriend HTML?

Case: You think your HTML / CSS guy is stupid and accidentally prints the database password

If so, I have news for you - your environment is already insecure if access to sensitive data can be changed from anywhere in the program.

Case:. You think your HTML guy will print random server constants - it's dangerous to let him, as a person, work with server logic

I see that he is either stupid or hates his job, and wants to be fired, and therefore will do something dumb, like print session variables. Great, but I will say ...

... Why the hell is this material not up to expert judgment ? Even if he did not have access to the direct logic of the server, but rather a bizarre template system, he could still spread his stupidity / hatred just because he has a final opinion about the exit. Or he may even conspire with another programmer (if any) and still access the server and co constants.

-

2.2.1 Good template engines automatically sanitize the output or let the template designer do it themselves - he knows better when the data needs to be sanitized

You are fictitious.

You do not know when to disinfect the release? You could not do this ..?

Even so, you may be just a monkey of code, and the HTML guy is an expert in embedding HTML technologies for web security, and he should be the only disinfectant product. In this case, giving him access to PHP, he also allows you to use similar htmlspecialchars() , and not that the template allows him to do the same.

As for automatic escaping, provided that you are transferring content safely, you can implement such a simple function in the code that you do.

-

2.2 ... and I can control what data is processed using

Think about classes, functions, etc. - You enter data, they work with it, then you get the result. As a rule, they do not deal with external data, unless it is handed over to them (otherwise, fuzzy, dangerous and bad practice - some constants aside). Using these same methods, you can convey exactly what you need for your output in an effective, clear and unlimited estate.

-

All that has been said is similar to the fact that, in your opinion, your template engine is more secure than regular code, because you are missing in several areas of general security:

  • You (or anyone else) did not view the review content - you allow individuals to display the content.
  • You do not apply the correct or safe programming methods and do not seem to understand that you can control the transmission from point A to B.

3. PHP syntax is too complicated / hard to teach people style

True, this is no more complicated than the psuedo syntax created by template systems such as Smarty, so if this is a problem, then dynamic content is not for you.

PHP's short syntax contains the following: - Is it too complicated?

<div class='username'><?= $username ?></div>


4. It is too much to develop my own solution.

Although I would not argue, you can choose whatever you want! Choose everything that best suits your needs. They are usually free, not difficult to integrate, and come with many features out of the box.



I get the impression that most people choose templates simply because they look "leading" in the file - they like to think that the TPL file is a special thing that they created, they like the syntax; It’s as if by some magic the variable is β€œcalled” by the small symbol @ or # and jumps from your logic to the output.

It looks like a trick - a beautiful sorceress (AKA The templating engine) attracts you with its beauty. Although eye-catching, it does bleed the demon and extracts your soul (server resources) in exchange for eye candies that no one else sees (your users will likely have a faster website and more features funded by $ $$ you 'saving when renting electricity / server)

 <title>{{@title}}</title> Vs <title><?= $title ?></title> 


I agree, there is only one case that I can think of, in which the templates have some kind of foundation for PHP - Portability for other applications. appartisan responds to these addresses. However, it’s easy to replace <?= $var ?> {{@var}} - this is the job for templating-esque.

+9
source

Simple and pure opinion, I think the only advantage is portability. You can reuse templates or views from the template engine to another backend application. Suppose you are moving an application from PHP to Java, you do not need to refactor the templates.

Otherwise, you add complexity, add a different level of execution (more time), more requirements to support the application (you need people who know this template mechanism) and so on. PHP itself is the best and more functional template engine you are going to get, possibly the fastest, and you can also cache with the advantage of managing the cache from the backend application rather than from the view.

+5
source

I will review this again, as the situation has changed significantly, and there is some evidence from an earlier answer.

Without delving into why frameworks use templated mechanisms over PHP that do the most. For some reason, there is a constant attempt to β€œfix” PHP with a different level of abstraction. Always with claims to simplicity without loss of versatility or performance.

Despite this, using PHP is still the fastest and most universal way of templating. PHP in its earliest incarnations looked like a template language. But let's take a look at the improvements in PHP and place them next to the after layers .

Twig and some others claim to cache something that has always been an addon in earlier versions of PHP. Caching is now a standard part of PHP5.5 + (Opcache), so using PHP as a template language will give more performance improvements.

Twig and others require simple syntax for designers. Comparing the syntax of the template engine , you will see that the logic is similar to the only advantage of using a template system, such as Twig, which is another layer of security sharing between the designer and the underlying system code.

Two very popular CMS Wordpress and Drupal used PHP as their template engines. Thus, the old argument for using the template engine to protect and simplify the use of PHP when developing a website is actually not valid on today's network. While Drupal 8 is migrating to Twig, this is mainly due to the fact that the branch is part of the Symfony Framework (back to why frameworks use templating mechanisms). Wordpress, on the other hand, still uses PHP. Because Wordpress is growing by leaps and bounds, with the help of web designers using PHP to make this happen. The Drupals community has also been partially divided by decisions to use Twig and Symfony.

Thus, it seems that using PHP is the best choice in terms of performance, as well as a preference for developers and designers. At least all the evidence leads to this conclusion.

This says my unfounded opinion. I think that using anything other than PHP as the template engine in today's website covers some of its inherent weaknesses in the basic structure or architecture of web applications. This weakness is complexity and complexity that cannot be easily explained at the level of the designer or user.

If you are writing a small application that should be small. Keep it small and run optimally with PHP and leave other modules for groups and enterprise-level projects

+4
source

I have a problem with the argument that the logic and data mapping should be as separate as possible. I found that validating and displaying data actually requires a lot of logic for the forms. Information about the data type, range of numbers, the relationship between different data requires a lot of code. The real question is whether to use the server-side template language or client-side Javascript. Using Ajax and client code to display and verify data, I get very little template code. The biggest problem with template engines is the introduction of new rules and code syntax. I see the future with PHP, jQuery and Ajax and templates, losing appeal.

0
source

All Articles