具有循环功能的PHP HTML模板 [英] PHP HTML Template with Loop capabilities

查看:87
本文介绍了具有循环功能的PHP HTML模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to ask some help and ideas on how to implement a loop inside the template. I can do foearch below but how can i include it to the template and show it in the results.

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

我想要达到的结果如下所示,以及如何放置$ template-> publish();.在变量中,这样我就可以用它来将数据存储到数据库中.非常感谢.

What i want to achieve the results is something like below and how do I put the $template->publish(); in a variable so I can use it to store that data to the DB. thanks a lot.

<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

The 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

the 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本身在模板方面和其他引擎一样出色.
不需要其他了

PHP itself is as good at templates as any other engine.
No need anything else

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

我使您的示例更加复杂,但更接近于现实生活.
它将在每行3列的表格中打印您的表格

I made your example a bit more complicated yet closer to the real life.
It will print your table in the rows by 3 columns in each

这篇关于具有循环功能的PHP HTML模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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