具有循环功能的PHP HTML模板

具有循环功能的PHP HTML模板,php,html,Php,Html,我想询问一些关于如何在模板内实现循环的帮助和想法。我可以做下面的foearch,但是我如何将其包含到模板中并在结果中显示它呢 foreach($results as $row) { $name = $row['name']; $address = $row['address']; } 我想要实现的结果如下所示,以及如何放置$template->publish();在一个变量中,这样我就可以使用它将数据存储到数据库中。非常感谢 <html> <head> <ti

我想询问一些关于如何在模板内实现循环的帮助和想法。我可以做下面的foearch,但是我如何将其包含到模板中并在结果中显示它呢

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>

我的模板类
你好,威廉!
时间是:03/10/04

嵌入式PHP也可以工作

名字就在这里

地址在这里

你好,威廉! 时间是:03/10/04

嵌入式PHP也可以工作

名字就在这里

地址在这里

你好,威廉! 时间是:03/10/04

嵌入式PHP也可以工作

名字就在这里

地址在这里

模板类

<?
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."<?");
   }
}
?>
<?
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.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();
?>

您的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>
";
}
$htmldata=”“;
($结果作为$行){
$name=$row['name'];
$address=$row['address'];
$htmldata.=”
你好,威廉!
时间是:03/10/04

嵌入式PHP也可以工作

“$name。”

“$地址。”

"; }
然后在template 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>

#头衔#
你好,名字!
时间是:#日期时间#


不要再发明轮子了,PHP作为一种模板语言工作得非常出色:

模板类

<?
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."<?");
   }
}
?>
<?
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();
?>


希望这有帮助。

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>

你好
时间是:

嵌入式PHP在模板中工作

但是在数据中嵌入PHP是一个非常糟糕的主意

我让你的例子更复杂,更接近现实生活。

它将按每行3列打印您的表格

感谢您的回复。是否可以将结果存储到一个变量,以便我可以使用它存储到DB?例如:$results=$template->publish();回应结果;您的模板类并没有发回结果,而是在控制台上显示输出,所以在模板类中,您需要使用return,但它不应该有嵌入式标记,应该直接打印变量。-1。代码中的HTML破坏了模板的整个概念。@webdev.gk在数据库中存储填充模板有什么意义?PHP中的短标记,即多年不推荐使用的短标记,需要在PHP.ini中启用,将来可能会被弃用,这意味着您需要在旧代码中全部更改它们。那么谁没有推荐使用呢?谁反对?对不起,我不相信谣言和迷信。从php手册中可以看出,“虽然有些人认为短标记和ASP样式的标记很方便,但它们的可移植性较差,通常不推荐使用。”以及“另外请注意,如果您将php嵌入XML或XHTML,则需要使用这些标记来保持与标准的一致性。”几年来,php.ini一直是在关闭短标记的情况下发布的,所以至少在您的帖子中,您应该指出它们需要启用。在XML中嵌入php没有任何意义。手册中有错误。不可能记下所有可以关闭的东西。该代码是合法的,它为我服务了十年,从未让我失望过,而且它在未来不会被弃用。我完全同意你的观点,上校。我的意思是,在编程中尝试遵循最佳实践的意义何在?这太愚蠢了。请不要再构建一个糟糕的模板系统。PHP是一个模板系统。您实际上是在模板系统中构建模板系统。只要使用PHP!