0

I am trying to get a navigation into my template.

Controller:

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);
    }

}

Template

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

View

    <?=$topnav?>

Error: If i call domain/admin/topnav it works else not. I am getting this error.

ErrorException [ Notice ]: Undefined variable: topnav

What do i do wrong?

Thnx!

Bas
  • 137
  • 11
  • You have controller class as Controller_Admin_Topnav so it will work for 'domain/admin/topnav'. – TBI Aug 06 '14 at 07:14
  • Thats correct, only i want to include this in my template – Bas Aug 06 '14 at 07:17
  • This was the part of the solution. http://stackoverflow.com/questions/8158017/how-to-manage-multiple-templates-and-template-assets – Bas Aug 06 '14 at 08:07

2 Answers2

1

All you need to do is binding some Views to the variables of the main template.

Example:

Main Template

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

Navigation Template

<p>This is the navigation</p>

Content Template

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

In the Controller

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

If you use path from controller: domain/admin/topnav it works because you are binding here $topnav variable

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

When you are rendering it in your template, there is no binding of $topnav variable so your topnav.php view is throwing error when rendering.

Just bind also $topnav variable here, for ex.

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

Of course declare $topnav first.

Grzesiek
  • 442
  • 4
  • 15