Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

php - How I separate logic from presentation?

Normally I am developing website and coding PHP and HTML something like this -

while (mysqli_stmt_fetch($stmt)) {      
    // Create Table Body
    $html .= "<tr>
";
    $html .= "  <td>$title</td>
";
    $html .= "  <td>$date</td>";                            
    $html .= "  <td align='center'>
";
    $html .= "      <a href='#'>
";
    $html .= "          <span class='view' title='View This Comment'></span>
";
    $html .= "      </a>
";
    $html .= "  </td>
";                           
    $html .= "  <td class='td_catchall' align='center'>
";
    $html .= "      <a href='#'>
";
    $html .= "          <span class='edit' title='Edit This Comment'></span>
";
    $html .= "      </a>
";
    $html .= "  </td>
";                       
    $html .= "  <td align='center'>
";
    $html .= "      <a href='#'>
";
    $html .= "          <span class='delete' title='Delete This Comment'></span>
";
    $html .= "      </a>
";
    $html .= "  </td>
";
    $html .= "</tr>
"; 
}

//Create View Blog Dialog Box 
$viewBlog  = "<div id='dialog-view'>
";
$viewBlog .= "      <h2>$title</h2>
";
$viewBlog .= "  <p>$date</p>
";
$viewBlog .= "  <p>";
$viewBlog .= "          <img src='".UPLOAD_DIR.$userName."/".$image."' />";
$viewBlog .= "      $comment</p>";
$viewBlog .= "</div>
";

But recently I came across to know one of my friend, that is a bad practice of saving the HTML in a PHP variable. And also said I need to separate logic from presentation.

If it is true, can anybody tell me how can I do it?

Any comments would be greatly appreciated. Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I strongly recommend a templating library like Twig or Mustache. However, the basics would be to use external PHP files as HTML, and use require. Here is a bit of a hacky example:

<?php
$comments = array();

while (mysqli_stmt_fetch($stmt)) {     
  $comments[] = $stmt;
}

require 'comments.php';

Then in comments.php:

<?php foreach ($comments as $comment) : ?>
<tr>
  <td><?php echo $comment['title'] ?></td>
  <td><?php echo $comment['date'] ?></td>                          
  <td align='center'>
      <a href='#'>
          <span class='view' title='View This Comment'></span>
      </a>
  </td>                           
  <td class='td_catchall' align='center'>
      <a href='#'>
          <span class='edit' title='Edit This Comment'></span>
      </a>
  </td>                       
  <td align='center'>
      <a href='#'>
          <span class='delete' title='Delete This Comment'></span>
      </a>
  </td>
</tr> 
<?php endforeach ?>

Here, I am adding each comment (or whatever it may be) to an array. Then, I include a file called comments.php. Your main file should be mostly PHP and should handle any logic, while comments.php should be mostly HTML and only use PHP for presentation (aka looping through an array and echoing out variables).

Your required file has access to all the variables it would have access to if it were inline.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...