使用相对路径的Smarty个性化类

时间:2013-03-09 05:13:18

标签: php smarty

我目前正在努力让我的智能模板有一个类。

如果我采取行动:

$smarty = new Smarty;
$smarty->setTemplateDir('../smarty/templates');

它将我的模板目录设置为“/ smarty / templates”... 所以我试着在班上做同样的事情:

class Template extends Smarty
{
    function __construct()
    {
        parent::__construct();
        $this->template_dir = "../smarty/templates/";
        $this->compile_dir = "../smarty/templates_c/";
    }
}

所以我可以简单地说:

$smarty = new Template;

这样,我就不必在我使用的每个页面上为模板声明我的目录。

错误:

Fatal error: Uncaught exception 'SmartyException' with message
'Unable to load template file 'home.tpl'' in
/home/content/p/i/e/piec5762/html/alex/school/assets/php/smarty/sysplugins
/smarty_internal_templatebase.php:127
Stack trace:
#0
/home/content/p/i/e/piec5762/html/alex/school/assets/php/smarty/sysplugins
/smarty_internal_templatebase.php(374):
Smarty_Internal_TemplateBase->fetch('home.tpl', NULL, NULL, NULL,
true)
#1
/home/content/p/i/e/piec5762/html/alex/school/index.php(40):
Smarty_Internal_TemplateBase->display('home.tpl')
#2
{main} thrown in
/home/content/p/i/e/piec5762/html/alex/school/assets/php/smarty/sysplugins
/smarty_internal_templatebase.php on line 127

我有文件:

index.php - where I'm making the object "$smarty"
assets/php/templates.php - where class Template
assets/smarty/templates - where I need to set the directory to
assets/php/smarty - where all my smarty files are

它与此相同:Extending PHP Smarty Singleton Class但更简单并且唱出相对路径

我应该怎样做才能在我自己的班级设置template_dir?

1 个答案:

答案 0 :(得分:1)

发生错误后

更新。您似乎与index.php有关,所以应该是:

class Template extends Smarty {
  function __construct() {
    parent::__construct();
    $this->setTemplateDir("./smarty/templates/");
    $this->setCompileDir("./smarty/templates_c/");
    }
  }

如果你想成为路径安全的话:

class Template extends Smarty {
  function __construct() {
    parent::__construct();
    $this->setTemplateDir(__DIR__."/../smarty/templates/");
    $this->setCompileDir(__DIR__."/../smarty/templates_c/");
    }
  }

__DIR__是当前文件(即您的templates.php模板类)目录