Tuesday, April 17, 2012

How to develop a custom plugin in pyrocms


Today I am going to tell you how we can develop our own custom plug-in in pyrocms. In pyrocms whenever we edit some theme, or some view files, sometimes you will see some tags like {{ session:data name="foo" value="bar" }}. These are actually pyrocms plug-ins.

What is a plug-in?

In computing, a plug-in (or plug-in) is a set of software components that adds specific abilities to a larger software application.

In pyrocms there are two types of plug-in

  1. Standalone plug-in
  2. Modular plug-in

Standalone Plug-in:

Stand alone is a general plugin which is not necessarily dependent to other modules etc, like Google map, a simple twitter application etc. In pyrocms standalone plug-in are location in addons/plugins/.php

Modular plug-in:

Modular plug-in is actually a part of module which we already have developed. It actually adds more functionality in our existing module, that’s why it is located in the same module directory like addons/modules//plugin.php

How to develop this?

Create a file in addons/shared_addons/plugins/ directory with name example,php. And then put the following code in it and save.


1:  <?php defined('BASEPATH') OR exit('No direct script access allowed');  
2:  /**  
3:   * Example Plugin  
4:   *  
5:   * Quick plugin to demonstrate how things work  
6:   *  
7:   * @package          PyroCMS  
8:   * @author          PyroCMS Dev Team  
9:   * @copyright     Copyright (c) 2009 - 2010, PyroCMS  
10:   *  
11:   */  
12:  class Plugin_Example extends Plugin  
13:  {  
14:       /**  
15:        * Hello  
16:        *  
17:        * Usage:  
18:        * {{ example:hello }}  
19:        *  
20:        * @param     array  
21:        * @return     array  
22:        */  
23:       function hello()  
24:       {  
25:            $name = $this->attribute('name', 'World');  
26:            return 'Hello '.$name.'!';  
27:       }  
28:  }  
29:  /* End of file example.php */  

How to use this?

Using this plugin is very simple. just put {{ example:hello }} tag in you them or in any view file and save it. By refreshing your page you will see "Hello World!" as output.

Please feel free to post your comments or queries.

4 comments:

  1. Can you please explain how to use array (single and multidimensional) in templates. Plugin function may return array also.

    ReplyDelete
    Replies
    1. yea, we can handle arrays in pyrocms plugins. For this I wrote a post in my blog. You can read it by clicking on this url,

      http://our-knowledge-base.blogspot.com/2012/05/working-with-arrays-in-pyrocms-plugins.html

      Delete

Please feel free to post your comments. If anyone has a good article or good thing to share, just send me that with your name to asadmehmoodstar@gmail.com. and if anyone want so receive updates regarding my blog, he can subscribe to my weekly newsletter on "Subscribe to our mailing list" section.

Thanks