Search code examples
phpmodel-view-controllertemplatesviewpresentation

Best way to place data in the view (MVC)


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.


Solution

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