Reply to Re: [PHP] Re: Templating engines

Your name:

Reply:


Posted by Rasmus Lerdorf on 04/30/05 02:33

Mattias Thorslund wrote:
> Skrol 29 wrote:
>
>>> What templating engines do you use with php and why?
>>> Ive been using smarty (http://smarty.php.net)
>>> Clive.
>>
>>
>>
>> I just can't work with PHP the usual way.
>> Mixing business source and interface source is something too bastard
>> for me.
>> Now that I know templates systems I totaly disagree to say that PHP is a
>> Template Engine. Ok, it has some basic template features, but they are
>> too
>> poor to use it this way seriously.
>
>
>
> Who says PHP itself is a template engine? I think nobody.

I do.

It comes down to whether you want the delineation between the template
and the business logic enforced by the system or not. PHP is a
general-purpose templating system that does not enforce this
delineation. It lets you choose exactly how much logic you are going to
allow in your templates. If you have no control over the people
creating your templates, that may not be appropriate, but in many cases
it is perfectly fine. It just takes a bit of discipline to use it
correctly.

You also need to recognize that you are going to need presentation-layer
logic. You simply can't get around that and you shouldn't confuse
presentation-level logic, which can be quite complex, with the business
logic behind your application. I often see people make the mistake of
trying to separate their PHP code from their HTML which the article you
referenced hinted at as well.

The approach I tend to point people at is something I have been calling
a Template API. That is, when you build your application, you create a
template api to go along with it. In essence you are creating a
templating system for each application you write.

For example, I describe a simple such system here:

http://talks.php.net/show/mtladv05/20

To me, this is a perfectly good template (from the slide):

<?php
start_poll(1);
$os = array("FreeBSD","Linux","OSX","Windows","Other");
?>
<p class="purpose">
Please answer a couple of questions for us.
</p>

<p class="question">
1. What is your name?
</p>
<?php text_answer('name',64)?>

<p class="question">
2. Which operating systems do you use on a daily basis?
</p>
<?php select_any_of($os)?>

<p class="question">
3. Which operating system do you prefer?
</p>
<?php select_one_of($os)?>

<?php end_poll(); ?>

The end_poll() call could be eliminated as well to make it slightly
cleaner, but otherwise this is straight-forward with no mixing of
business-logic and content.

My main issue with general-purpose templating systems is that they
always end up inventing a new language. It may start off as a subset of
some other language, but eventually it turns into a new language. In
Smarty, for example, you now have stuff like:

{object->method p1="arg1" p2=$arg2 assign="output"}
Result was {$output}

I fail to see how this is in any way better/easier/superior to:

<?$output = $object->method("arg1",$arg2)?>
Result was <?=$output?>

As far as I am concerned you shouldn't be dealing with any sort of
objects from a template to begin with. There should be nothing but
simple variables and straight function calls from a template, but this
is a religious issue that people will never agree on. Hence PHP's
neutral approach where the exact templating delienation is left up to
the users.

-Rasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация