Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Controller_Template and ViewModel together?
  • Hi all,
    I'm playing around with Controller_Template and ViewModel. Is it possible to get a Controller_Template to work with ViewModel, because I am unable to do so.
    Admittedly, I'm fumbling around:
    public function action_index()
     {
      $data['messages'] = Model_Message::find('all');
      $this->template->title = "Messages";
      $this->template->content = ViewModel::forge('message/index', $data);
     }
    
    This returns an error:
    ErrorException [ Error ]: Method name must be a string
    
    COREPATH/classes/viewmodel.php @ line 240
    

    I know this is utterly wrong, because ViewModel does not receive that second param $data like that. But I'm lost.
    Any help would be greatly appreciated!
    Thank you.
  • Use
    $this->template->content = ViewModel::forge('message/index')->set($data);
    
    You can use the same methods on a Viewmodel as on a view.
  • Thank you Harro,
    Unfortunately, whilst that does correctly pass the variable it returns an error from set():
    Warning!
    
    ErrorException [ Warning ]: Missing argument 2 for Fuel\Core\ViewModel::set(), called in C:\wamp\www\fuelphp-v1\fuel\app\classes\controller\message.php on line 9 and defined
    
    COREPATH/classes/viewmodel.php @ line 171:
    
    170: */
    171: public function set($key, $value, $filter = null)
    172: {
    Notice!
    
    ErrorException [ Notice ]: Undefined variable: value
    
    COREPATH/classes/viewmodel.php @ line 174:
    
    173: is_null($filter) and $filter = $this->_auto_filter;
    174: $this->_view->set($key, $value, $filter);
    175: 
    

    I am also unable to set the $title of the template from the ViewModel.
    Even though there is no error when using $this->title = "Test";
  • Ah, yeah, set() requires a key-value pair. Alternative:
    $this->template->content = ViewModel::forge('message/index');
    $this->template->content->set('messages', Model_Message::find('all'));
    $this->template->content->set('title', "Messages");
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion