通常我正在开发网站和编码 PHP 和 HTML 类似这样的内容 -
while(mysqli_stmt_fetch($ stmt)){ //创建表体 $ html。=< tr> \ N; $ html。=< td> $ title< / td> \\\; $ html。=< td> $ date< / td>; $ html。=< td align ='center'> \\\; $ html。=< a href ='#'> \\\; $ html。=< span class ='view'title ='查看此评论'>< / span> \\\; $ html。=< / a> \\\; $ html。=< / td> \\\; $ html。=< td class ='td_catchall'align ='center'> \\\; $ html。=< a href ='#'> \\\; $ html。=< span class ='edit'title ='编辑此评论'>< / span> \\\; $ html。=< / a> \\\; $ html。=< / td> \\\; $ html。=< td align ='center'> \\\; $ html。=< a href ='#'> \\\; $ html。=< span class ='delete'title ='删除此评论'>< / span> \\\; $ html。=< / a> \\\; $ html。=< / td> \\\; $ html。=< / tr> \\\; } //创建视图博客对话框 $ 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> \\\;但是最近我碰巧认识了我的一位朋友,这是保存HTML在一个PHP变量中。并且还说我需要从演示中分离逻辑。
如果确实如此,有人可以告诉我该怎么办?
任何意见将不胜感激。 谢谢。
解决方案
我如何将逻辑从表示中分离出来? 将它分开。
虽然它被称为业务逻辑 / strong>分离。你在两层都有逻辑。名字说明了这一点:
以您的示例为例
$ res = $ stmt-> get_result(); $ data = array(); while($ row = mysqli_fetch_assoc($ res)){ $ data [] = $ row; }尽管我会使用更智能的方法从数据库中获取数据,这:
$ data = $ db-> getArr(SELECT ...);然后重复所有数据库或其他服务交互的所有步骤。您的目标是准备好业务逻辑必须提供的所有数据。然后您的业务逻辑结束,您可以转向演示。
如果您可以轻松地交换模板引擎,您可以与坏的分离开来(您无法使用其他方法回答,介意你) - 所以,特定的引擎并不重要。我们来看一个最简单的例子 - PHP
创建一个名为 tpl.php 的文件并将该代码放在
< table> <?php foreach($ data as $ row):extract($ row); ?> < tr> < td><?= $ title< / td> 等等< / tr> <?php endforeach?>然后将此文件包含在您的业务逻辑文件中。或者 - 更好 - 在更高层次的模板中。
您可以在这个主题 使用这样的分隔,你可以:
尽管从目前的方法或其他方法来看,这一切都是不可能的。这个想法是分离。
Normally I am developing website 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 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.
解决方案How I separate logic from presentation?
Separate it.
Though it is called business logic from presentation logic separation. You have logic in both layers. The name says it all:
- perform your "logic" first
- then turn to "presentation."
Taking your example it have to be something like this
$res = $stmt->get_result(); $data = array(); while ($row = mysqli_fetch_assoc($res)) { $data[] = $row; }though I would use some more intelligent approach to get data from database, something like this:
$data = $db->getArr("SELECT ...");then repeat all the steps for all the database or other other services interaction. Your goal is to have all the data ready that business logic have to supply. Then your business logic ended and you can turn to presentation.
You can tell a good separation from the bad one if you can easily interchange template engines (you can't do it with approach from other answer, mind you) - so, particular engine doesn't matter. Let's take a simplest one - 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 your business logic file. Or - better - in some higher level template.
You can see a good real life example of the approach in this topic
Using such a separation, you can:
While all this is just impossible with your current approach or approach from other answer. The idea is separation of matters.
更多推荐
我如何将逻辑与演示分开?
发布评论