0

スマートなテンプレートでヘルパーの静的関数を使用したい。ko3とkohana-module-smartyを使用しています-https ://github.com/MrAnchovy/kohana-module-smarty/なので、私の質問は、ヘルパーを自動ロードしてテンプレートで使用する方法です。

app / class / url.php


class url {
function test(){
return'test';
}
}


views / index.tpl


{$ url.test}

4

1 に答える 1

0

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>
于 2010-12-02T19:33:51.393 に答える