PHP vs template

I am currently discussing the choice between PHP as a template engine and a template engine on top of PHP.

What is your choice and why?

I say why use a different template engine when PHP is the template itself.

+59
php templates
Apr 08 '09 at 20:43
source share
20 answers

For template engines:

  • Added security to configure the end user. Themes in pure PHP have unlimited ability to harm the user and their installation. Thus, the template engine removes this risk if it is good.
  • Ease of use for non-programmers such as graphic artists or web designers.

For plain-php:

  • The speed of pure PHP cannot be matched with any template engine built on it.
  • The full power of PHP is available for exit, not just for the interpreted or filtered part.

I prefer PHP itself, if at all possible. And most people do not want to hack your software, creating a special topic, so it’s easy to read and explore its security quickly. However, I am “between a guy” who performs both templating and programming, and even some graphics; my skill set is different from a strict programmer and a strict artist / designer.

+47
Apr 08 '09 at 20:53
source share

I found that when I introduced Smarty, it was quite simple to get web designers to create HTML files with flexible variables. People in the development team are now focused on more extensive work, i.e. In the production of the contents of Smarty variables.

This shortened the development life cycle, while the work could be divided between a large number of people and ultimately led to better projects.

+37
Apr 08 '09 at 20:51
source share

Well, this is just my opinion, but the engine templates suck. You must first understand how the template engine works, and then learn how to use it. This seems to be a waste of time, because only PHP does it best and offers much more flexibility.

+22
Apr 08 '09 at 20:47
source share

The following reasons apply:

  • Dividing your application into templates using a mechanism makes your application less vulnerable to stop code errors.
  • Using templates can give you more flexibility in the future when refactoring, as the namespace will not be directly embedded in the application
  • The use of templates encourages developers to support business logic and OUT code from the presentation layer.
  • Using templates, it’s easier to create mockups of data sets and transfer them to the template engine and get a preview of how the data site will look.
+14
Apr 08 '09 at 20:53
source share

Using the template engine can be useful if you have a non-programmer making templates. In many cases, a simplified template language may be easier for a non-programmer than PHP itself.

However, I find that I am moving away from using templates when it is just me (or I and other developers).

+10
Apr 08 '09 at 20:46
source share

PHP is not a template engine, but a language that can be used to write templates or template engines. The template engine is not only a language, but also a programming API that allows scripts to find, organize templates, or assign data to them from a script. Pure PHP offers you absolutely nothing - it's just a language. Instead, you should take libraries such as Zend_View in the Zend Framework for comparison (basically, it works just like Smarty, except that it uses PHP to write templates). You should ask if you should use the template engine with PHP or something else as a template language.

When it comes to the templates of the languages ​​themselves, then it’s good ... normal loops and conditions are enough to write templates, but this “enough” does not mean that it is easy, convenient, efficient or flexible. PHP does not offer anything special for template designers, but many "template languages" (like Smarty) provide only a limited subset of PHP, so I'm not surprised that programmers choose PHP. At least they can write functions and use OOP, which are too large for this (in my opinion), but really help and really help.

The fact is that custom template languages ​​are not limited by the disadvantages of PHP, but their designers do not see it, claiming that "displaying variables and a loop is enough." Possible areas where template languages ​​can be much more efficient:

  • Display and rendering of forms (I did not see any frameworks with PHP as a template language, which provided an easy, flexible and universal system for adjusting the appearance of the form).
  • Understanding the structure of an HTML / XML document.
  • Automatic filters for injection XSS.
  • Solving various common problems at the presentation level (for example, customizing the appearance of the paging system, displaying data in columns, etc.).
  • Template portability and true separation of application logic and implementation details from templates.

Examples of language templates that follow this method are mentioned above by PHPTAL and Open Power Template 2. Some similar ideas can also be found in TinyButStrong, but unfortunately this template engine is very slow.

+10
Jul 10 '09 at 16:40
source share

PHP is great for most tasks, but a template engine can make it easier to scale a project.

Off a shelf such as Smarty or PHPTAL is great if you don't have time to roll on your own (and don't require more than what they offer). In addition, you can quite easily replace or change them with your own version later if you find that you need something more specialized.

I personally had good experience with PHPTAL, primarily because it does not work and is simple.

+5
Apr 08 '09 at 21:12
source share

PHP as a template engine will not complain when you mix your HTML syntax. This will allow you to forget to close tags, set them incorrectly, etc.

PHP output is not escaped by default, so if you haven’t noticed to strictly add htmlspecialchars() everywhere, your site will have HTML injection vulnerabilities (XSS).

 <p>Hello <?= $name ?></b> <!-- Simple template full of errors --> 

These problems are much worse if you are trying to generate XHTML correctly. It’s not that you cannot do it with simple PHP - of course you can, but it takes a lot of effort and diligence.

That is why my recommendation is PHPTAL . OPT2 is fine.

+5
May 25 '09 at 19:57
source share

Savant is what you are looking for. Its a cool wrapper class that allows you to use PHP templates in your templates instead of interpreting the new template language on top of PHP.

Pros:

It makes sense not to add extra work to the system.
Learning Curve for Developers If you are all disciplined, this is the way to go. (Savant)

Minuses:

Although Savant encourages you to separate properly, it does not force the developer to separate business logic from development code. I have a rule that should never be broken. You can only display variables, use conditions and loops in your templates. You should never let a developer create variables in a template. Unfortunately, developers never do this no matter how many times you tell them. Thus, the use of an engine such as Smarty becomes so, because developers are forced to fully support business and design.

If I am doing a project for myself or someone who does not have many developers, I prefer to use Savant. If his project is much larger than just me, then in architecture I will go for something like Smarty.

In any case, code separation is an important weather in which you use Savant or Smarty. I am sure there are other good options.

+5
Nov 30 '09 at 18:05
source share

I found that creating a lightweight template template in PHP works best for us. Allows for good code separation, and our graphic designer could learn a few simple rules to follow, but most write HTML / CSS, not PHP. I can write heavy lifting code without thinking about the interface.

+3
Apr 08 '09 at 20:54
source share

The following article summarizes various points of view on Template Engines for PHP.

Running PHP Templates of the Third Kind http://www.tinybutstrong.com/article_3rd_kind.html

+3
Apr 01 2018-11-11T00:
source share

PHP is not a template for its scripting language.

+2
Jun 04 '09 at 14:11
source share

My choice for any new project would probably be to simply use the PHP templating capabilities, perhaps in conjunction with the MVC framework, instead of using another template engine. After all, for simplicity of code or purity of separation, it doesn't matter if you have {$myVar} or <?= $myVar ?> In your code. More complex template functions, such as conditions, loops, or backend technologies such as caching, can also be processed (or better) using PHP or the MVC framework.

I used both approaches (with and without a template engine), but only in personal projects, never in team projects, so there may be good arguments for using the template engine in such a setting.

+2
Jun 04 '09 at 14:22
source share

I don’t know if this is related to VirtueMart, Joomla or the concept of templates in PHP, but setting VirtueMart to your own graphic theme is PURE HELL. (I'm not talking about simple CSS style).

+1
Jun 04 '09 at 13:35
source share

PHP coding functions have evolved, the design function is the same, so if you work in a team with designers and developers, you need a template engine to parallelize the job and let designers work with HTML.

My personal choice is Raintpl, because it is easy, friendly and fast. A benchmark can be selected here (also smart and smart - it's nice!):

http://www.raintpl.com/PHP-Template-Engines-Speed-Test/

+1
Jan 10 2018-11-11T00:
source share

I recently wrote a blog post.

For security reasons, of course, use the template engine (Twig is secure).

A good template engine (e.g. Twig) offers:

  • Security (the most important thing that ever)
  • Not verbose (e.g. php)
  • Additional template features
+1
Aug 08 2018-12-12T00:
source share

If I had to spend time exploring the standalone template engine, I would rather spend time exploring the Framework. My choice is to go with the Zend Framework, which when implemented using the MVC approach provides you with the power of the framework, as well as your own PHP templating capabilities.

0
Apr 08 '09 at 20:54
source share

Since I asked this question, I came across mustache . This is a logical boilerplate language.

Thus, this is not the same as PHP or smarty, where you have the opportunity to add all kinds of logic to you, but it forces you to store all the logic in something like viewing models.

It seems to me that this is the best option, and then switch to the template language, where logic is still possible.

0
Jul 20 2018-12-12T00:
source share

It is currently the fastest and easiest to use. Having made a set of tests on PHP templates, the branch appears in second place immediately after the native php language.

0
Sep 01 '12 at 1:11
source share

I use the template engine in PHP because I prefer to have a high degree of separation between business logic and presentation logic. Web programming is much easier when your PHP (or any other programming language) does not have HTML scattered around the world. This is the Microsoft code behind so popular.

I am using a template engine called KudzuPHP. This is my KudzuASP port for classic ASP. It differs from many template engines in that the code that contains the business rules and logic becomes an event handler for the template engine after the template engine is called. This approach allows you to modify templates (moving large blocks of a view) without requiring changing the PHP code.

KudzuPHP has its own library system, and writing new tags and extension libraries is easy.

Here you can find KudzuPHP: http://www.andrewfriedl.com/downloads/ If you need a version built into the Wordpress plugin that allows you to code the Wordpress API without PHP, go to Wordpress.org and find the plugins for Kazu.

0
Jul 28 '13 at 22:32
source share



All Articles