Kohana模板导航,页眉或页脚

时间:2014-08-06 07:11:33

标签: php templates kohana

我正在尝试导航到我的模板。

控制器:

class Controller_Admin_Topnav extends Controller_Template {

    public $template = 'admin/template';

    public function action_index()
    {       
        $topnav = 566;
        $this->template->content = View::factory('admin/topnav')
            ->bind('topnav', $topnav);
    }

}

模板

    <?=View::factory('admin/topnav')?>
    <?= $content; ?>

查看

    <?=$topnav?>

错误: 如果我打电话给domain / admin / topnav,它的工作原理不是。我收到了这个错误。

ErrorException [ Notice ]: Undefined variable: topnav

我做错了什么?

日Thnx!

2 个答案:

答案 0 :(得分:1)

您需要做的就是将一些视图绑定到主模板的变量。

示例:

主要模板

<?php echo $navigation; ?>
<?php echo $content; ?>

导航模板

<p>This is the navigation</p>

内容模板

<p>This is the content of my website</p>

在控制器

$this->template = View::factory('mainTemplate')
       ->bind('navigation', View::factory('navigationTemplate'))
       ->bind('content', View::factory('contentTemplate'));

答案 1 :(得分:0)

如果你使用来自控制器的路径:domain / admin / topnav它可以工作,因为你在这里绑定$ topnav变量

$this->template->content = View::factory('admin/topnav')
        ->bind('topnav', $topnav);

当你在模板中渲染它时,$ topnav变量没有绑定,所以你的topnav.php视图在渲染时会抛出错误。

此处也绑定$ topnav变量,例如

<?=View::factory('admin/topnav')
    ->bind('topnav', $topnav)?>
<?= $content; ?>

当然先申报$ topnav。