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) {
    $html = ob_get_contents();
    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?


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(
        'some_func' => some_func()

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

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

  • 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.