views:

390

answers:

3

I've experienced first hand the extent of the horror and foot-shooting that the ugliness of PHP can cause. I'm onto my next project (you may be wondering why I'm not just switching languages but that's not why I'm here) and I've decided to try doing it right, or at least better, this time.

I've got some models defined, and I've started on a main controller. I'm at a fork in my decisions about how to implement the view. So far, the main controller can be given lists of display functions to call, and then it can spew out the whole page with one call. It looks like:

function Parse_Body()  
{  
    foreach ($this->body_calls as $command)  
    {  
        $call = $command['call'];  
        if (isset($command['args'])) $call($command['args']);  
        else $call();  
    }  
}

My dilemma is this:

Would it be better to have all of my display functions return the HTML they generate, so that the main controller can just echo $page; or should the display files use raw HTML outside of PHP, which gets output as soon as it's read?

With the former, the main app controller can precisely control when things get output, without just relinquishing complete control to the whim of the displays. Not to mention, all those lists of display functions to call (above) can't really be executed from a display file unless they got passed along. With the latter method, I get the benefit of doing HTML in actual HTML, instead of doing huge PHP string blocks. Plus I can just include the file to run it, instead of calling a function. So I guess with that method, a file is like a function.

Any input or advice please?

+1  A: 

You can get the benefit of both, obtaining a string of HTML while also embedding HTML within PHP code, by using the output control functions:

From the PHP manual @ http://www.php.net/manual/en/ref.outcontrol.php:

<?php

function callback($buffer)
{
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}

ob_start("callback");

?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php

ob_end_flush();

?>
Guruno
+1  A: 

Would it be better to have all of my display functions return the HTML they generate, so that the main controller can just echo $page; or should the display files use raw HTML outside of PHP, which gets output as soon as it's read?

One of the advantages of php is that the processing is similar to the output:

So:

<h1> <?= $myHeading; ?> </h1>

Is more clear than:

echo "<h1>$myHeading</h1>";

An even more than:

echo heading1($myHeading); //heading1() being an hypothethical user defined function.

Based on that I consider that it is better to in the view to have HTML and and just print the appropriate dynamic fields using php.

In order to get finner control over the output you can use: ob_start as gurunu recommended.

You could of course use any of the several php MVC frameworks out there. My prefered one, now is: Solarphp

but Zend Framework and Cakephp could help you too.

And finally if you don't want to use any framework You could still use a pretty slim templating engine: phpSavant. That will save you a few headaches in the development of your view. th

elviejo
but again, PHP Group recommends shorttags to be dropped. so think about using <?= and ?> again for you might need to change it near future.
thephpdeveloper
Using one of the suggested frameworks (ZF, Solar) is probably a good course of action.
Jani Hartikainen
Mauris Can you point to the "recommendation of short tags to be dropped". People recommend against using them because on some php installations they are disabled in php.iniHowever I like them in my views / templates. It is easier than writing: <?php echo $myHeaing; ?>on several places.
elviejo
A: 

First buffer everything. then replace tags using a parser at end of script.

<?php

$page_buffer = '';

function p($s){
   global $page_buffer;
   $page_buffer .= $s;
}

$page_buffer = str_replace(
     array('<$content$>','<$title$>'),
     array($pagecontent,$pagetitle),
     $page_buffer);

echo $page_buffer;

?>

Samstyle PHP Framework implements output buffering and View model this way

And did I mention about benefits of buffering your output in a variable before "echo-ing"? http://thephpcode.blogspot.com/2009/02/php-output-buffering.html

thephpdeveloper