我如何将逻辑与演示分开? [英] How I separate logic from presentation?

查看:135
本文介绍了我如何将逻辑与演示分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我正在开发网站和编码 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>分离。你在两层都有逻辑。名字说明了这一点:


  • 首先执行您的逻辑

  • 然后转到presentation 。



以您的示例为例

  $ 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?>

然后将此文件包含在您的业务逻辑文件中。或者 - 更好 - 在更高层次的模板中。



您可以在这个主题 使用这样的分隔,你可以:


  1. 使用可以做语法高亮和暗示的好编辑器的强大功能(尽管我怀疑你正在使用它)。

  2. 将您的设计外包给专业设计师。

  3. 轻松地将演示技术从HTML更改为AJAX。
  4. 对于不同外观的不同站点使用相同的引擎。

  5. 实现任意数量的主题或皮肤。

  6. 从HTML,CSS或JS添加一些丰富的装饰品时保持活力。

尽管从目前的方法或其他方法来看,这一切都是不可能的。这个想法是分离


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:

  1. Use power of good editor that can do syntax highlighting and hinting (though I doubt you are using one).
  2. outsource your design to a professional designer.
  3. easily change presentation technology from HTML to AJAX.
  4. use the same engine for the different sites with different appearances.
  5. Implement any number of "themes" or "skins".
  6. Stay alive when adding some rich decorations from either HTML, CSS or JS.

While all this is just impossible with your current approach or approach from other answer. The idea is separation of matters.

这篇关于我如何将逻辑与演示分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆