[PHP-users 1884] simple な template class を作ってみました。

Osamu Shigematsu php-users@php.gr.jp
Thu, 06 Sep 2001 17:31:39 +0900


重松です。こんにちは。

以前、大垣さんに教えて頂いた方法を使って、template class を自作してみました。

一応、入れ子も OK ですけど、その場合は、明示的に参照渡しにしてください。
# あるいは一番深いところから register していく。

<?php

require_once('simple_template.inc');
require_once('simple_checkbox.inc');

class lovely extends simple_checkbox{
    var $choices = array('dog','cat','rabbit');
}
 
$tpl1 = new simple_template('main.tpl');
$tpl2 = new simple_template('sub.tpl');
$tpl3 = new simple_template('sub.tpl');

$tpl1->register('CONTENT',&$tpl2);
$tpl2->register('CONTENT',&$tpl3);
$tpl3->register('CONTENT',new lovely('COMPANION',2));

print $tpl1->parse();

?>

<!-- main.tpl -->
かわいい動物を選んでください。
<%=$CONTENT%>
<!-- main.tpl -->

<!-- sub.tpl -->
<%=$CONTENT%>
<!-- sub.tpl -->

実行結果
<!-- main.tpl -->
かわいい動物を選んでください。
<!-- sub.tpl -->
<!-- sub.tpl -->
<input type="checkbox" name="COMPANION[]" value="0" />dog&nbsp;
<input type="checkbox" name="COMPANION[]" value="1" />cat&nbsp;
<input type="checkbox" name="COMPANION[]" value="2" checked="checked"
/>rabbit
<!-- sub.tpl -->
<!-- sub.tpl -->
<!-- main.tpl -->

アンケートなんかで便利だと思いますがいかがなもんでしょうか。

表なんかの行を自動的にへこへこのばすような実装はできていません。
良いアイデアがありましたら、アドバイスよろしくお願いします。m(__)m

<?php

class simple_object{
    var $val;
    
    function simple_object($val){
        $this->val = &$val;
    }
    
    function parse(){
        $html = (string)$this->val;
        return $html;
    }
}

class simple_template extends simple_object{
    var $tpl;
    var $val = array();
    
    function simple_template($tpl){
        $this->tpl = $tpl;
    }

    function register($key,$val){
        $this->val[$key] = &$val;
    }
    
    function parse(){
        foreach($this->val as $key => $val){
            $$key = (is_subclass_of($val,'simple_object'))
                    ? $val->parse() : (string)$val;
        }
        
        ob_start();
        include($this->tpl);
        $html = ob_get_contents();
        ob_end_clean();
        
        return $html;
    }
}

class simple_input extends simple_object{
    var $name;
    
    function simple_input($name,$val){
        $this->name    = $name;
        $this->val    = $val;
    }
}

class simple_popup extends simple_input{
    var $choices = array('FALSE','TRUE');
    function parse(){
        $html  = "<select name=\"{$this->name}\">\n";
        foreach($this->choices as $key => $val){
            $html .= '<option value="' . $key . '" ';
            $html .= ($key == $this->val) ? 'selected="selected">' : '>';
            $html .= $val;
            $html .= "</option>\n";
        }
        $html .= '</select>';
        return $html;
    }
}

class simple_checkbox extends simple_input{
    var $choices = array('FALSE','TRUE');
    
    function parse(){
        if(!is_array($this->val))
            $this->val = empty($this->val) ? array() : array($this->val);
            
        foreach($this->choices as $key => $val){
            $html[] =
                '<input type="checkbox" name="' . $this->name . '[]" ' .
                'value="' . $key . '" ' .
                (in_array($key,$this->val) ? 'checked="checked" />' : '/>')
.
                $val;
        }
        return @join("&nbsp;\n",$html) . "\n";
    }
}

?>


-- 
Osamu Shigematsu

http://www.ravi.ne.jp/%7eshige/
mailto:shige@ravi.ne.jp