Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Passing variables to a ViewModel
  • Hi, I am currently designing an application that is using Fuel where ViewModel's are being used as a layer between controllers and views to facilitate the use of different skins. I am however finding it difficult to find how you pass a variable between the controller and the ViewModel. I know when using normal views you just pass it as an argument of the factory method, however I've jumped in the code and ViewModel's don't seem to support this. I have a feeling the answer to this question is going to be very simple. Ian
  • It can be done like this in the controller:
    // assign the created viewmodel to a variable
    $vm = ViewModel::factory('Index');
    
    // Set anything just on it
    $vm->variable = 'value';
    

    Generally it's bad practice though, this should only be done when really necessary - otherwise you could just as well use just views. When you created before() and/or set_template() methods they are run before you add anything to it, so $variable won't be available for those. All the other methods are run after the controller is done, which means you can use $this->variable inside the view() and after() methods.
  • Hi, Thanks I thought it would be something really simple anyway, but if its bad practise then I might try out a few different ways and see what I come up with.
  • 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? 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?
  • 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?
    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:
    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?
    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.
    I would first try to grasp the basics of ViewModels though before you start with the more advanced usage.
  • Hey Jelmer Quick question.. What is a ViewModel? and also, why's it's name camel cased? Style guide
  • The ViewModel is explained in the User Guide.
    [edit]Also next time: read the topic before asking the question that was answered in the post you're actually replying to.[/edit]
    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.

    About the name: I didn't make it up and actually don't really like it, but it's part of a variation on the MVC pattern called the Model-View-ViewModel pattern.
    Also as the style guide states it is allowed in certain cases to use camelcasing in classnames, in this case to use an underscore would look like the View_Model is part of the View class - while in fact it is more the other way around: the ViewModel encapsulates a View.
  • ah cheers for that i did have a look in the documentation - must have skipped right past it :)
  • This is exactly what I wanted to do so how would it be done in best practice?

    My example is this:

    Using the pdf package I wanted to output a pdf. I dont want to write the pdf formatting code in the controller and in my view it should be in the view. So in the controller I have:
    $pdf = ViewModel::forge('welcome/pdfInvoice');
    $pdf->invoice_id = 1;//in real life taken from input
    $pdf->render();

    So that I can stick this in my View Model
    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();
    }

    Note that my acual /apps/view file is an empty file and also I want to create a set of methods in my ViewModel to rationalise the pdf formatting code.
  • Why would you want to "misuse" a viewmodel for this purpose? Why not create a standard class for it?
  • Thanks Harro - Your advice made me look for standard classes in the docs which I missed in the documentation. It works fine and I can put all my pdf code in directory classes/pdf.

Howdy, Stranger!

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

In this Discussion