Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use templating system in PHP? [closed]

Why should I use templating system in PHP?

The reasoning behind my question is: PHP itself is feature rich templating system, why should I install another template engine?

The only two pros I found so far are:

  1. A bit cleaner syntax (sometimes)
  2. Template engine is not usually powerful enough to implement business logic so it forces you to separate concerns. Templating with PHP can lure you to walk around the templating principles and start writing code soup again.

... and both are quite negligible when compared to cons.

Small example:

PHP

<h1><?=$title?></h1> <ul>   <? foreach ($items as $item) {?>   <li><?=$item?></li>   <? } ?> </ul> 

Smarty

<h1>{$title}</h1> <ul>   {foreach item=item from=$items}   <li>{$item}</li>   {/foreach} </ul> 

I really don't see any difference at all.

like image 606
Josef Sábl Avatar asked Jan 12 '09 16:01

Josef Sábl


People also ask

What is the purpose of templates in PHP?

A PHP template engine is a way of outputting PHP in your HTML without using PHP syntax or PHP tags. It's supposed to be used by having a PHP class that will send your HTML the variables you want to display, and the HTML simply displays this data.

What is the benefit of 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.

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 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.


2 Answers

Yes, as you said, if you don't force yourself to use a templating engine inside PHP ( the templating engine ) it becomes easy to slip and stop separating concerns.

However, the same people who have problems separating concerns end up generating HTML and feeding it to smarty, or executing PHP code in Smarty, so Smarty's hardly solving your concern separation problem.

See also:

  • PHP as a template language or some other templating script
  • What is the best way to insert HTML via PHP
like image 184
Kent Fredric Avatar answered Oct 11 '22 00:10

Kent Fredric


The main reason people use template systems is to separate logic from presentation. There are several benefits that come from that.

Firstly, you can hand off a template to a web designer who can move things around as they see fit, without them having to worry about keeping the flow of the code. They don't need to understand PHP, just to know to leave the special tags alone. They may have to learn a few simple semantics for a few tags but that is a lot simpler than learning the whole language.

Also, by splitting the page into separate files, both programmer and designer can work on the same 'page' at once, checking in to source control as they need, without conflicts. Designers can test their template visuals against a stable version of the code while the programmer is making other, potentially breaking changes, against their own copy. But if these people were both editing the same file and had to merge in different changes, you can encounter problems.

It also enforces good programming practice in keeping business logic away from presentation logic. If you put your business logic mixed in with the presentation then you have a more difficult time extracting it if you need to present it differently later. Different modes of presentation in web apps are increasingly popular these days: RSS/ATOM feeds, JSON or AJAX responses, WML for handheld devices, etc. With a template system these can often be done entirely with a template and no or little change to anything else.

Not everybody will need or appreciate these benefits however. PHP's advantage over Java/Python/Ruby/etc is that you can quickly hack up web pages with some logic in them, and that's all well and good.

like image 38
Kylotan Avatar answered Oct 11 '22 00:10

Kylotan