是否可以在派生构造函数中设置 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'); 有效。
有什么想法吗?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即使在构造函数的上下文中,您也可以使用
$this
。所以尝试一下:应该有效。
You can use
$this
even in context of the constructor. So try:Should work.
模板目录可以是一个数组,并且它可能在内部执行此操作。还有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.
您确定问题出在该代码上吗?我在所有应用程序中使用这样的代码:
Are you sure the problem is from that code? I use in all my apps code like this: