带有循环功能的PHP HTML模板

时间:2011-11-27 05:46:12

标签: php html

我想就如何在模板中实现循环提出一些帮助和想法。我可以在下面进行操作,但如何将其包含在模板中并在结果中显示。

foreach($results as $row) {
 $name = $row['name'];
 $address = $row['address'];
}

我想要实现的结果如下所示,我如何放置$ template-> publish();在变量中,我可以使用它将数据存储到数据库中。非常感谢。

<html>
<head>
<title>My Template Class</title>
</head>
<body>
<table><tr>
<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>

<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>

<td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>Name goes here</p>
<p>Address goes here </p>
</td>
</tr>
</table>
</body>
</html>

模板类

<?
class Template {
   public $template;
   function load($filepath) {
      $this->template = file_get_contents($filepath);
   }
   function replace($var, $content) {
      $this->template = str_replace("#$var#", $content, $this->template);
   }
   function publish() {
            eval("?>".$this->template."<?");
   }
}
?>

模板design.html

<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
</body>
</html>

index.php

<?
include "template.class.php";
$template = new Template;
$template->load("design.html");
$template->replace("title", "My Template Class");
$template->replace("name", "William");
$template->replace("datetime", date("m/d/y"));
$template->publish();
?>

3 个答案:

答案 0 :(得分:7)

PHP本身在模板方面与其他任何引擎一样出色 不需要任何其他东西

$pagetitle = "My Template Class";
foreach($results as $row) {
  $row['date'] = date("m/d/y");
  $data[] = $row;
}
$data = chunk_split($data,3);    

然后在模板中

<html>
<head>
<title><?=$pagetitle?></title>
</head>
<body>
 <table>
<?php foreach ($data as $chunk): ?>
  <tr>
<?php foreach ($chunk as $row): ?>
   <td>
    <h3>Hello <?=$name?>!</h3>
    <p>The time is: <?=$date?></p>
    <p>Embedded PHP works in the template</p>
    <p><b>But embed PHP in the data is a VERY BAD IDEA</b></p>
    <p><?=$address?></p>
   </td>
<?php endforeach ?>
  </tr>
<?php endforeach ?>
 </table>
</body>
</html>

我让你的榜样变得更加复杂,但更接近现实生活 它会在每行

中按行打印您的表格

答案 1 :(得分:3)

只是不要重新发明轮子,PHP作为一种模板语言非常有效:

模板类

<?
class Template
{
   private $template;
   private $vars;
   function load($filepath) {
      $this->template = $filepath;
   }
   function replace($var, $content)
   {
      $this->vars[$var] = $content;
   }
   function publish()
   {
       extract($this->vars);
       include($this->template);
   }
}
?>

模板design.phtml

<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<?php foreach($rows as $row) { extract($row); ?>
  <h3>Hello <?php echo $name; ?></h3>
  <p>The time is: <?php echo $datetime; ?></p>
  <?php echo "<p>Embedded PHP works too!</p>"; ?>
<?php } ?>
</body>
</html>

使用几乎相同,只需分配多行即可使用它:

<?
include "template.class.php";
$template = new Template;
$template->load("design.phtml");
$template->replace("title", "My Template Class");
$rows = array();
$rows[] = array(
    "name" => "William",
    "datetime" => date("m/d/y"),
);
$template->replace("rows", $rows);
$template->publish();
?>

希望这有用。

答案 2 :(得分:0)

您的PHP代码:

$htmldata ="";

($results as $row) {
 $name = $row['name'];
 $address = $row['address'];
$htmldata .="
<tr><td>
<h3>Hello William!</h3>
<p>The time is: 03/10/04</p>
<p>Embedded PHP works too!</p>
<p>".$name."</p>
<p>".$address." </p>
</td>
</tr>
";
}

然后在您的模板design.html中,您将传递$ htmltable变量并嵌入其中:

<html>
<head>
<title>#title#</title>
</head>
<body>
<h3>Hello #name#!</h3>
<p>The time is: #datetime#</p>
<? echo "<p>Embedded PHP works too!</p>"; ?>
<table>
<?php echo $htmltable; ?>
</table>
</body>
</html>