开发者

How to include helpers in smarty?

开发者 https://www.devze.com 2023-01-27 17:06 出处:网络
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 au

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号