// assign the created viewmodel to a variable $vm = ViewModel::factory('Index'); // Set anything just on it $vm->variable = 'value';
If there are different ways of parsing the same View you can add multiple methods to the ViewModel other than the default view() method.
It's one extra level of seperation, where you seperate the view specific logic out of the controller and possibly the views and have them in ViewModels. If you don't like/need that, don't use it. If you don't get why this might be a good thing, you probably don't need it.drifitz drifitz wrote on 02/27/11 3:34 am:Hi I'm trying to figure out whether I should be using this so I have a question about ViewModels. What are the benefits of using a ViewModel to set data, rather than set it directly in the controller?
The methods in the ViewModel define which data gets passed to a view. Sometimes a single view can be used in multiple ways, in such a case you might have different methods for dealing with a view.drifitz drifitz wrote on 02/27/11 3:34 am:Also, in the user guide you say
If there are different ways of parsing the same View you can add multiple methods to the ViewModel other than the default view() method.
What do you mean by this? What other ways are there to parse a view?
It's one extra level of seperation, where you seperate the view specific logic out of the controller and possibly the views and have them in ViewModels.
$pdf = ViewModel::forge('welcome/pdfInvoice');
$pdf->invoice_id = 1;//in real life taken from input
$pdf->render();
public function view() {
// Load PDF library
Package::load('pdf');
Package::load('orm');
$invoice = Model_Invoice::find($this-->invoice_id);
$invoice_items = $invoice->invoiceitems;
$pdf = \Pdf\Pdf::factory('tcpdf')->init('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->add_page();
$pdf->Cell(60, 5, 'Summary: '.$invoice->summary, 0, 0, 'R');
$pdf->Ln();
//..eventually
$pdf->output();
}
It looks like you're new here. If you want to get involved, click one of these buttons!