php - 不使用模板引擎将逻辑与表示分离

标签 php

如何在不使用任何模板引擎的情况下将逻辑与表示分离(传统的 php,而不是 OOP) 提前致谢

最佳答案

为什么不使用 PHP 本身作为模板引擎?它在我为您的其他问题发布的代码中使用。您的程序必须分为两个主要部分:获取数据和显示数据。

每个页面都必须有自己的模板。在我发布的代码中有2个非常简单的模板,form.phplist.php
只需使用整个网站模板扩展它,您就完成了!
这是一个稍微复杂一点的 PHP 模板示例:

<table border="0" cellpadding="2" cellspacing="0" width="600">
<? foreach ($data as $row): ?> 
  <tr bgcolor="#666699">
    <td align=left>
      <font color="white"><b><?=$row['name']?></b></font>
    </td>
    <td align=right><font color="white">
      <?=$row['date'] ?>
    </font></td>
  </tr>
  <tr bgcolor="f0f0f0">
    <td colspan=2><?=$row['body'] ?></td>
  </tr>
  <? if ($row['answer']): ?>
  <tr bgcolor="d3d3d3">
    <td colspan=2 valign="top">
      <table border="0" cellpadding="0" cellspacing="5">
        <tr>
          <td valign="top"><b>Answer: </b></td>
          <td><?=$row['answer'] ?></td>
        </tr>
      </table>
    </td>
  </tr>
  <? endif ?>
  <? if($admin): ?>
  <tr>
    <td colspan=2>
      <font size=-1>
      <?=$row['id']?> - <?=$row['ip']?> - <?=$row['topic']?>
  <? if($row['del']): ?>
      <a href="/gb/?action=show&id=<?=$row['id']?>">show</a>
  <? else: ?>
      <a href="/gb/?action=hide&id=<?=$row['id']?>">hide</a>
  <? endif ?>
      <a href="/gb/?action=edit&id=<?=$row['id']?>">edit</a>
      </font>
    </td>
  </tr>
  <? endif ?>
<? endforeach ?>
</table>

它的名字是这样的

<?
//some code to get data
include 'tpl_top.php';
include 'tpl_list.php';
include 'tpl_bottom.php';
?>

我觉得太棒了!
但不知道,如果这是你所要求的:)

关于php - 不使用模板引擎将逻辑与表示分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2574424/

相关文章:

php - str_replace 为一堆代码 php

php - FosElasticBundle未分析字段

php - Yii2,登录后重定向。避免 Controller \ Action \行为

php - 在后端使用 C 计算的网页前端

php - Laravel 模块用于更新联属网站中的价格、库存和评论

php - 在检查器中生成代码覆盖率

javascript - else 语句逻辑未运行

php - 是否可以从(自动)加载中抑制单个 Composer 依赖项?

javascript - 使用 Javascript 从 onClick 更新数据库

php - MySQL如何让值过期?