Paradigm Shift Design

ISHITOYA Kentaro's blog.

register_resource

Smartyテンプレートを動的に作成する方法 - Paradigm Shift Designの続き.

ちょっとテストコードを書いてみた.

<?php
require_once("smarty/Smarty.class.php");
class Resource{
    protected $templates = array();
    public function getTemplate($name, $source, $object){
        echo $this->templates[$name];
        return true;
    }
    public function getTimestamp($name, $timestamp, $object){
        echo "getTimestamp";
        return true;
    }
    public function getSecure($name, $object){
        return true;
    }
    public function getTrusted($name, $object){
    }
    public function addTemplate($name, $template){
        $this->templates[$name] = $template;
    }
}

$resource = new Resource();
$template = file_get_contents("test.tpl");
$resource->addTemplate("test.tpl", $template);
$smarty = new Smarty();
$smarty->register_resource("pages",
                           array(array($resource, "getTemplate"),
                                 array($resource, "getTimestamp"),
                                 array($resource, "getSecure"),
                                 array($resource, "getTrusted")));

$smarty->display("pages:test.tpl");
?>

クラスのメソッド呼べるかなと思ったけど,問題なかった.ん,すばらしい.