Skip to content
augustohp edited this page Dec 15, 2011 · 3 revisions

Some use cases to Respect Template.

The template

This should be done be a front end developer. It is just vanilla HTML as it should be.

<!doctype html>
<html>
  <head><title></title></head>
  <body>
    <h1></h1>

    <ul id="buy">
        <li>Thing one</li>
        <li>Thing two</li>
        <li>Thing three</li>
    </ul>
    <table>
      <thead>
        <tr>
          <th> What? </th>
          <th> Where? </th>
          <th> Price </th>
        </tr>
      </thead>
      <tbody>
        <tr class="odd respect-template">
          <td></td>
          <td></td>
          <td>R$</td>
        </tr>
        <tr class="even respect-template">
          <td></td>
          <td></td>
          <td>R$</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

The PHP code

The code needed to render the above template with some data. This should not be too much different from what backend developers do today.

<?php
use Respect\Template\Html;

$template              = new Html('buy.html');
$template['title']     = 'Things to buy';
$template['body > h1'] = 'Things to buy';
$template['#buy']       = array('Apples', 'Bananas', 'Beer');
$template['table']     = array(
    array('Beer', 'Doe Market', '4,00'),
    array('Beer', 'Appleseed Market', '3,75'),
    array('Beer', 'FloorMart', '3,65'),
); 
echo $template; // or `echo $template->render();`

The Result

<!doctype html>
<html>
  <head><title>Things to buy</title></head>
  <body>
    <h1>Things to buy</h1>

    <ul id="buy">
      <li>Apples</li>
      <li>Bananas</li>
      <li>Beer</li>
    </ul>
    <table>
      <thead>
        <tr>
          <th> What? </th>
          <th> Where? </th>
          <th> Price </th>
        </tr>
      </thead>
      <tbody>
        <tr class="odd">
          <td>Beer</td>
          <td>Doe Market</td>
          <td>R$4,00</td>
        </tr>
        <tr class="even">
          <td>Beer</td>
          <td>Appleseed Market</td>
          <td>R$3,75</td>
        </tr>
        <tr class="odd">
          <td>Beer</td>
          <td>FloorMart</td>
          <td>R$3,65</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
Clone this wiki locally