3

I am implementing a custom field type which has quite a complex display_field function. I would like to separate the view logic and at the moment I have done the following:

function display_field($data) {
    ob_start();
    include('view/designer.php');
    $html = ob_get_contents();
    ob_end_clean();
    return $html;
}

The above works, but given that ExpressionEngine is built on CodeIgnitor which has a solid MVC architecture, what is the proper way to display a view?

6

Yeah, that's the wrong way to go about it. You want to use the load class and use the view() method. You can even pass variables to the view that can be used programmatically.

public function display_field($data)
{
    $vars = array(
        'data'     => $data,
        'some_var' => 'some value',
        'some_array' => array(
            '1',
            '2',
            '3'
        ),
        'some_func' => some_func()
    );

    return $this->EE->load->view('designer', $vars, TRUE);
}

Reference http://ellislab.com/codeigniter/user-guide/libraries/loader.html

| improve this answer | |
  • Awesome thanks! I tried using $this->load->view but to no avail. This is a much cleaner approach to using the PHP buffer mechanism. – Lea Hayes Dec 16 '12 at 20:39
  • This code definitely works. I use the same logic on all of my add-ons. – Justin Kimbrell Dec 16 '12 at 20:40
  • Yeah this certainly works! When I tried before I was just missing the $this->EE-> part. Can I access the lang from within my view? – Lea Hayes Dec 16 '12 at 20:44
  • 2
    $this->EE->lang->loadfile('your_module_name'); – Justin Kimbrell Dec 16 '12 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.