是否可以在派生构造函数中设置 smarty 目录?

发布于 2024-10-23 13:33:08 字数 759 浏览 13 评论 0原文

嘿,我正在尝试做这样的事情:

<?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'); 有效。

有什么想法吗?

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 $this 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随心而道 2024-10-30 13:33:08

即使在构造函数的上下文中,您也可以使用 $this。所以尝试一下:

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

应该有效。

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.

弥繁 2024-10-30 13:33:08

模板目录可以是一个数组,并且它可能在内部执行此操作。还有addTemplateDir()。 Smarty 会按顺序遍历它们。使用$this->是构造函数的正确方法。

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.

横笛休吹塞上声 2024-10-30 13:33:08

您确定问题出在该代码上吗?我在所有应用程序中使用这样的代码:

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";
(...)

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";
(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文