php函数中的Smarty
我正试着和Smarty一起做一些OOP。例如,当我把php函数中的Smarty,php,smarty,Php,Smarty,我正试着和Smarty一起做一些OOP。例如,当我把 $smarty->display('header.tpl'); 在构造函数中,一切都起作用。但是,当我将此代码放入函数中并在构造中调用该函数时,什么也没有发生 有没有一个解决方案可以让我在函数中使用代码,然后在函数中调用它 init.php代码: class init{ public function __construct(){ $smarty = new Smarty(); $smarty->set
$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');
}
}
require('smarty/libs/Smarty.class.php');
require_once ('modules/init/init.php');
$init = new init();
Index.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');
}
}
require('smarty/libs/Smarty.class.php');
require_once ('modules/init/init.php');
$init = new init();
更新(问题更改后)
似乎您希望在构造函数中创建之后,可以从所有类方法访问$smarty
变量。那是错误的。可以在类内使用$this
访问类变量。所以你必须写:
$this->smarty-> ...
无论何时使用它
我不能说你的解决方案到底有什么问题,因为发布的代码不完整。但是你所尝试的应该是有效的 举个例子,我会有这样一个类:
class SimpleView {
/**
* Note that smarty is an instance var. This means that var
* is only available via `$this` in the class scope
*/
protected $smarty;
/**
* Constructor will be called if write 'new SimpleView()'
*/
public function __construct(){
// note $this
$this->smarty = new Smarty();
$this->smarty->setTemplateDir('templates');
$this->smarty->setCompileDir('templates_c');
$this->smarty->setCacheDir('cache');
$this->smarty->setConfigDir('configs');
}
/**
* The build function is public. It can be called from
* outside of the class
*/
public function build(){
$this->smarty->display('header.tpl');
$this->smarty->display('content.tpl');
$this->smarty->display('footer.tpl');
}
}
$view = new SimpleView();
$view->build();
然后像这样使用它:
class SimpleView {
/**
* Note that smarty is an instance var. This means that var
* is only available via `$this` in the class scope
*/
protected $smarty;
/**
* Constructor will be called if write 'new SimpleView()'
*/
public function __construct(){
// note $this
$this->smarty = new Smarty();
$this->smarty->setTemplateDir('templates');
$this->smarty->setCompileDir('templates_c');
$this->smarty->setCacheDir('cache');
$this->smarty->setConfigDir('configs');
}
/**
* The build function is public. It can be called from
* outside of the class
*/
public function build(){
$this->smarty->display('header.tpl');
$this->smarty->display('content.tpl');
$this->smarty->display('footer.tpl');
}
}
$view = new SimpleView();
$view->build();
我编辑了代码。我试图按照您的建议进行更改,但似乎不起作用。我在您的代码中没有看到
class
构造。另外,正如我所说的:注意$this
。上次编辑时忘了复制它。我将尝试添加$this我刚刚尝试添加$this,但它只给出一个错误,页面上没有显示任何内容。