PHPOOT

What is PHPOOT?

PHPOOT is a template engine for PHP. With this, you can separate HTML from programming code, and presentation stracture from presentation logic.

I have got the concept of this engine thanks to Amrita template library that is for Ruby.

Presentation?

Presentation is the part where your application presents information for your user. Here I classify the Presentation into three part, which are 'stracture', 'design' and 'logic'. HTML has charge of presentation stracture, CSS does of design, and PHP itself should take charge of logic. So I claim that PHPOOT should not have complicated control stracuture like 'if ... then ...'.

And the separation make it possible for designers and programmer to work on their own file. But the three part depends on each other, so plenty of communication between them is necessary.

Features

Requirements

License

the GNU Lesser General Public License version 2.1, or any later version.

Download

PHPOOT has sourceforge.jp project page. Download latest PHPOOT from there.

Logo

Author

Haruki SETOYAMA

Way to use

I show you quick start guide to use PHPOOT version 0.4 or later.

Hello World

This is a simple template file, that is written with HTML.

<html>
  <body>
    <h1 var="{title}">title will be inserted here</h1>
    <p var="{body}">body text will be inserted here</p>
  </body>
</html>

PHPOOT treats elements with "var" attribute as variable and gets the data for them from model data by the value of "var" attribute as key. The variable name of model data that is described in "var" attribute have to be enclosed with brace like {foo}.

PHP code to use the template is below.

require_once 'phpoot.php';
$tmpl = new phpoot;
$tmpl->setTemplateFile('./helloworld.html');
$model_data = array(
  'title' => 'Hello World',
  'body'  => 'PHPOOT is a template engine for PHP'
);
$tmpl->display($model_data);

The output of this code is ...

<html>
  <body>
    <h1>Hello World;/h1>
    <p>PHPOOT is a template engine for PHP</p>
  </body>
</html>

<h1 var="title">...</h1> was replaced with <h1>$model_data['title']</h1>. And <p var="body">...</p> was modified in the same way.

list

template

<ul>
  <li var="{list1}"></li>
</ul>

code

require_once 'phpoot.php';
$tmpl = new phpoot;
$tmpl->setTemplateFile('./list.html');
$model_data = array(
  'list1' => array(1, 2, 3)
);
$tmpl->display($model_data);

When the element of data model to use is array, PHPOOT display them one by one.

PHP do not distinguish array and hash, but normaly array(0 => 'foo', 1 => 'bar') is called array and array('a' => 'foo', 'b' => 'bar') is called hash. So PHPOOT look on PHP array with key 0 as array.

output

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>
  		

table

template

<table border="1">
  <tr><th>name</th><th>author</th></tr>
  <tr var="{table1}">
    <td var="{name}"></td><td var="{author}"></td>
  </tr>
</table>
    	

code

require_once 'phpoot.php';
$tmpl = new phpoot;
$tmpl->setTemplateFile('./table.html');
$model_data = array(
  'table1' => array(
    array( 'name' => 'PHP',    'author' => 'Rasmus Lerdorf' ),
    array( 'name' => 'Ruby',   'author' => 'matz' ),
    array( 'name' => 'python', 'author' => 'Guido van Rossum' )
  )
);
$tmpl->display($model_data);
		

PHPOOT accepts nestd hash.

output

<table border="1">
  <tr><th>name</th><th>author</th></tr>
  <tr>
    <td>PHP</td><td>Rasmus Lerdorf</td>
  </tr>
  <tr>
    <td>Ruby</td><td>matz</td>
  </tr>
  <tr>
    <td>python</td><td>Guido van Rossum</td>
  </tr>
</table>
  		

conditional

If a element of model data is null or false, the tag with the element, its content and its close tag will be deleted. Using this, you can select the part of template to be displayed.

If true was given as model data for some element, the content of the tag will be displayed un-modified.

template

<html>
  <body>
    <div var="{groups}">
      <h1 var="{title}"></h1>
      <div var="{no_data}">
        <em>This group has no data.</em>
      </div>
      <div var="{one_data}">
        This group has only one data: "<span var="{data}"></span>".
      </div>
      <div var="{many_data}">
        Here's the list of this group's data.
        <ul>
          <li var="{list}"></li>
        </ul>
      </div>
    </div>
  </body>
</html>
    	

code

require_once 'phpoot.php';
$tmpl = new phpoot;
$tmpl->setTemplateFile('./conditional.html');
$data = array(
  array('Group A', array('only_one')),
  array('Group B', array('one', 'two', 'three')),
  array('Group C', array())
);
$model_data = array();
foreach ($data as $datum) {
  $model_datum = array();
  $model_datum['title'] = $datum[0]; 
  switch(count($datum[1])){
  	case 0: 
  		$model_datum['no_data'] = true;
  		break;
  	case 1: 
  		$model_datum['one_data'] = array(
	      'data' => $datum[1][0]
	    );
  		break;
  	default:
  		$model_datum['many_data'] = array(
	      'list' => $datum[1]
	    );
  }
  $model_data[] = $model_datum;
}
$tmpl->display(array('groups' => $model_data));
		

output

<html>
  <body>
    <div>
      <h1>Group A</h1>
      <div>
        This group has only one data: "only_one".
      </div>
    </div>
    <div>
      <h1>Group B</h1>
      <div>
        Here's the list of this group's data.
        <ul>
          <li>one</li>
          <li>two</li>
          <li>three</li>
        </ul>
      </div>
    </div>
    <div>
      <h1>Group C</h1>
      <div>
        <em>This group has no data.</em>
      </div>
    </div>
  </body>
</html>
  		

modify attributes of HTML element

Use {data} style value to embed data in HTML attribute.

template

<table border="1">
  <tr><th>name</th><th>webpage</tr>
  <tr var="{table}">
    <td var="{name}"></td>
    <td><a href="{url}" var="{webpage}"></a></td>
  </tr>
</table>
    	

code

require_once 'phpoot.php';
$tmpl = new phpoot;
$tmpl->setTemplateFile('./attribute.html');
$model_data = array(
  'table' => array(
    array( 
		'name' 	  => 'PHP',
		'url'     => 'http://www.php.net/',
		'webpage' => 'PHP: Hypertext Preprocessor'
	),
	array( 
		'name' 	  => 'Ruby',
		'url'     => 'http://www.ruby-lang.org/',
		'webpage' => 'Ruby Home Page'
	),
	array( 
		'name' 	  => 'python',
		'url'     => 'http://www.python.org/',
		'webpage' => 'Python Language Website'
	)
  )
);
$tmpl->display($model_data);
		

output

<table border="1">
  <tr><th>name</th><th>webpage</th></tr>
  <tr>
    <td>PHP</td>
    <td><a href="http://www.php.net/">PHP: Hypertext Preprocessor</a></td>
  </tr>
  <tr>
    <td>Ruby</td>
    <td><a href="http://www.ruby-lang.org/">Ruby Home Page</a></td>
  </tr>
  <tr>
    <td>python</td>
    <td><a href="http://www.python.org/">Python Language Website</a></td>
  </tr>
</table>
  		

Note: Order of embeded attribute against var one is important.

other example

see "sample" directory in release of PHPOOT library.

This guide is

The sample of this guide is based on the guide of Amrita template library.

Revision: $Revision: 1.3 $