web.curious.gk
web.curious.gk

Reputation: 167

PHP HTML Template with Loop capabilities

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'];
}

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>

The template class

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

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>

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

Upvotes: 3

Views: 17162

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157872

PHP can be used as a very basic template engine and quite usable in this role for small or educational projects. However, a more complex project will demand a more sophisticated engine, such as Twig.

Nevertheless, the principle will remain the same. Prepare your data first:

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

and then renter a template. In case of using a PHP template, simply include a .php file like one provided below.

Note that when outputting any data inside HTML, you must always escape HTML control characters.

<html>
<head>
<title><?= htmlspecialchars($pagetitle) ?></title>
</head>
<body>
 <table>
<?php foreach ($data as $chunk): ?>
  <tr>
<?php     foreach ($chunk as $row): ?>
   <td>
    <h3>Hello <?= htmlspecialchars($name) ?>!</h3>
    <p>The time is: <?= htmlspecialchars($date) ?></p>
    <p><?= htmlspecialchars($address) ?></p>
   </td>
<?php     endforeach ?>
  </tr>
<?php endforeach ?>
 </table>
</body>
</html>

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

Upvotes: 15

hakre
hakre

Reputation: 198115

Just don't re-invent the wheel, PHP works wonderfully as a templating language:

The template class

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

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

The use is pretty much the same, just assign more than one row to make use of it:

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

Hope this is helpful.

Upvotes: 5

Arfeen
Arfeen

Reputation: 2623

Your PHP code:

$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>
";
}

Then in your template design.html, you will pass the $htmltable variable and embedd there:

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

Upvotes: 0

Related Questions