phpTenjin Examples

last update: $Date: 2008-02-22 23:39:32 +0900 (Fri, 22 Feb 2008) $

Release: 0.0.2

Table of Contents:

Bordered Table

Template: 'table.phtml'
<html>
  <body>
    <h1>{=$title=}</h1>
    <table>
<?php $i = 0; ?>
<?php foreach ($items as $item) { ?>
<?php     $color = ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; ?>
      <tr bgcolor="{==$color=}">
        <td>{==$i=}</td>
        <td>{=$item=}</td>
      </tr>
<?php } ?>
    </table>
  </body>
</html>
Convert into PHP script:
$ phptenjin -a convert table.phtml
<?php echo '<html>
  <body>
    <h1>', htmlspecialchars($title), '</h1>
    <table>
'; $i = 0; 
 foreach ($items as $item) { 
     $color = ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'; 
echo '      <tr bgcolor="', $color, '">
        <td>', $i, '</td>
        <td>', htmlspecialchars($item), '</td>
      </tr>
'; } 
echo '    </table>
  </body>
</html>
';
?>
Main program: 'table.php'
<?php
// create Engine object
require_once('Tenjin.php');
$engine = new Tenjin_Engine();

// render template with context data
$context = array('title'=>'Bordered Table Example',
                 'items'=>array('<AAA>', 'B&B', '"CCC"'));
$output = $engine->render('table.phtml', $context);
echo $output;
?>
Result:
$ php table.php
<html>
  <body>
    <h1>Bordered Table Example</h1>
    <table>
      <tr bgcolor="#CCCCFF">
        <td>1</td>
        <td>&lt;AAA&gt;</td>
      </tr>
      <tr bgcolor="#FFCCCC">
        <td>2</td>
        <td>B&amp;B</td>
      </tr>
      <tr bgcolor="#CCCCFF">
        <td>3</td>
        <td>&quot;CCC&quot;</td>
      </tr>
    </table>
  </body>
</html>

Form

Template: 'form.phtml':
<?php #@ARGS action, params, label ?>
<form action="{=$action=}" method="post">
  <p>
    <label>Name:</label>
    <input type="text" name="name" value="{=$params['name']=}" />
  </p>
  <p>
    <label>Gender:</label>
    <?php $gender = $params['gender']; ?>
    <input type="radio" name="gender" value="M" {==checked($gender=='M')=} />Man
    <input type="radio" name="gender" value="W" {==checked($gender=='W')=} />Woman
  </p>
  <input type="submit" value="{=$label=}" />
</form>
Template: 'create.phtml':
<?php $_context['title'] = 'Create user'; ?>
<?php $_context['label'] = 'Create'; ?>
<?php $_context['action'] = 'action.cgi'; ?>
<?php import(':form'); ?>
Template: 'update.phtml':
<?php $_context['title'] = 'Update user'; ?>
<?php $_context['label'] = 'Update'; ?>
<?php $_context['action'] = 'update.cgi'; ?>
<?php import(':form'); ?>
Layout template: 'layout.phtml'
<html>
 <body>
  <h1>{=$title=}</h1>
  <div class="main-content">
{==$_content=}
  </div>
 </body>
</html>
Main program: 'main.php':
<?php
// create Engine object
require_once('Tenjin.php');
$properties = array('postfix'=>'.phtml', 'layout'=>'layout.phtml');
$engine = new Tenjin_Engine($properties);

// render template with context data
$params = array('name'=>'Foo', 'gender'=>'M');
$context = array('params'=>$params);
$output = $engine->render(':update', $context);   # ':update' == 'update'+postfix
echo $output;
?>
Result:
$ php main.php
<html>
 <body>
  <h1>Update user</h1>
  <div class="main-content">
<form action="update.cgi" method="post">
  <p>
    <label>Name:</label>
    <input type="text" name="name" value="Foo" />
  </p>
  <p>
    <label>Gender:</label>
    <input type="radio" name="gender" value="M"  checked="checked" />Man
    <input type="radio" name="gender" value="W"  />Woman
  </p>
  <input type="submit" value="Update" />
</form>

  </div>
 </body>
</html>

Preprocessing

Library: 'helper.php'
<?php
$languages = array(
    'en' => 'Engilish',
    'fr' => 'French',
    'de' => 'German',
    'es' => 'Spanish',
    'ch' => 'Chinese',
    'ja' => 'Japanese',
);

function link_to($label, $action=null, $id=null) {
    $url = '/app';
    if ($action) $url .= '/'.$action;
    if ($id)     $url .= '/'.$id;
    $url = preg_replace('/%2F/', '/', urlencode($url));
    return "<a href=\"".$url."\">$label</a>";
}
?>
Template: 'select.phtml'
<?PHP require('helper.php'); ?>
<form>
  <label>Select your language:</label>
  <select name="lang">
  <?php $lang = $params['lang']; ?>
  <?php $table = array(true=>' selected="selected"', false=>''); ?>
<?PHP foreach ($languages as $val=>$name) { ?>
    <option value="{*==$val=*}" {==$table[$lang=='{*==$val=*}']=}>{*=$name=*}</option>
<?PHP } ?>
  </select>
  <input type="submit" value="OK" />
</form>
<p>
  {*==link_to('Back', 'index')=*} |
  {*==link_to('Show '._ep('$params["name"]'), 'show', _p('$params["id"]'))=*}
</p>
Preprocessed code:
$ phptenjin -a preprocess select.phtml
<form>
  <label>Select your language:</label>
  <select name="lang">
  <?php $lang = $params['lang']; ?>
  <?php $table = array(true=>' selected="selected"', false=>''); ?>
    <option value="en" {==$table[$lang=='en']=}>Engilish</option>
    <option value="fr" {==$table[$lang=='fr']=}>French</option>
    <option value="de" {==$table[$lang=='de']=}>German</option>
    <option value="es" {==$table[$lang=='es']=}>Spanish</option>
    <option value="ch" {==$table[$lang=='ch']=}>Chinese</option>
    <option value="ja" {==$table[$lang=='ja']=}>Japanese</option>
  </select>
  <input type="submit" value="OK" />
</form>
<p>
  <a href="/app/index">Back</a> |
  <a href="/app/show/{==$params["id"]=}">Show {=$params["name"]=}</a>
</p>
Main program: 'main.php'
<?php
// create engine
require 'Tenjin.php';
$properties = array('postfix'=>'.phtml', 'preprocess'=>true);
$engine = new Tenjin_Engine($properties);

// render template with context data
$params = array('id'=>1234, 'name'=>'Foo', 'lang'=>'ch');
$context = array('params'=>$params);
$output = $engine->render(':select', $context);
echo $output;
?>
Result:
$ php main.php
<form>
  <label>Select your language:</label>
  <select name="lang">
    <option value="en" >Engilish</option>
    <option value="fr" >French</option>
    <option value="de" >German</option>
    <option value="es" >Spanish</option>
    <option value="ch"  selected="selected">Chinese</option>
    <option value="ja" >Japanese</option>
  </select>
  <input type="submit" value="OK" />
</form>
<p>
  <a href="/app/index">Back</a> |
  <a href="/app/show/1234">Show Foo</a>
</p>