How to write a plugin

From bemoko developer wiki

Jump to: navigation, search

Plugins allow you to interact with content and data services from within the bemokoLive platform. These plugins can then be hooked into the page flow and accessed from within the templates (or other plugins).

These steps are covered in more detail with a full walk through in the Tutorials. It's a good idea to take a look at these if you want to know more about developing sites with bemoko and how to use plugins, but the information below gives you a high-level introduction.

Step 1 : Create A Plugin

In your site directory create a sub-directory called plugins and in this directory create a file called MyPlugin.groovy as follows:

class MyPlugin {  
  def getMessage() {
    return "Hello"
  }
}

Step 2 : Register the plugin in the site configuration

In your site-config.xml file which is located at the root of your site, register the plugin:

<?xml version="1.0" encoding="UTF-8"?>
<site>
  <content-sources>
    <source name="myplugin" plugin="MyPlugin"/>      
  </content-sources>
</site>

This configuration makes the plugin you created available under the name myplugin.

Step 3 : Access your plugin from a template

In your template, e.g. HTML fragment, access the plugin content with ${content.myplugin.message}

<div>${content.myplugin.message}</div>

Why is it ${content.myplugin.message}?

  1. ${...} tells the template to render the value inside the brackets and include the output on the page
  2. content.myplugin says use the plugin named myplugin as defined in the site-config.xml above.
  3. content.myplugin.message says go to the property message, going via the getter method getMessage() if it is available

Next Steps

You can also write your plugin in Java if you prefer, e.g. create the file MyPlugin.java instead

public class MyPlugin {  
  public String getMessage() {
    return "Hello";
  }
}

Whether the plugin is written in Java or Groovy you have full access to the Java libraries that you normally use. For example the second tutorial shows how you can access a remote feed. You can find more details on plugins here including the following:

  • plugin life-cycle - init, execute and destroy
  • plugin scope - request, site or session

From within plugins you also have access to

  • Logging - control logging from your plugin to help with troubleshooting and auditing
  • Custom Configuration - drive you plugin with configuration
  • bemoko API - access details about the user, device, intent and site