Creating new Prado components by template composition
Wednesday, January 25th, 2006Update (28/4/2006): You must extend TCompositeControl abstract class rather than TTemplateControl.
One of the easiest method of creating a new custom component is by template composition. For example, suppose we want to create a labelled textbox component, that is, a textbox component with a label attached. Let us call this new component LabeledTextBox.
First, create the template for the new proposed component and save the file as LabeledTextBox.tpl1).
<com:TLabel ID="label" ForControl="textbox" Text="label:" /> <com:TTextBox ID="textbox" />
Second, we eed a PHP class to expose the textbox and label properties. Component with templates must extend the TCompositeControl class. Save the PHP file as LabeledTextBox.php.
<?php class LabeledTextBox extends TCompositeControl { /** * @return TTextBox textbox instance */ public function getTextBox() { return $this->textbox; } /** * @return TLabel textbox label */ public function getLabel() { return $this->label; } } ?>
That completes the custom component definition. If you have saved the about code to a certain directory, you must include them in the namespace before using them.
Finally, the usage of this new component is very simple, just use them like all other components.
<com:LabeledTextBox Label.Text="username:" /><br /> <com:LabeledTextBox Label.Text="password:" />
Notice that Label.Text is a sub-property, where the Label property belongs to LabeledTextBox and corresponds to the getLabel() function. The sub-property (or property of another property) Text is a property of TLabel component.
The addition of sub-properties in Prado v3 makes component template composition really simple and effective. So, try it and create some new custom component by grouping together your oftened used components through template composition.
.tpl file extension by default.