Location: PHPKode > scripts > PHP MicroTemplate > php-microtemplate/HTPL.php
<?php // $Id: HTPL.php,v 1.1.1.1 2001/11/28 18:24:51 ramenboy Exp $

/*
 * HTPL - HTML specialization of MTPL (MicroTemplate)
 *
 * This class lets you get away with block markers that look like
 * "<!--- BEGIN: some_block --->" (note the triple-hyphen). It works by
 * first doing a string replace from '<!---' to '--->' and then exploding
 * on the string '--->'. The performance penalty for the string replacement
 * is only slight.
 *
 * The reason for the triple-hyphen is to distinguish block markers from
 * ordinary HTML comments. You can change this if you like, but be aware that
 * all HTML comments will be stripped out as a result. This means that tricks
 * like <script><!-- ... // --></script> will be reduced to <script></script>,
 * which renders your JavaScript useless. This is the best solution I could
 * find without resorting to regular expressions and sacrificing efficiency.
 * If you've got a better idea, please let me know.
 *
 * This class also includes the static methods checkbox(), option(), and
 * radio(), which provide simple methods to map hash tables to sets of
 * form elements. See the test cases for examples of their use.
 *
 * If this script is opened directly, it will look for a sample template
 * file called "test.htpl" and run some brief test cases that should
 * demonstrate how this class is to be used.
 *
 * Author:  Dave Benjamin <hide@address.com>
 * Source:  http://www.ovumdesign.com/
 * Created: Mon Nov 26 11:45:11 MST 2001
 * Version: $Revision: 1.1.1.1 $
 */

require_once 'MTPL.php';

define('HTPL_SEP1', '<!---');
define('HTPL_SEP2', '--->');

class HTPL extends MTPL {

// public:

    // Reads a template file and returns an HTPL object
    function readFile($file) {
        $text = @file($file) or MTPL::error(E_MTPL_FILE, $file);
        return new HTPL(implode('', $text));
    }

    // Returns an array that can be used to generate a set of checkboxes
    function checkbox($in, $sel = array(), $kname = 'value',
                                           $vname = 'label') {
        $out = array();

        foreach ($in as $key => $val) {
            $out[] = array(
                $kname    => $key,
                $vname    => $val,
                'checked' => in_array($key, $sel) ? 'checked' : ''
            );
        }

        return $out;
    }

    // Returns an array that can be used to generate a set of <option> tags
    function option($in, $sel = null, $kname = 'value', $vname = 'label') {
        $out = array();

        foreach ($in as $key => $val) {
            $out[] = array(
                $kname     => $key,
                $vname     => $val,
                'selected' => $key == $sel ? 'selected' : ''
            );
        }

        return $out;
    }

    // Returns an array that can be used to generate a set of radio buttons
    function radio($in, $sel = null, $kname = 'value', $vname = 'label') {
        return HTPL::checkbox($in, array($sel), $kname, $vname);
    }

// protected:

    // Returns an alternating list of block markers and content
    function explodeTemplate($str) {
        return explode(HTPL_SEP2, str_replace(HTPL_SEP1, HTPL_SEP2, $str));
    }
}

// Test cases
// ----------

// Evaluate the following code if this script is called directly.
if (realpath($SCRIPT_FILENAME) == __FILE__):

// Report all errors, warnings, and notices.
error_reporting(E_ALL);

// Start the timer.
list($a, $b) = explode(' ', microtime()); $time = $a + $b;

// Initialize a list of variable assignments for the parseLoop() example.
$vars_list = array(
    array('b' => 5, 'c' => 6),
    array('b' => 7, 'c' => 8),
);
$vars = array('user' => array('name' => 'joe'));

// Read the template file.
$htpl = HTPL::readFile('test.htpl');

// Demonstrate parseLoop() and parseOut().
$htpl->parseLoop('main.sub', $vars_list, $vars);
echo $htpl->parseOut('main.sub2', array('a' => 4));

// Demonstrate basic parse().
$htpl->parse('main.sub2');
$htpl->parse('main.sub2');
echo $htpl->parse('main', array('a' => 2));

// Demonstrate using HTPL utility functions to create a form.
$vars = array(
    'AZ' => 'Arizona',
    'CA' => 'California',
    'ZK' => 'Planet Zektar',
);

isset($select)   or $select   = 'CA';
isset($checkbox) or $checkbox = array('AZ', 'ZK');
isset($radio)    or $radio    = 'ZK';

$htpl->parseLoop('form.option',   HTPL::option($vars, $select));
$htpl->parseLoop('form.checkbox', HTPL::checkbox($vars, $checkbox));
$htpl->parseLoop('form.radio',    HTPL::radio($vars, $radio));

echo $htpl->parse('form');
print_r(compact('select', 'checkbox', 'radio'));

// Stop the timer.
list($a, $b) = explode(' ', microtime());
$time = round($a + $b - $time, 4);
echo "<p>Time elapsed: $time seconds.</p>";

endif; // this script is called directly

?>
Return current item: PHP MicroTemplate