How do I separate logic from presentation?

Usually I design a site and coding PHP and HTML something like this -

 while (mysqli_stmt_fetch($stmt)) { // Create Table Body $html .= "<tr>\n"; $html .= " <td>$title</td>\n"; $html .= " <td>$date</td>"; $html .= " <td align='center'>\n"; $html .= " <a href='#'>\n"; $html .= " <span class='view' title='View This Comment'></span>\n"; $html .= " </a>\n"; $html .= " </td>\n"; $html .= " <td class='td_catchall' align='center'>\n"; $html .= " <a href='#'>\n"; $html .= " <span class='edit' title='Edit This Comment'></span>\n"; $html .= " </a>\n"; $html .= " </td>\n"; $html .= " <td align='center'>\n"; $html .= " <a href='#'>\n"; $html .= " <span class='delete' title='Delete This Comment'></span>\n"; $html .= " </a>\n"; $html .= " </td>\n"; $html .= "</tr>\n"; } //Create View Blog Dialog Box $viewBlog = "<div id='dialog-view'>\n"; $viewBlog .= " <h2>$title</h2>\n"; $viewBlog .= " <p>$date</p>\n"; $viewBlog .= " <p>"; $viewBlog .= " <img src='".UPLOAD_DIR.$userName."/".$image."' />"; $viewBlog .= " $comment</p>"; $viewBlog .= "</div>\n"; 

But recently, I was faced with getting to know one of my friends, it is a bad practice to save HTML in a PHP variable. He also said that I need to separate the logic from the presentation.

If this is true, can someone tell me how can I do this?

Any comments would be greatly appreciated. Thanks.

+4
source share
2 answers

How do I separate logic from presentation?

Separate it.

Although it is called business logic from presentation logic . You have logic in both layers. The name says it all:

  • first follow your "logic"
  • then go to "presentation".

Taking your example, it should be something like this

 $res = $stmt->get_result(); $data = array(); while ($row = mysqli_fetch_assoc($res)) { $data[] = $row; } 

although I would use a more intelligent approach to retrieve data from a database, something like this:

 $data = $db->getArr("SELECT ..."); 

then repeat all steps for all interactions with the database or other services. Your goal is to have all the data ready for business logic. Then your business logic is over, and you can refer to the presentation.

You can tell the good separation from the bad , if you can easily exchange template engines (you cannot do this using an approach from another answer, mind you) - so the particular engine isn’t. "Let's take the simplest option - PHP

Create a file called tpl.php and put this code there

 <table> <?php foreach ($data as $row): extract($row); ?> <tr> <td><?=$title</td> and so on </tr> <?php endforeach ?> 

then include this file in the business logic file. Or - better - in a higher-level template.

You can see a good example of life in this section.

Using this separation, you can:

  • Use a powerful editor that can do syntax highlighting and hint (although I doubt you are using it).
  • Pass on your design to a professional designer.
  • Easily change presentation technology from HTML to AJAX.
  • use the same engine for different sites with different forms.
  • The introduction of any number of "themes" or "skins".
  • Stay alive by adding some rich decoration from HTML, CSS or JS.

Although all this is simply not possible with your current approach or approach from another answer. The idea is to separate questions.

+7
source

I highly recommend a template library such as Twig or Mustache . However, the basics will be to use external PHP files as HTML and use require. Here is a little hacker 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 add every comment (or whatever) to the array. Then I include a file called comments.php . Your main file should be mainly PHP and should handle any logic, and comments.php should be mostly HTML and use only PHP for presentation (aka looping through an array and echo variables).

Your required file has access to all the variables that it would have access to if it had been embedded.

+11
source

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


All Articles