2012年1月16日月曜日

smarty オブジェクトで操作

require_once('MySmarty.php');
require_once('user_bean.php');

$uses=array();
$uses[] = new user_bean(1,"test_1","2011/01/01");
$uses[] = new user_bean(2,"test_2","2010/01/01");
$uses[] = new user_bean(3,"test_3","2009/01/01");
           
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('uses',$uses);
$mysmarty->display('index.tpl');
?>
//beanクラス
<?php
class user_bean{
    //プロパティ
    public $name;
    public $id;
    public $birth;
    //メソッド
    function __construct($id,$name,$birth){
        $this->id = $id;
        $this->name = $name;
        $this->birth = $birth;
    }
    public function getage(){
        return floor((time()-strtotime($this->birth))/(60*60*24*365));
    }
}
?>

//テンプレート
{foreach from=$uses item=use}
    <p>id {$use->id}</p>
    <p>名前 {$use->name}</p>
    <p>歳 {$use->getage()}</p>
{/foreach}

0 件のコメント:

コメントを投稿