Thursday, May 3, 2012

Working with arrays in pyrocms plugins

Hi guys, I am writing this post for one of my cool blog reader. He posted a comment that how we can return arrays from plugins to plugin view files and then how we can loop through and display the values of that array.

Pyrocms is very cool in handling this type of thing because it has a very strong template engine already built-in its codeignitor package. In order to explain how to use array in pyrocms plugin, i will take the same "example" plugin which I explained in this post http://our-knowledge-base.blogspot.com/2012/04/how-to-develop-custom-plugin-in-pyrocms.html.

Adding another function:

Open "example" plugin code and put the following function some where in that class.
 <?php  
 public function myposts()  
 {  
           $plugin_data = array();  
           $plugin_data[] = array(  
                                    'value'          => "Post # 1 value",  
                                    'title'          => 'Post # 1 title'  
                               );  
           $plugin_data[] = array(  
                                    'value'          => "Post # 2 value",  
                                    'title'          => 'Post # 2 title'  
                               );  
           $plugin_data[] = array(  
                                    'value'          => "Post # 3 value",  
                                    'title'          => 'Post # 3 title'  
                               );  
           return $plugin_data;  
      }  
 ?>  

so after adding this function whole plugin code will look like this,
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:  
30:
31:   public function myposts()  
32:   {  
33:         $plugin_data = array();  
34:         $plugin_data[] = array(  
35:                                    'value'          => "Post # 1 value",  
36:                                    'title'          => 'Post # 1 title'  
37:                               );  
38:         $plugin_data[] = array(  
39:                                    'value'          => "Post # 2 value",  
40:                                    'title'          => 'Post # 2 title'  
41:                               );  
42:         $plugin_data[] = array(  
43:                                    'value'          => "Post # 3 value",  
44:                                    'title'          => 'Post # 3 title'  
45:                               );  
46:          return $plugin_data;  
47:      }  
48:  }  
49:  /* End of file example.php */

How to use this?
Using this is very simple. You just have to paste following code in view file. It will show post titles in the form of a list.
 {{ example:myposts }}  
     <strong>{{ name }}:</strong> {{ value }}<br />  
 {{ /example:myposts }}  

Although this is a very simple explanation of how to handle arrays in pyrocms, but after ready and understanding this example, you can easily modify this or even can develop your own complex plugin.

Please feel to post comments on this.

Thanks

2 comments:

  1. Thanks Asad for something useful for beginners, but you have a mistake in the names you have used. The usage code should read:


    {{ example:myposts }}
    {{ value }}: {{ title }}
    {{ /example:myposts }}

    ReplyDelete

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