Page 1 of 1

how to get stuff from controller to view...

Posted: Wed Feb 20, 2008 7:25 am
by kabo
Hi!

I've made a component and in the controller done:

Code: Select all

$model =& $this->getModel();
$sector = "User-input-here";
$model->setSector($sector);
But when I do this in my view:

Code: Select all

$model =& $this->getModel();
echo $model->getSector();
I get nothing! Am I missing something here?
Is this the correct way to handle userinput?
Thanks for any help!

Re: how to get stuff from controller to view...

Posted: Wed Feb 20, 2008 9:02 am
by kabo
OK, I've solved it by doing it this way instead.
In the controller:

Code: Select all

$sector = "User-input-here";
$document =& JFactory::getDocument();
$viewType = $document->getType();
$view =& $this->getView(null, $viewType);
$view->setSector($sector);
And then in the view:

Code: Select all

$model =& $this->getModel();
$model->setSector($this->sector);
$model->doStuffThatRequiresASector()
It works but seems like weird way of doing it... It would be better if I could just manipulate the model directly from my controller. :/

Re: how to get stuff from controller to view...

Posted: Wed Feb 20, 2008 2:52 pm
by kabo
AHA!
Figured out a better way to do it :)

Controller:

Code: Select all

function display() {
$model =& $this->getModel();
$document =& JFactory::getDocument();
$viewType = $document->getType();
$view =& $this->getView(null, $viewType);
$model->setSector("User-input");
$view->setModel($model);
$view->display();
}
View:

Code: Select all

function display($tpl = null) {
$foo = $this->model->DoStuffThatRequiresASector();
$this->assignRef('foo', $foo );
parent::display($tpl);
}
function setModel(&$model) {
$this->model =& $model;
}
Hope this post can help somebody else and help them figure it out faster than I did...

Re: how to get stuff from controller to view...

Posted: Thu Feb 21, 2008 12:09 am
by Rogue4ngel
Thanks for posting your findings! It may come in handy for someone else.