Example of using Teng with PHP

There are numerous ways to produce the reference example output (The Reference Example) using Teng PHP Extension. Two significantly different options exist.

The first approach uses native PHP data structures to pass a data tree from a PHP script to Teng.

Source file - palette.php:

<?php
  // initialize Teng engine with default data root
  $teng = teng_init();

  // build data tree from bottom up
  $data = array();

  for ( $i = 'A'; $i < 'F'; $i++ ) {
      $row = array( 'rnum' => $i );

      for ( $j = 0; $j < 10; $j++ )
          $row[ 'col' ][] = array( 'cnum' => $j );

      $data[ 'row' ][] = $row;
  }

  // generate page
  echo( teng_page_string( $teng, "palette.html", $data,
      array( "content_type" => "text/html",
             "encoding" => "ISO-8859-1" ) ) );

  // release teng engine (not necessary, but good practice)
  teng_release( $teng );
?>

A different approach uses the Teng extension API to build the data tree.

<?php
  // initialize Teng engine with default data root
  $teng = teng_init();

  // create new data tree
  $data = teng_create_data_root();

  // build data tree from top to bottom
  for ( $i = 'A'; $i < 'F'; $i++ ) {
     // add nested row fragment
     $row = teng_add_fragment( $data, "row", array( "rnum" => $i ) );

     for ( $j = 0; $j < 10; $j++ )
        // add nested column fragment
        teng_add_fragment( $row, "col", array( "cnum" => $j ) );
  }

  // generate page
  echo( teng_page_string( $teng, "palette.html", $data,
      array( "content_type" => "text/html",
             "encoding" => "ISO-8859-1" ) ) );

  // release data tree (not necessary, but good practice)
  teng_release_data( $data );

  // release teng engine (not necessary, boot good practice)
  teng_release( $teng );
?>

The first approach is a recommended option for simplistic data trees, with at most two nesting levels and little or no data outside the root fragment. The second approach is better suitable for more complex data trees, which would be difficult to build and navigate using PHP arrays. It should be also noted that, for larger data trees, Teng extension API is considerably more effective than native PHP data structures and consumes less memory.

Template file - palette.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Teng Template example</title>
</head>
<body>

<table border="1">
    <?teng frag row?>
        <tr>
            <td>${rnum}</td>
            <?teng frag col?>
                <td>${cnum}</td>
            <?teng endfrag?>
        </tr>
    <?teng endfrag?>
</table>

</body>
</html>

Output from Teng:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Teng Template example</title>
</head>
<body>
<table border="1">
	<tr>
		<td>A</td>
		<td>0</td>
		<td>1</td>
		<td>2</td>
		<td>3</td>
		<td>4</td>
		<td>5</td>
		<td>6</td>
		<td>7</td>
		<td>8</td>
		<td>9</td>
	</tr>
	<tr>
		<td>B</td>
		<td>0</td>
		<td>1</td>
		<td>2</td>
		<td>3</td>
		<td>4</td>
		<td>5</td>
		<td>6</td>
		<td>7</td>
		<td>8</td>
		<td>9</td>
	</tr>
	....
	<tr>
		<td>E</td>
		<td>0</td>
		<td>1</td>
		<td>2</td>
		<td>3</td>
		<td>4</td>
		<td>5</td>
		<td>6</td>
		<td>7</td>
		<td>8</td>
		<td>9</td>
	</tr>
</table>
</body>
</html>

Links: