views:

69

answers:

2

In the framework I am working with, if I want to present data in a view I do this:

<h1><?php echo $this->header ?></h1>

Recently I discovered xTemplate and noticed it uses string placeholders, so the above code in a xTemplate would be:

<h1>{HEADER}</h1>

I always thought that a little php in the view was ok and was also faster than doing a str_replace, but I have recently started working with a designer and I think lot's of php tags in the html might confuse him.

I would like to know your thoughts on this subject.

Thanks.

Note: This might be subjective but I really want to find out the best way or at least know all the pros and cons of both of these approaches.

+7  A: 

PHP is designed as a templating language. I see no point in creating template engines above template engine.

Compare

 Hi, <b><?=$username?></b>

 Hi, <b>{username}</b>

Is the first variant really complicated?

If you try Smarty, you will be very surprized how complicated it is. It is pure EVIL: http://www.smarty.net/manual/en/language.function.foreach.php

 {foreach name=outer item=contact from=$contacts}
   <hr />
   {foreach key=key item=item from=$contact}
     {$key}: {$item}<br />
   {/foreach}
 {/foreach}

Is it simpler to read than PHP's foreachs? Don't make me laugh.

EVIL. Don't use any template engines other than the PHP itself. Provide enough helpers functions to your designer and he will not complain.

valya
+1 for *Smarty is pure EVIL*
Gordon
So are other template engines above PHP. I can understand template engines for Python since it's not embedded in HTML, but templates for PHP are strange and stupid. I used to think it's a good idea :(
valya
This is really what I think too. I have seen smarty code (via sugar 5 templates) and it's horrible!!! Simply horrible. xTemplate didn't seem that bad :P but I do agree with you. One thing I don't use is the short tag, don't know really why, but I never liked it.
AntonioCS
+1  A: 

Interesting question. I don't know how confused a designer could be by seeing PHP but PHP as the following advantages:

  • You don't have to learn another syntax (template language)
  • PHP is flexible enough to do everything (although of course you should NOT do everything) in the template.
Felix Kling