Where should HTML render in an object oriented PHP design?

Using object oriented PHP, where should HTML be displayed?

Business processes include several steps for maintaining customer records.
Should the rendering of each business process get a separate PHP file? i.e. viewCustomerTransactions.php?

Where should this code be?

$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?

+4
source share
3 answers

I'm still figuring out what the best way to keep php and layout separate is not too much fluff. At the moment, I really like the inclusion template approach, because it is so simple and has no restrictions.

So, for your example, you will 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 the 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 before. This is good and good for small sites because this method causes some overhead. If you are serious about templates, use smarty. it is easy to learn and it has automatic caching, so it is very fast.

I just understand that you can also do this as follows:

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 :)

+4
source

If I were you, I would save the html in a variable instead of repeating it like this:

 $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>"; } 

Then you have the html data stored in the $ html variable and you can repeat it wherever you want.

 echo $html; 

Does it help you solve the pairing problem?

Sh.

+1
source

I would have to confirm that the inclusion of the template (mentioned by Jules Colle) is one of the answers, however, it may be useless to support when the project is too large, so keep in mind that the document included the file for which file as it exists (in present) there is no (free) IDE solution for this type of chain ... a solution that easily takes you from one file to another or simply puts everything in a procedurally-like code.

Edit: don't forget the magic constants: http://www.php.net/manual/en/language.constants.predefined.php

and this: $ _SERVER ['DOCUMENT_ROOT']

0
source

Source: https://habr.com/ru/post/1336017/


All Articles