jpwco
jpwco

Reputation: 881

Where should HTML be rendered in Object-Oriented PHP design?

Using Object-Oriented PHP, where should HTML be rendered?

The business processes include several actions to maintain customer records.
Should the rendering of each business process get a separate PHP file? ie. viewCustomerTransactions.php?

Where should code like this reside?

$custTrans = Customer.getTransactions();

foreach ($custTrans as $ct){
     $amount = $ct[0];
     $date = $ct[1];
     $product = $ct[2];

     echo '<div class="custTrans">';
         echo '<span class="custTransAmount">'.$amount.'</span>';
         echo '<span class="custTransDate">'.$date.'</span>';
         echo '<span class="custTransproduct">'.$product.'</span>';
     echo '</div>';
}

Perhaps an MVC framework like codeigniter would be better?

Upvotes: 4

Views: 3084

Answers (3)

Jules Colle
Jules Colle

Reputation: 11939

I'm still figuring out what's the best way to keep php and layout seperate without too much fuzz. For the moment I really like the include-templating approach, beacause it's so simple and has no restrictions.

So, for your example, you would have a php file (example.php) that looks like this:

<?php
$custTrans = Customer.getTransactions();

$displ_transactions = array();

foreach ($custTrans as $ct){
     $transaction = array(
         'amount' => $ct[0],
         'date' => $ct[1];
         'product' => $ct[2];
     );
     $displ_transactions[] = $transaction; // this will push the transacion into the array
}
include 'example.tpl.php'
?>

And then you need a second file (example.tpl.php):

<?php foreach ($displ_transactions as $transaction) { ?>
     <div class="custTrans">
         <span class='custTransAmount'><?php echo $transaction['amount'] ?></span>;
         <span class='custTransDate'><?php echo $transaction['date'] ?></span>;
         <span class='custTransproduct'><?php echo $transaction['product'] ?></span>;
     </div>
<?php } ?>

Just call example.php in your browser and you will see the same result as you had before. This is all good and well for small websites, because this method causes some overhead. If you are serious about templating, use smarty. it's easy to learn, and it has automatic caching, so it's super fast.

I just realize you can also do it this way:

example.php:

<?php
$custTrans = Customer.getTransactions();

foreach ($custTrans as $ct){
     $amount = $ct[0];
     $date = $ct[1];
     $product = $ct[2];
     include 'example.tpl.php';
}
?>

example.tpl.php:

 <div class="custTrans">
     <span class='custTransAmount'><?php echo $amount ?></span>;
     <span class='custTransDate'><?php echo $date ?></span>;
     <span class='custTransproduct'><?php echo $product ?></span>;
 </div>

Use whatever suits you best :)

Upvotes: 4

3xCh1_23
3xCh1_23

Reputation: 1499

I would have to confirm that the include-templating (mentioned by Jules Colle) is one of the answers, nevertheless, it might get messy to maintain when the project is too large, so keep in mind to document what file is included by what file, since there is (currently) no (free) IDE solution to this type of chaining... a solution that would easily bring you from one file to another or simply lay-out everything into a procedural-like code.

Edit: do not forget the magic constants: http://www.php.net/manual/en/language.constants.predefined.php

and this: $_SERVER['DOCUMENT_ROOT']

Upvotes: 0

wilsonpage
wilsonpage

Reputation: 17610

If I were you I would store the html in a variable instead of echoing it out like so:

$custTrans = Customer.getTransactions();

$html = "";
foreach ($custTrans as $ct){
     $amount  = $ct[0];
     $date    = $ct[1];
     $product = $ct[2];

     $html .= "<div class="custTrans">";
         $html .= "<span class='custTransAmount'>".$amount."</span>";
         $html .= "<span class='custTransDate'>".$date."</span>";
         $html .= "<span class='custTransproduct'>".$product."</span>";
     $html .= "</div>";
}

You then have this html data stored in the variable $html and you can echo it out where ever you like.

echo $html;

Does that solve you problem mate?

W.

Upvotes: 1

Related Questions