How to include helpers in smarty?
Id like to use static functions from helpers in a smarty template. 开发者_开发百科Im using ko3 and kohana-module-smarty - https://github.com/MrAnchovy/kohana-module-smarty/ so my question is how to autoload helper and use it in a template, ie:
app/class/url.php
class url {
function test () { return 'test'; } }views/index.tpl
{$url.test}
You should be able to pass Url
as a variable, $url
, and access it within your view with {$url->test()}
. I'm not sure if you would be able to access static functions like Url::test()
though.
If you're using a helper in the same views, you can create a new controller that binds the variable in the view:
<?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);
}
}
?>
Then extend it in your other controllers:
<?php
// application/classes/controller/welcome.php
class Controller_Welcome extends Controller_Site
{
public function action_index()
{
$this->template->content = 'Yada, yada, yada...';
}
}
And access it within your views:
{* application/views/my_template.tpl *}
<p>This is a {$url_helper->test()}.</p>
<p>{$content}</p>
精彩评论