色々あって、Smartyが使えないケースがありまして、
しかし!私の気持ちてきにはsmartyを使って画面表示制御を行ないたくて…
出した結論が…
Smartyっぽいクラスで代替しよう!でした。
まぁ、そんな時に書いたのが下記のクラスです。
class MySmarty {
protected $template_dir;
protected $assign;
protected $disp_stream;
protected $const;
public function __construct(){
$this->template_dir = PRIVATE_DIR.'display';
$this->assign = array();
$this->const['BASE_URL'] = BASE_URL;
}
public function assign( $arg1, $arg2 = null){
if( is_array( $arg1) ){
if( count($arg1) == 0 ) return;
foreach( $arg1 as $i => $v ) $this->assign[$i] = $v;
} else {
$this->assign[$arg1] = $arg2;
}
}
public function setConstants( $arg1, $arg2 = null){
if( is_array( $arg1) ){
if( count($arg1) == 0 ) return;
foreach( $arg1 as $i => $v ) $this->const[$i] = $v;
} else {
$this->const[$arg1] = $arg2;
}
}
public function setDisplay( $file ){
$file = $this->template_dir.'/'.$file;
$this->disp_stream = file_get_contents( $file );
}
public function display(){
$disp = $this->disp_stream;
$this->assign( $this->const );
foreach( $this->assign as $i => $v ){
$disp = str_replace( '{'.$i.'}', $v, $disp );
}
$disp = str_replace( '{literal}', '', $disp );
$disp = str_replace( '{/literal}', '', $disp );
echo $disp;
}
public function getIndexs(){
$str = $this->disp_stream;
$ret = array();
$max = strlen( $str );
$pos = 0;
$qti = '';
while( ($pos = strpos( $str, '{', $pos )) !== false ) {
$epos = 0;
if( ($epos = strpos( $str, '}', $pos )) === false ){
echo "'}' is not found";
break;
}
$tmp = substr( $str, $pos+1, $epos - $pos -1 );
switch( $tmp ){
case 'literal':
$pos = strpos( $str, '{/literal}' );
if( $pos === false ) die('literal format error');
$pos += 10;
continue;
case 'qtiBegin':
$dpos = $pos + 10;
$epos = strpos( $str, '{qtiEnd}', $pos );
$ret[$qti] = substr( $str, $dpos, $epos - $dpos );
$tstr = $str;
$str = substr( $tstr, 0, $pos ).
substr($tstr, $epos+8);
$pos = $epos + 8;
$this->disp_stream = $str;
continue;
}
$ret[$tmp] = '';
$qti = $tmp;
$pos = $epos;
}
return $ret;
}
}