我正在尝试与 Smarty 一起做一些 OOP。例如,当我放
$smarty->display('header.tpl');
在构造函数中,一切正常。但是,当我将此代码放在一个函数中并在构造中调用该函数时,什么也没有发生。
有没有解决方案,所以我可以在函数中使用代码,然后在函数中调用它?
init.php 代码:
class init{
public function __construct(){
$smarty = new Smarty();
$smarty->setTemplateDir('templates');
$smarty->setCompileDir('templates_c');
$smarty->setCacheDir('cache');
$smarty->setConfigDir('configs');
//$smarty->testInstall();
$smarty->display('header.tpl');
$smarty->display('content.tpl');
$smarty->display('footer.tpl');
//-----------------------------------
//-----------------------------------
//check url
//-----------------------------------
//-----------------------------------
if(isset($_REQUEST['params']) && $_REQUEST['params']!=''){
$aParams = explode('/', $_REQUEST['params']);
print_r ($aParams);
if(isset($aParams[0])) $this->lang = $aParams[0];
if(isset($aParams[1])) $this->page = $aParams[1];
}
if(!$this->lang) $this->lang = 'nl';
if(!$this->page) $this->page = 'home';
//-----------------------------------
//-----------------------------------
//Functions
//-----------------------------------
//-----------------------------------
$this->buildPage();
$this->buildHeader_Footer();
}
function buildPage(){
require_once('modules/' . $this->page . '/' . $this->page . '.php');
if($this->page == 'home') new home($this->lang, $this->page, $this->action, $this->id, $this->message);
else if($this->page == 'contact') new contact($this->lang, $this->page, $this->action, $this->id, $this->message);
}
function buildHeader_Footer(){
$smarty->display('header.tpl');
$smarty->display('footer.tpl');
}
}
索引.php 代码:
require('smarty/libs/Smarty.class.php');
require_once ('modules/init/init.php');
$init = new init();