tags:

views:

661

answers:

2

This applies to Kohana 2.3.2

I've recently started making my Views more dynamic. Using the default template view as a base, now I am doing in the controller

$this->template->innerView = new View('article');

Then, in the middle of my template.php, I have

<?php echo $innerView; ?>

To echo the 'guts' of the article view between my header and footer. This works fine, except all the vars I defined to $this->template are unaccessible from the new view. I know I could probably do

$this->template->innerView->title = 'My Title';

But if there was a way to make child Views inherit their parent's variables, that would be great.

Is there?

A: 

I just found a way, but it uses the evil word 'global' :P

http://docs.kohanaphp.com/core/view#set_global

I needed this because I use the page title in the normal template (for within <title></title>) and also as the <h2></h2> of the page.

It's as simple as this

$this->template->innerView = new View('article');
$this->template->set_global('title', 'My Title');

I'm not sure how wide the scope is of the variable (until it bites me), that's why I'm hoping if someone has a better solution they'll post it :)

alex
+2  A: 

Hey Alex,

The set_global() method only sets the variable to be global across all views. It's not what you think when you hear "Global" in PHP so you got it right, this is exactly what you should use when you want to make a variable available across multiple views.

Thanks Michael!
alex