Re: Re: Templating engines

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rasmus Lerdorf wrote:

Mattias Thorslund wrote:

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.



That explanation is sensible enough.


[skipping ahead a little]

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 many projects, I think the approach you describe in the example (below) is appropriate. One nice thing is that output can begin before the whole page has been nearly processed. I don't know if many other templating solutions can do that.

To me, it looks like it would be hard to keep the functions that are called by the template to a reasonable format or number, at least in a larger project. If it's a template that is to be used by many kinds of pages, there would be either a large number of functions, or there would be generic functions that need to do different things based on the situation.

Of course, the functions could become methods of a "page" object. Then, it gets easy to create specialized subclasses of page objects which can respond differently to the same generic method call. Sounds more attractive the more I think about it :-)

Then again, if I need a big object to handle the business logic (and dynamic presentation logic) because my template approach is sequential, it might be a toss-up compared to using sequential business logic and a small template object.


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.


Except the class described in the article I was talking about. It uses PHP as the template language. It's a big reason why I find it attractive.

[skipping discussion on Smarty]

As far as I am concerned you shouldn't be dealing with any sort of objects from a template to begin with.


This does place some demands on the business logic.

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.


No doubt, people will continue to argue over it for a long time to come. I try not to be religious about it, I simply want to know as many sides of the issue as possible.

Best,

Mattias


-- More views at http://www.thorslund.us

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux