Make a splash with any EE add-on: How to render a view

Clinton Reeves / Posted 7.12.2011

Make a splash with any EE add-on: How to render a view

Recently, I was tasked with developing a simple custom plug-in for ExpressionEngine. Since my passion is in back end development, and I am still quite new to the CodeIgniter framework, I was excited to dive into this new system.

While developing the plug-in, I ran into a problem that was initially quite frustrating (though it did get better once I understood what I was doing).

If you are developing add-ons for ExpressionEngine (or MojoMotor), you are most likely going to run into a situation where you need to render a view. If you haven't run into this problem yet, I can assure you that it is only a matter of time before you do. In the two, soon to be three, add-ons I have developed, I have had to do it every time.

Fortunately, there are several great tutorials out there that will give you the coding you need to get back on track. But, aside from just copying and pasting code you found on a website, it really is important to understand what the code actually does. Because really, have you ever tried to debug an application when you don't understand it?

The following is the code and the know-how you will need to render a view when developing an add-on using CodeIgniter. Don’t just get your feet wet by copy and pasting: dive in and drink deep of the knowledge.

In order to render a view for your add-on, you will need to add the following code:

function Plugin_name()
		//This is going to create the reference 
		//to the EE super object 

		$this->EE =& get_instance();
function show_the_view()
		return $this->view_loader('views/index')

//code to load the view file

public function view_loader($viewPath, array $inputArray = 
NULL, $returnValue = FALSE)

		$orig_view_path = $this->EE->load->_ci_view_path;
		$this->EE->load->_ci_view_path = PATH_THIRD.

		$returnView = $this->EE->load->view($viewPath, 
		'', $return);
		$this->EE->load->_ci_view_path = $orig_view_path;

		return $returnView;

The first function above (plugin_name()) creates the reference to the ExpressionEngine super object. This is very common in add-ons. 

The second function (show_the_view()) is a simple call to the third function, which will be doing our heavy lifting. With this function call, you will notice that we are passing the value views/index. This is the path to your view file, which resides inside of your add-on.

The third function (view_loader()) is where the view rendering action takes place. Right away, you will notice that there are three input parameters that this function accepts:

1. $viewPath

$viewPath is the path to your view from inside of your add-on directory. 

For example, if your add-on was stored in:


The path to your view, from inside of your add-on directory would be: views/index.

2. $inputArray

$inputArray is used if you would like to pass an array of data to the view() function (see below). This data can later be used in the view. Since this is a simple example, I have set the default value equal to “NULL,” and it won't be used. 

3. $returnValue

$returnValue is a boolean type and, as such, can either be true or false. A value of true will tell the view() function to return the data, and a value of false will tell the view() function to just load it.  In our case, we don’t need to return data, so I have the default value set to FALSE. 

Now that we know that the input parameters are and what they do, lets walk through this function line by line.

The first line of code you see in view_loader() is:

$orig_view_path = 

What we are doing here is creating, in essence, a copy of the current or “original” view path as created by ExpressionEngine. So, we store the current (or unmodified) view path in a variable. This variable will be used once all of our code has been completed.

The next step is to change the view path to whatever we want it to be. In this case, since we are developing a third_party add-on we are going to set the view path as follows:


PATH_THIRD is a reserved word which, when invoked, will return the path to the third_party directory (where the add-on should be stored). Once we have the base directory, we want to append the name of our add-on to it so we are pointing to the appropriate file within the third_party directory. 

Note:  Please be sure to change plugin_name to the name of your plugin. Also, be sure to include the "/ "at the end of the name of your plug-in.

So, at this point, we have stored the original view path location in a variable for later use, and we have officially changed the view path to the directory of our choice.

Next, we are going to load the view. We do this with the following code:

$returnView = $this->EE->load->view($viewPath,
'', $return);

What that code there says is this: Invoke the view element (view() function), of the load class, within the EE super object, and pass these variables to it at the same time.

Look at it another way, this is going to call the view() function, which resides in Loader.php, which can be found in the system/codeigniter/core/ directory.

The results returned by the view() function will then be saved in a variable ($returnView) which we will use momentarily.

At this point, the primary processing of our code is complete. We just need to clean things up a little bit and then we can return to the $returnView value.

Now, let's clean things up. We do that with the following code:

$this->EE->load->_ci_view_path = $orig_view_path;

This sets the view path back to the original view path. 

And once that's done, we send the $returnView value back to the invoking function and that's it! 

Once you understand the how and the why, you’ll be able to make a big splash every time you create an add-on in CodeIgniter.

Clinton Reeves

Clinton Reeves

Back-end Developer

Clinton is a back-end developer at Q Digital Studio who lives and breathes code. He is Q Digital's resident problem-solver; if he can't fix it, he'll just create new code or a shiny new add-on. When he's not problem-solving at the studio, he's likely under the hood of a car or remodeling his house.