Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use PHP template engines [closed]

I am building a website in php with lot of pages and we are a team of 9 working on it. So just want to explore that when should we use PHP template engines and when we shouldn't. So I'm interested in pros and cons of using PHP Template engines so I can make a decision whether to use it in my case or not.

like image 632
GG. Avatar asked May 04 '11 18:05

GG.


People also ask

When should you use a templating engine?

Template engines are used when you want to rapidly build web applications that are split into different components. Templates also enable fast rendering of the server-side data that needs to be passed to the application. For example, you might want to have components such as body, navigation, footer, dashboard, etc.

What is an advantage to using a templating engine?

A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.

What is one of the benefits of template rendering?

Using a template engine makes it easy to separate out what's being displayed from how it's being displayed.

Are templating engines necessary?

You actually dont need them, but they have a lot of features that makes your pages more dynamic..


3 Answers

PHP is a template language.

In my experience so far with various template systems, the conclusion was pretty simple: use none.

Instead, write PHP as it should be written! Never do anything inside a .html besides echo and for/foreach. If you write the code based on a design pattern like MVC this becomes even more obvious: just echo and foreach inside and explain the frontends they should never mess with the code inside <?php and ?>.

It worked for me ever since. It usually was harder for me to explain them Smarty than to explain to never mess with php.

Template systems also add weight to your server (sometimes is just a bit, sometimes you may feel it). It may seem over-optimization at first, but I prefer to keep it as simple as it can be.

Note:

Smarty, for example, is HARDER to spot in a .html file because the only Smarty syntax highlighter I know is a NetBeans plugin and it's pretty beta. PHP, on the other hand, has it syntax highlighted in any decent editor. Also easier for the frontends to spot and not mess with.

Wrap up

Cons (for using template system)

  • Increases server load (lighter or heavier, doesn't matter - you can see it)
  • Induces bad practice (logic wrapped inside template language syntax)
  • No syntax highlighting for template languages' syntax - harder to spot (for coder and frontend)
  • Time spent learning it and teaching it to the frontends
  • Harder to explain to the frontend team (I've taught basic PHP to frontends several times - while many more were already able to write their own 'beginner-level' PHP; I have never taught a frontend Smarty so they can do something other than {$var} )
  • Makes you avoid the REAL problem: logic and presentation separation!
  • Adds extra weight to your project

Pros (for using template system)

  • Extreme boredom (probably the only valid argument)

Template system replacement

  • Logic and presentation separation (I'd suggest MVC for this task and for the pros it provides for other fields of development: easier maintenance, abstract database and so on)
  • Force yourself to write in the view only echo and iteration for echoing: foreach and for should accomplish 99% of the iteration needs; you can also use while and do while
like image 187
Bogdan Constantinescu Avatar answered Sep 24 '22 16:09

Bogdan Constantinescu


For me, when deciding if a separate template engine should be used or to just use PHP for the templating, it always boils down to this:

When to use template engine

When you must restrict (sandbox) the code that can be run in the templates.


When not to use template engine

All the other times.


like image 37
webbiedave Avatar answered Sep 25 '22 16:09

webbiedave


The PHP purists will tell you that PHP is itself a template engine. I consider myself a purist on this matter, and recommend just using PHP. It even has an alternate syntax for if and loop blocks that are pretty much designed for template-style readability.

Some people, however, still prefer using template engines, such as Smarty. There are a number of things to consider if you do choose that route:

Who's going to be maintaining the template? If the person maintaining the templates already knows PHP, there's no point in making them learn a new, pseudo-PHP template engine. If they don't know PHP, then it's still questionable, depending on their background, since most template engines just implement a syntax not unlike PHP tags (such as <% %>)

How complicated will your templates get? Some template engines are extremely restrictive in what you can do in the templates (forcing you to put everything in the controller, some almost to the point of uselessness or unnecessary hoop-jumping), while others are about as permissive as raw PHP (which many would argue defeats the purpose of a template engine).

How much does efficiency and speed matter? Template engines add overhead. Period. Converting the custom tags to PHP tags takes resources. How much they add and how much it matters depends on a number of factors (including the engine itself). If you need more speed from your site, then I'd vote the template engine as among the first to go.

As I said, I also recommend using PHP as your template "engine," but do be aware of some of the pitfalls. Mainly, it's really easy to add more logic than necessary into your templates. Make sure you have a rule to only include echo, for/foreach, and basic if blocks (such as if is_admin() and the like), and make sure to enforce it.

like image 30
Shauna Avatar answered Sep 26 '22 16:09

Shauna