2011-03-16 32 views
0

嘿,我試圖做這樣的事情:是否可以在派生構造函數中設置smarty目錄?

<?php 
class MySmarty extends Smarty { 
    public function __construct() { 
     parent::__construct(); 

     $smartyRoot = '/home/smarty/'; 

     parent::setTemplateDir($smartyRoot. "/templates/"); 
     parent::setCompileDir($smartyRoot."/templates_c/"); 
     parent::setCacheDir($smartyRoot."/cache/"); 
     parent::setConfigDir($smartyRoot."/configs/"); 
    } 
} 

$smarty = new MySmarty(); 
$smarty->display("index.tpl"); 
?> 

但它無法用SmartyException("Unable to load template file")。從smarty_internal_template.php行163,它看起來像在檢查任何顯示之前檢查是否存在$this

我的系統似乎是正確設置,因爲這樣做的建議方式(調用$smarty->set*Dir($smartyRoot.'foo');作品

任何想法

回答

0

你確定這個問題是從代碼?我在所有的應用程序代碼一樣使用這個:

class MySmarty extends Smarty { 

function __construct() { 
    global $path, $template_dir, $production; 


    parent::__construct(); 

    $this->template_dir = $template_dir; 
    $this->compile_dir = "$template_dir/tpl_c"; 
    $this->config_dir = "$template_dir/configs"; 
    $this->cache_dir = "$template_dir/cache"; 
(...) 
+0

呵呵。不,我不完全確定,我只是以爲我已經消除了我所有的其他選擇。做你做的實際工作(即'$ this - > * _ dir ='foo';'而不是'$ this-> setFooDir();'這有點奇怪 – quodlibetor 2011-03-16 18:44:51

2

你甚至可以在構造函數中的上下文中使用$this因此,嘗試?:

$this->setTemplateDir($smartyRoot. "/templates/"); 
    $this->setCompileDir($smartyRoot."/templates_c/"); 
    $this->setCacheDir($smartyRoot."/cache/"); 
    $this->setConfigDir($smartyRoot."/configs/"); 

應該工作。

+0

這實際上是我開始的,而且完全沒有用。即使我同意它應該。我遇到的一個大問題是$ this-> setTemplateDir('');出於某種原因,將$ template_dir轉換爲數組。很確定這不應該發生。 – quodlibetor 2011-03-16 18:46:24

1

該模板目錄可以是一個數組,它可能在內部做到這一點。還有addTemplateDir()。 Smarty將按順序遍歷它們。使用$ this->是構造函數的正確方法。

+0

啊,謝謝,這絕對有道理,我只是由於複製/粘貼代碼不起作用,認爲存在某種錯誤。 – quodlibetor 2011-03-18 18:19:58

相關問題