怠惰なテンプレライブラリRaw Template Engineを更新しました。

Raw Template Engine 0.2

更新点:

  • プラグインによる拡張機能追加

大層な新機能ですが実際は・・・

テンプレ:

<html>
<head>
<title><?=$title?></title>
</head>

<body>
<h1><?=$title?><h1>
<hr />
<h2>Methods</h2>
<ul>
<?foreach ($methods as $method):?>
    <li><?=$method?></li>
<?endforeach;?>
</ul>
<h2>Directories</h2>
<ul>
    <li>
        plugins/
        <ul>
            <li>html_options.php</li>
            <li>html_radios.php</li>
        </ul>
    </li>
    <li>
        templates/
        <ul>
            <li>index.php</li>
        </ul>
    </li>
    <li>index.php</li>
</ul>
<h2>Plugins</h2>
<ul>
    <li><?html_options("html_options", $plugins)?></li>
    <li><?html_radios("html_radios", $plugins)?></li>
</ul>
</body>
</html>

PHP:

<?php
include_once("Raw.php");

$raw =& new Raw();
$raw->assign("title", "Raw Template Engine");
$raw->assign("methods", array("assign", "assign_by_ref", "fetch", "display"));
$raw->assign("plugins", array(1 => "html_options", 2 => "html_radios"));
$raw->display("index.php");

ライブラリ:

<?php
//
// Raw Template Engine - the template engine using raw php.
//
// Copyright (C) 2005 Masaki Komagata <komagata@p0t.jp> 
//     All rights reserved.
//     This is free software with ABSOLUTELY NO WARRANTY.
//
// You can redistribute it and/or modify it under the terms of 
// the PHP License, version 3.0.
//
class Raw {
    var $template_dir = "templates/";
    var $plugin_dir = "plugins/";
    var $template_vars = array();
    function assign($name, $value) { $this->template_vars[$name] = $value; }
    function assign_by_ref($name, &$value) { $this->template_vars[$name] =& $value; }
    function display($template) { echo $this->fetch($template); }
    function fetch($template) {
        $this->_load_plugins();
        extract($this->template_vars);
        ob_start();
        include($this->template_dir.$template);
        $result = ob_get_contents();
        ob_end_clean();
        return $result;
    }
    function _load_plugins() {
        if (is_dir($this->plugin_dir) and ($dh = opendir($this->plugin_dir))) {
            while (($file = readdir($dh)) !== false) @include_once($this->plugin_dir.$file);
            closedir($dh);
        }
    }
}

表示:

raw-0.2.png

ただの関数をsmartyプラグインみたいに見せてるだけです!

Comments


Option