3

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();
?>
1
  • Please don't build yet another awful templating system. PHP is a templating system. You are literally building a templating system in a templating system. Just use PHP! Commented Nov 28, 2011 at 6:24

3 Answers 3

15

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

Sign up to request clarification or add additional context in comments.

6 Comments

Short tags in PHP, i.e. <?= ?> have not been recommended for years, will need to be enable in the php.ini and may be deprecated in the future which means you will need to change them all in your old code. So use <?php echo $xxc ?>
have not been recommended by whom? Deprecated by whom? I don't listen to rumors and superstitions, sorry.
From the php manual "As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended." and "Also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards." and the php.ini for several years has been released with short tags off so at the very least in you post you should have pointed out that they need enabling.
There is no point in embedding PHP in XML. There are bugs in the manual. it is impossible to note every thing that can be turned off. The code is legitimate, it served me for a decade and never failed me, and it is not going to be deprecated in the future.
I quite agree with you Col. i mean what is the point in trying to follow best practices in programming it's just silly.
|
5

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.

Comments

0

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>

4 Comments

thanks for the reply Arfeen. is it possible to store the result to a variable so I could use it for storing to DB? ex: $results = $template->publish(); echo $results;
your template class is not sending back the result, rather it is displaying output on console, so in your template class, you need to use return, but then it should not have embedded tag, it should be variable printed directly.
-1. Having HTML in the code making whole idea of templates spoiled.
@webdev.gk what's the point in storing filled template in the database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.