*初めてのSmarty [#d606beab]

RIGHT:更新日&lastmod();  

**Smartyパッケージのインストール [#l248e9e2]

[[ここを見る>Linux Memo/Smarty]]

**設定 [#s8089892]

Smartyはデフォルトではテンプレートが変更されたときはコンパイルし直すが、これではデバッグ中不十分で毎回コンパイルしなおすには

Smarty/libs/Smarty.class.phpの

     * @var boolean
     */
 //   var $force_compile   =  false;
     var $force_compile   =  true;

にする

**はじめに [#zded4ecb]
Smartyの環境を設定する自分用のクラスをSmartyクラスの継承クラスとして作成しておく

''ismSmarty.class.php''

 <?php
 
 define('SMARTY_DIR', '/usr/local/Smarty/libs/');
 
 require_once(SMARTY_DIR . 'Smarty.class.php');
 
 class ismSmarty extends Smarty{
     public function __construct(){
 //         $this->Smarty();   <=必要か不明
         $this->template_dir = '/home/okada/smatry_test/templates/';
         $this->compile_dir  = '/home/okada/smatry_test/templates_c/';
         $this->config_dir   = '/home/okada/smatry_test/configs/';
         $this->cache_dir    = '/home/okada/smatry_test/cache/';
     }
 }

このクラスではSmarty.class.phpのパスと、このクラスのオブジェクトを作成するときに各必要な変数をセットする。

**変数について [#pe107f9b]
変数には単純変数、単純配列、連想配列、オブジェクト変数がある。
 
 <?php
 
 //先の自分用のクラスのパスを指定
 include("../ismSmarty.class.php");
 $Mysmarty = new ismSmarty();
 
 //単純変数
 $Mysmarty->assign("name", "QRA");
 
 //
 $Mysmarty->assign("word",array("Hello", "world", array("JE2", "ISM")));
 
 //
 $Mysmarty->assign("data",array("call"=>"JE2ISM", "QTH"=>array("QTH1"=>"三重
 県", "QTH2"=>"伊勢市", "JCC"=>"21")));
 
 //オブジェクト変数
 //クラスの定義は後からでもOKなようだ。
 $obj = new ismClass("山田花子", "2004/05/06");
 $Mysmarty->assign("object", $obj); //オブジェクト変数を指定
 
 $Mysmarty->display("test01.tpl");
 
 class ismClass{
     function __construct($_name, $_birth){
         $this->name = $_name;
         $this->birth = $_birth;
     }
     public $name;
     public $birth;
 
     function getage(){
        return floor((time()-strtotime($this->birth))/(60*60*24*365));
     }
 }
 
 ?>

***テンプレート [#q1b7f1ff]
場所は/home/okada/smatry_test/templatesで



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS