Page 1 of 1

Question about Separating html from php

Posted: Tue Aug 05, 2008 9:36 pm
by zzack38
Hello,

I am new to php coding. I want to separate the html template files from the php coding. I have done so using require statements and separated the html into individual php files. Everything seems to be working fine but I was wondering if this is the correct way to do this :?:

I would like to know the experts opinion about this, Please advise. Thanks

Here is an example of how I have it set up.

Main Php

Code: Select all

<?php
  require_once 'config.php';
  
  if (!session_id())
      session_start();
  /*
   if (empty($_SESSION['user_id'])) {
   header("Location: $site_base/login.php");
   exit;
   }
   */
  
  require_once 'inc/db_connect.php';
?>
 <?php
  require 'template1.php';
?>
<!-- SUBMENU TABLE -->
 <?php
  require 'side_menu1.php';
?>
<!-- SUBMENU TABLE END -->
 
<?php
  require 'template2.php';
?>
 
<!-- CONTENT STARTS HERE -->
 
      <clear="all" />
      <div id="frame_inner">
         <br>
 
                        <div id="content">
                                <h1>Welcome to <?php
  echo $site_name;
?></h1>
                                <?php
  $q = mysql_query("SELECT value FROM texts WHERE id = " . HOME_WELCOME, $db) or die(mysql_error($db));
  $row = mysql_fetch_assoc($q);
  mysql_free_result($q);
  echo $row['value'];
?>
 
                                <h2>About Us</h2>
                                <?php
  $q = mysql_query("SELECT value FROM texts WHERE id = " . HOME_ABOUT_US, $db) or die(mysql_error($db));
  $row = mysql_fetch_assoc($q);
  mysql_free_result($q);
  echo $row['value'];
?>
 
                                <hr />
 
                                <h2>Site News</h2>
                                        <?php
  $q = mysql_query("SELECT value FROM texts WHERE id = " . HOME_SITE_NEWS, $db) or die(mysql_error($db));
  $row = mysql_fetch_assoc($q);
  mysql_free_result($q);
  echo $row['value'];
?>
                                        <b> </b>
 
                                        <div style="margin-top: 15px;"><b>&#187;  </b></div>
 
          <clear="all" />
        </div>
      </div>
    </div>
 
<!-- CONTENT ENDS HERE -->
 
<?php
  require 'template3.php';
?>
 
<!-- Copyright Header -->
<?php
  require 'inc/footer.php';
?>
<!-- Copyright Header End -->
<?php
  require 'template4.php';
?>
 
<?php
  require_once 'inc/db_end.php';
?>

=================================================================
Template 1 php

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title><?php
  echo $site_name;
?></title>
  <link href="<?php
  echo $css_base;
?>/review.css" media="screen" rel="Stylesheet" type="text/css">
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
 
  <!--Start DWLayoutTable-->
 
  <tbody><tr>
   <td height="105" colspan="2" align="right" valign="top" background="<?php
  echo $img_base;
?>/head2.jpg" 
bgcolor="#AE2344"><div align="left"><br>
     </div></td>
   <td background="<?php
  echo $img_base;
?>/l1.jpg"></tr>
  <tr>
   <td width="170" height="40" valign="top" background="<?php
  echo $img_base;
?>/side2.jpg" bgcolor="#AC2047"></td>
   <td width="1064" align="right" background="<?php
  echo $img_base;
?>/bbbut.jpg" bgcolor="#000000">
  <table border="0" cellpadding="0" cellspacing="0" width="802" height="40">
 
  <!--End DWLayoutTable-->
 
     <tr>
       <td width="13" valign="top" background="<?php
  echo $img_base;
?>/bbbut.jpg"><img src="<?php
  echo $img_base;
?>/spacer.gif" width="1" height="1"></td>
       <td width="789" valign="top" height="40">
    <table width="100%" border="0" cellpadding="0" cellspacing="0" background="<?php
  echo $img_base;
?>/bbbut.jpg">
           <tr>
             <td width="737" height="40" background="<?php
  echo $img_base;
?>/bbbut.jpg" bgcolor="#000000" 
 
align="center"><div align="center" class="style1 style2">
              <a href="main.php" class="nav1">Home</a> 
            <color class="style5">|</color>
              <a href="reviews.php" class="nav1">Reviews</a> 
            <color class="style5">|</color>
              <a href="site_reviews.php" class="nav1">Site Reviews</a> 
            <color class="style5">|</color>
              <a href="boards.php" class="nav1">Boards</a> 
            <color class="style5">|</color>
              <a href="qa.php" class="nav1">Q&A</a> 
            <color class="style5">|</color>
              <a href="contact.php" class="nav1">Contact Us</a> 
            <color class="style5">|</color>
              <a href="misc.php" class="nav1">Misc</a></td>
       <td width="136">&nbsp;</td>
           </tr>
       </table></td>
     </tr>
  </table></td>
   <td background="<?php
  echo $img_base;
?>/l1.jpg"><img src="<?php
  echo $img_base;
?>/l1.jpg" alt="" width="14" height="40" 
 
border="0"></td>
  </tr>
  <tr>
    <td height="712" align="left" valign="top" background="<?php
  echo $img_base;
?>/side.jpg">
    <table border="0" cellpadding="0" cellspacing="0" width="170">
      <tbody>

===============================================================

Template 2 php

Code: Select all

   </tbody></table>
    <table border="0" cellpadding="7" cellspacing="0" width="170">
      <tbody><tr>
        <td>&nbsp;</td>
    </tr>
    </tbody></table>
    <table border="0" cellpadding="7" cellspacing="0" width="170">
      <tbody>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </tbody>
    </table>    <p>&nbsp;</p></td>
    <td valign="top"><table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tbody><tr>
        <td height="10">
                <img src="<?php
  echo $img_base;
?>/spacer.gif" height="10" width="1"></td>
      </tr>
    </tbody></table>
      <table border="0" cellpadding="7" cellspacing="0" width="100%">
      <tbody><tr>
        <td>
===================================================================

Template 3 php

Code: Select all

</td>
      </tr>
    </tbody>
      </table></td>
    <td background="<?php
  echo $img_base;
?>/l1.jpg" valign="top">
        <img src="<?php
  echo $img_base;
?>/l1.jpg" width="14" height="29"></td>
  </tr>
  <tr>
    <td height="13" background="<?php
  echo $img_base;
?>/side.jpg" bgcolor="#AC2047"></td>
    <td valign="top"></td>
    <td background="<?php
  echo $img_base;
?>/l1.jpg" valign="top"></td>
  </tr>
</tbody></table>
==================================================================

Template 4 php

Code: Select all

</tbody>
</table>
 
</body>
</html>

Re: Question about Separating html from php

Posted: Wed Aug 06, 2008 1:29 am
by RobertGonzalez
Regardless of how you do it the fact is you are going to have to marry PHP output with your markup. A lot of new developers immediately run to a template engine (Smarty is the most popular by far, but there are a lot of good alternatives).

Really what you want the most is to separate your program logic from your presentation scheme. Having something:

Code: Select all

<div id="body"><?php
if ($_SESSION['decapitated'] == 'spamtards') {
  if (in_array($_SESSION['spamtards'], $usertodecapitate)) {
    echo '<p>I am so going to cut off your head ' . $usertodecapitate . '</p>';
  } else {
    echo '<p>Good thing for you I can\'t find you</p>';
  }
} else {
  echo '<p>There is no one to decapitate today. Crap.</p>';
}
?></div>
 
Is a little hard to read and maintain, whereas something like:
decapitate.php

Code: Select all

<?php
$msg = null;
if ($_SESSION['decapitated'] == 'spamtards') {
  if (in_array($_SESSION['spamtards'], $usertodecapitate)) {
    $msg = 'I am so going to cut off your head ' . $usertodecapitate;
  } else {
    $msg = 'Good thing for you I can\'t find you';
  }
} else {
  $msg = 'There is no one to decapitate today. Crap.';
}
 
require_once 'decapitate-view.php';
?>
 
decapitate-view.php

Code: Select all

<?php if ($msg): ?><div id="body"><p><?php echo $msg ?></p></div><?php endif; ?>
makes things a little easier to understand and maintain. Make any sense?