开发者

Is it possible to set smarty directories in a derived constructor?

开发者 https://www.devze.com 2023-02-17 10:12 出处:网络
Hey, I\'m trying to do something like this: <?php class MySmarty extends Smarty { public function __construct() {

Hey, I'm trying to do something like this:

<?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");
?>

But it fails with a SmartyException("Unable to load template file"). From smarty_internal_template.php line 163, which looks like it checks for the existence of $th开发者_Go百科is before doing any display.

My system seems to be set up correctly, since the suggested way of doing it (calling $smarty->set*Dir($smartyRoot.'foo'); works.

Any ideas?


You can use $this even in context of the constructor. So try:

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

Should work.


The template dir can be an array, and it might internally do that. There is also addTemplateDir(). Smarty will traverse them in order. Using $this-> is the correct approach for the constructor.


Are you sure the problem is from that code? I use in all my apps code like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号