My issue is that Kohana only renders the view. When I
View::factory('/foo/bar')
in Controller_Other, it doesn't hit Controller_Foo first. I want it to hit the controller, then render the view.
class Controller_Other extends Controller_Template {
public $template = 'main';
public function action_index() {
$this->template->body = View::factory('/foo/bar');
}
}
How would I get it to run through the controller first:
class Controller_Foo extends Controller_Template {
public function action_bar() {
$this->myVar = 'foo';
}
}
so that in the view, $myVar
is always set in views/foo/bar.php
when I call it from some other View::factory()
?
Edit:
there must be a cleaner way than forcing action_bar
to render its own view to string then going:
$foo = new Controller_Foo($this->request, $this->response);
$this->template->body = $foo->action_bar();
I'm not sure what you're doing - want to bind global view variable or do an internal request. Anyway, here are the examples for both cases:
bind global view variable
class Controller_Other extends Controller_Template {
public $template = 'main';
public function action_index() {
View::bind_global('myVar', 'foo');
//binds value by reference, this value will be available in all views. Also you can use View::set_global();
$this->template->body = View::factory('/foo/bar');
}
}
do an internal request
this is 'foo/bar' action
class Controller_Foo extends Controller_Template {
public function action_bar() {
$myVar = 'foo';
$this->template->body = View::factory('/foo/bar', array('myVar' => $myVar);
}
}
class Controller_Other extends Controller_Template {
public $template = 'main';
public function action_index() {
$this->template->body = Request::factory('foo/bar')->execute()->body();
//by doing this you have called 'foo/bar' action and put all its output to curent requests template body
}
}
You should always pass $myVar variable to view before render it like
public function action_index() {
$this->template->body = View::factory('/foo/bar')->set('myVar', 'foo');
}
In other controller you should set it again because View is just a temlate. If you plan to use the same view in different places of script you can assign View instance to some variable and use it somewhere like:
public function action_index() {
$this->view = View::factory('/foo/bar')->set('myVar', 'foo');
$this->template->body = $this->view ;
}
public function action_bar() {
$this->template->head = $this->view ;
}