Component Writer's Guide :: Create your first component

From RadPHP XE2 Documentation
Jump to: navigation, search

Creating a component in RPCL is easy. You simply inherit from a base class and dump the web code (HTML, JS, etc) that makes up the component. Next, you set up the interface to allow the user to customize the component look and feel, and add any design-time interface necessary to make it work inside RadPHP.

Below is a simple component:

<?php
        //Includes
        require_once("rpcl/rpcl.inc.php");
        use_unit("controls.inc.php");
 
        //Class definition
        class MyFirstComponent extends Control
        {
            function __construct($aowner = null)
            {
                parent::__construct($aowner);
            }
 
            function dumpContents()
            {
                parent::dumpContents();
            }
        }
?>

This component inherits from Control and basically does nothing at this point. To add functionality, you need to write code in the dumpContents() method. For example,

            function dumpContents()
            {
                echo "<table width=\"100\">";
                echo "<tr>";
                echo "<td>Hello from RPCL</td>";
                echo "</tr>";
                echo "</table>";
            }

This code dumps a table component 100 pixels wide, with a single cell containing "Hello from RPCL". Below is code for testing this component:

        use_unit("forms.inc.php");
        $testpage=new Page();
 
        $testcomp=new MyFirstComponent($testpage);
        $testcomp->Parent=$testpage;
        $testcomp->Left=200;
        $testcomp->Top=200;
 
        $testpage->show();

This code create a page on which to place the component, and creates a new instance of the component. If you run this project, you will see an empty page with just the text "hello from rpcl" on the (200,200) coordinates.

To add a border and make it easier to see the project, change the code in the table tag as shown below:

echo "<table width=\"100\" border=\"2\">";

When you run again, the web page shows a 100 pixel table because of the border. Make additional modifications to the line as shown below:

echo "<table width=\"$this->Width\" height=\"$this->Height\" border=\"2\">";

Also, change this test code:

$testcomp->Width=400;
$testcomp->Height=400;

Now, running displays a 400x400 table with the text inside. In conclusion, note you can dump any web content to be part of your component.

Personal tools