开发者

how to organize the code?

开发者 https://www.devze.com 2023-02-24 00:30 出处:网络
<?php if(arg(0)==\"test\"){ echo \"code here\"; }else{ echo \'<div class=\"item-list\"> <?php if (!empty($title)) : ?>
<?php
if(arg(0)=="test"){
echo "code here";
}else{
echo '<div class="item-list">
  <?php if (!empty($title)) : ?>
    <h3><?php print $title; ?></h3>
  <?php endif; ?>
  <<?php print $options['type']; ?>>
    <?php foreach ($rows as $id => $row): ?>
      <li class="<?php print $classes[$id]; ?>"开发者_如何学Python><?php print $row; ?></li>
    <?php endforeach; ?>
  </<?php print $options['type']; ?>>
</div>
';}

the else echo is too long. is there a way to make it small.


Generally speaking, this is bad code. You are messing business logic with views. I would definitely use a very simple template system like :

Simple and Fast Template Engine

This code cannot be maintained and if it gets bigger, it would be a hell to even read. Please do yourself a favor and separate logic from views.

Take a look at this nice example from the url above, to see how code should be organized :

<?php    
require_once('template.php');    

$tpl = & new Template('./templates/');    
$tpl->set('title', 'User Profile');    

$profile = array(    
   'name' => 'Frank',    
   'email' => 'frank@bob.com',    
   'password' => 'ultra_secret'    
);    

$tpl->set_vars($profile);    

echo $tpl->fetch('profile.tpl.php');    
?>

The associated template looks like this:

<table cellpadding="3" border="0" cellspacing="1">    
   <tr>    
       <td>Name</td>    
       <td><?=$name;?></td>    
   </tr>    
   <tr>    
       <td>Email</td>    
       <td><?=$email;?></td>    
   </tr>    
   <tr>    
       <td>Password</td>    
       <td><?=$password;?></td>    
   </tr>    
</table>

And the parsed output is as follows:

<table cellpadding="3" border="0" cellspacing="1">    
 <tr>    
   <td>Name</td>    
   <td>Frank</td>    
 </tr>    
 <tr>    
   <td>Email</td>    
   <td>frank@bob.com</td>    
 </tr>    
 <tr>    
   <td>Password</td>    
   <td>ultra_secret</td>    
 </tr>    
</table>


I'm surprised that code would even run. Here's a better way, without using echo: just close the php interpreter while you generate your html output.

<?php
if(arg(0)=="test"){
echo "code here";
}else{
?>
<div class="item-list">
  <?php if (!empty($title)) : ?>
    <h3><?php print $title; ?></h3>
  <?php endif; ?>
  <<?php print $options['type']; ?>>
    <?php foreach ($rows as $id => $row): ?>
      <li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
    <?php endforeach; ?>
  </<?php print $options['type']; ?>>
</div>
<?
}
?>


<?php

$output = "<div class='item-list'>";
if(!empty($title)) $output .= "<h3>".$title."</h3>";
$output .= $options['type'];
foreach($rows as $id => $row) {
    $output .= '<li class="' . $classes[$id] . '">' . $row . '</li>';
}
$ouput .= $options['type'] . "</div>";

$output = (arg(0) == "test" ? "code here" : $output);
echo $output;

?>


It looks nicer if you don't switch between PHP and HTML context (also indentation does help):

<?php
   if (arg(0)=="test") {
      echo "code here";
   }
   else {
      echo '<div class="item-list">'
         . @$title
         . "<$options[type]>";

      foreach ($rows as $id => $row) {
          print "<li class=\"{$classes[$id]}\">$row</li>";
      }

      echo "</$options[type]>";
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消