0

我目前正在努力让我的 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 but more simplistic and sing RELATIVE PATHS

我应该怎么做才能在我自己的类中设置 template_dir?

4

1 回答 1

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模板类)目录在哪里

于 2013-03-09T05:18:46.290 回答