我想在智能模板中使用来自助手的静态函数。我正在使用ko3和kohana模块-智能- https://github.com/MrAnchovy/kohana-module-smarty/,所以我的问题是如何自动加载助手并在模板中使用它:
app/class/url.php
类url {
function test () { return 'test';}}
视图/index.tpl
{$url.test}
发布于 2010-12-02 19:33:51
您应该能够将Url作为变量传递给$url,并在您的视图中使用{$url->test()}访问它。不过,我不确定您是否能够访问像Url::test()这样的静态函数。
如果在相同视图中使用助手,则可以创建一个新控制器,该控制器绑定视图中的变量:
<?php
// application/classes/controller/site.php
class Controller_Site extends Controller_Template
{
public $template = 'smarty:my_template';
public function before()
{
$this->template->set_global('url_helper', new Url);
}
}
?>然后在其他控制器中扩展它:
<?php
// application/classes/controller/welcome.php
class Controller_Welcome extends Controller_Site
{
public function action_index()
{
$this->template->content = 'Yada, yada, yada...';
}
}并在您的意见中访问它:
{* application/views/my_template.tpl *}
<p>This is a {$url_helper->test()}.</p>
<p>{$content}</p>https://stackoverflow.com/questions/4328961
复制相似问题