【发布时间】:2014-02-26 05:32:53
【问题描述】:
KSmarty 是一个 Kohana 模块,旨在将 Smarty 与 Kohana 集成。我正在尝试将我当前的项目(已经在使用 Smarty)迁移到使用 Kohana。
我正在尝试设置 KSmarty,但在使模板正常工作时遇到了困难。这是来自 KSmarty 的“hello world”示例:
application/classes/Controller/Welcome.php
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Welcome extends Controller_Template
{
public $template = 'welcome';
public function action_index()
{
// Assign a value to the variable 'intro'
$this->template->intro = 'Hello world!';
// Create a nested view by loading a different template
$this->template->content = View::factory('content');
}
}
// End Welcome
application/views/welcome.tpl
<html>
<body>
<h1>{$intro}</h1>
<p>
{$content}
</p>
</body>
</html>
application/views/content.tpl
Yes, this works!
但是,对我来说,控制器/视图组合无法按预期工作。以下是我尝试过的action_index() 的变体:
public function action_index()
{
echo 'foo';
}
// Output: foo
public function action_index()
{
// Assign a value to the variable 'intro'
$this->template->intro = 'Hello world!';
// Create a nested view by loading a different template
$this->template->content = View::factory('content');
}
// No output
// No error in apache log, php log, or kohana log
public function action_index()
{
Ksmarty::instance()->assign(array(
'intro' => 'Hello world!',
'content' => APPPATH.'/views/content.tpl'
// Note: also changed {$content} in template to {include $content}
));
Ksmarty::instance()->display(APPPATH.'/views/welcome.tpl');
}
// Expected HTML output
我可以像这样简单地使用 Ksmarty::instance() 并让我的网站正常运行,但这不是 Kohana Views 系统的设计方式,感觉就像是一个杂物,尤其是在 KSmarty 示例之后与 Kohana 对视图的使用相匹配。
我把头发拉出来试图把这个固定下来,考虑到 Kohana 在初始安装时给我的头发拉扯量,这令人印象深刻。我做错了什么?
哦,为了达到这一点,我对 KSmarty 做了两个更改:
-
Kohana::config('smarty')的所有实例都替换为Kohana::$config->load('smarty');据我所知,这是 Kohana 版本更改的问题。 - 注释掉
$s->security = Kohana::$config->load('smarty')->security;;据我所知,这是 Smarty 版本更改的问题,KSmarty 无论如何都配置为FALSE。
【问题讨论】: