Smartyはデフォルトではテンプレートが変更されたときはコンパイルし直すが、これではデバッグ中不十分で毎回コンパイルしなおすには
Smarty/libs/Smarty.class.phpの
* @var boolean
*/
// var $force_compile = false;
var $force_compile = true;
にする
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のパスと、このクラスのオブジェクトを作成するときに各必要な変数をセットする。
変数には単純変数、単純配列、連想配列、オブジェクト変数がある。
<?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));
}
}
?>
場所は/home/okada/smatry_test/templatesで