Website Development

Miscellaneous Forums/General Discussion/Website Development

@Indiepath

Quite frankly that is a terrible way of doing things. You should be seperating the logic from the view - don't have any HTML in your PHP code!


Maybe my second example was a bit misleading because it's a basic example. In my real code, all HTML layout is supported by CSS and so has the advantage of keeping both code maintenance and layout maintenance separate. Design elements can also be chosen to be presented by the programmer within their main code file, which is then injected into the template. This is essentially the same as what the Smarty template engine is doing, but without using external code. A better example of what I mean is as follows:

Content file:
<?php
include "../settings.inc.php";

$TEMPLATE = array();
$TEMPLATE['head'] = "Some head HTML";
$TEMPLATE['content'] = "Some content";
$TEMPLATE['title'] = "Title example";

$SETTINGS = array();
$SETTINGS['show_contactInfo'] = true;
$SETTINGS['title_bigger'] = true;




if($SETTINGS['title_bigger'] == true)$TEMPLATE['content'] .= "<h1>".$TEMPLATE['title']."</h1>";
  else print $TEMPLATE['title'];

$TEMPLATE['content'] .= $TEMPLATE['content'];

if($SETTINGS['show_contact'] == true)$TEMPLATE['content'] .= $TEMPLATE['show_contact'];


include "../template.inc.php";
?>



Template:
<html>
  <head>
    <?=$TEMPLATE['head'];?>
  </head>
  <body>
    <?=$TEMPLATE['content']?>
  </body>
</html>


Taking the example further, you could separate the rules defined using the $SETTINGS array into another file so that all your pages share one common set of rules that automatically populate and modify the $TEMPLATE array, but lets keep things simple for the example ;-).