我想在 smarty 模板中使用来自助手的静态函数。我正在使用 ko3 和 kohana-module-smarty - https://github.com/MrAnchovy/kohana-module-smarty/所以我的问题是如何自动加载帮助程序并在模板中使用它,即:
应用程序/类/url.php
类 url {
function test () {
return 'test';
}
}
意见/index.tpl
{$url.test}
我想在 smarty 模板中使用来自助手的静态函数。我正在使用 ko3 和 kohana-module-smarty - https://github.com/MrAnchovy/kohana-module-smarty/所以我的问题是如何自动加载帮助程序并在模板中使用它,即:
应用程序/类/url.php
类 url {
function test () {
return 'test';
}
}
意见/index.tpl
{$url.test}
您应该能够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>