Articles


Developing our first plugin, part 2

Difficulty: Easy

Adding more functions and accessing data with our first plugin

In this example we’ll build on our first plugin and introduce a couple of new concepts.

Plugins with multiple functions

A single plugin file can encapsulate any number of related functions. To add more than one function we add an extra segment to the tag. For example, our {exp:hello_world} tag could become {exp:hello_world:display}. The 3rd segment should describe the tag’s function.

Adding the new tag to the plugin is as simple as adding a new method to the plugin’s class that corresponds to the tag’s 3rd segment.

public function display() {
    
return "<p>Hello, world</p>";

Note, that we have no longer use the $return_data variable. When using the display method we can simply return the value that we want to display rather than assigning it to the $return_data variable (the $return_data variable is only used because PHP constructors cannot return a value).

We could add more functions to our plugin by adding new methods to our plugin’s class.

Getting data into the plugin

There are 2 ways to provide the plugin with data.

Parameters

Parameters allow you to pass small bits of information to the plugin and are usually used to configure how the plugin works, eg:

{exp:hello_world:display name="Fred"

Here’s how we access this data:

public function display() {
    $name 
$this->EE->TMPL->fetch_param("name""world");
    return 
"<p>Hello, " $name "</p>";

We’re using the EE super-object for the first time here. This object gives us access to lots of built-in CodeIgniter and ExpressionEngine functionality, like database access and template parsing. We’ll be using the Template class here.

The fetch_param method takes the tag parameter name as the first parameter, and a second (optional) default value for when the tag parameter is not set. In this case, if the name= parameter is missing from the template tag the default value of world will be used.

Tag data

Tag data is made available by using tags pairs in a template, eg:

{exp:hello_world:display}Bob{/exp:hello_world:display} 

When a tag pair is used, the contents between the opening and closing tags can be used by the plugin. This data is also accessed using the Template class:

public function display() {
    $tagdata 
$this->EE->TMPL->tagdata;
    return 
"<p>Hello, " $tagdata "</p>";

In this case, we get the data using the $this->EE->TMPL->tagdata property.

Summary

We’ve quickly seen how to make a plugin with a number of different functions, and we’ve fetched data from the template to use within the plugin.

We’ve also used the EE super-object for the first time and accessed the Template class to easily data into our add-on.

 

Comments

In case this trips anyone else up, the Tag data example should be as follows;

{exp:hello_world:display}Bob{/exp:hello_world:display}

Posted by Neil on November 18, 2012

Thanks Neil - fixed now…

Posted by Andrew on November 20, 2012

Can we have some more tuts pls :D?

Posted by Klauss on June 2, 2014

Add a comment

(Your email address will not be displayed on the site, but will be used for your gravatar)

Notify me of follow-up comments?