From 84a6c667801d46c4cb6f831c9e40043b308f96d8 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sun, 5 Feb 2006 23:38:21 +0000 Subject: Added two examples showing creating new controls by composition. --- .../protected/pages/Controls/NewControl.page | 90 +++++++++++++++++++++- .../Controls/Samples/LabeledTextBox1/Home.page | 11 +++ .../Controls/Samples/LabeledTextBox1/Home.php | 13 ++++ .../Samples/LabeledTextBox1/LabeledTextBox.php | 18 +++++ .../Samples/LabeledTextBox1/LabeledTextBox.tpl | 2 + .../Controls/Samples/LabeledTextBox2/Home.page | 11 +++ .../Controls/Samples/LabeledTextBox2/Home.php | 13 ++++ .../Samples/LabeledTextBox2/LabeledTextBox.php | 32 ++++++++ 8 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.page create mode 100644 demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.php create mode 100644 demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.php create mode 100644 demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.tpl create mode 100644 demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.page create mode 100644 demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.php create mode 100644 demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/LabeledTextBox.php (limited to 'demos/quickstart/protected/pages/Controls') diff --git a/demos/quickstart/protected/pages/Controls/NewControl.page b/demos/quickstart/protected/pages/Controls/NewControl.page index f4ded5a4..8e2a21cf 100644 --- a/demos/quickstart/protected/pages/Controls/NewControl.page +++ b/demos/quickstart/protected/pages/Controls/NewControl.page @@ -1,15 +1,101 @@

Writing New Controls

+

+Writing new controls is often desired by advanced programmers, because they want to reuse the code that they write for dealing with complex presentation and user interactions. +

+

+In general, there are two ways of writing new controls: composition of existing controls and extending existing controls. They all require that the new control inherit from TControl or its child classes. +

Composition of Existing Controls

+

+Composition is the easiest way of creating new controls. It mainly involves instantiating existing controls, configuring them and making them the constituent components. The properties of the constituent components are exposed through subproperties. +

+

+One can compose a new control in two ways. One is to override the TControl::createChildControls() method. The other is to extend TTemplateControl (or its child classes) and write a control template. The latter is easier to use and can organize the layout constituent compoents more intuitively, while the former is more efficient because it does not require parsing of the template. +

+

+As an example, we show how to create a labeled textbox called LabeledTextBox using the above two approaches. A labeled textbox displays a label besides a textbox. We want reuse the PRADO provided TLabel and TTextBox to accomplish this task. +

-

+

Composition by Writing Templates

+

+We need two files: a control class file named LabeledTextBox.php and a control template file named LabeledTextBox.tpl. Both must reside under the same directory. +

+

+Like creating a PRADO page, we can easily write down the content in the control template file. +

+ +<com:TLabel ID="Label" ForControl="TextBox" /> +<com:TTextBox ID="TextBox" /> + +

+The above template specifies a TLabel control named Label and a TTextBox control named TextBox. We would to expose these two controls. This can be done by defining a property for each control in the LabeledTextBox class file. For example, we can define a Label property as follows, +

+ +class LabeledTextBox extends TTemplateControl { + public function getLabel() { + $this->ensureChildControls(); + return $this->getRegisteredObject('Label'); + } +} + +

+In the above, the method call to ensureChildControls() ensures that both the label and the textbox controls are created (from template) when the Label property is accessed. The TextBox property can be implemented similarly. +

+ + +

Composition by Overriding createChildControls()

+

+For a composite control as simple as LabeledTextBox, it is better to create it by extending TControl and overriding the createChildControls() method, because it does not use templates and thus saves template parsing time. +

+

+Complete code for LabeledTextBox is shown as follows, +

+ +class LabeledTextBox extends TControl { + private $_label; + private $_textbox; + protected function createChildControls() { + $this->_label=new TLabel; + $this->_label->setID('Label'); + // add the label as a child of LabeledTextBox + $this->getControls()->add($label); + $this->_textbox=new TTextBox; + $this->_textbox->setID('TextBox'); + // add the textbox as a child of LabeledTextBox + $this->getControls()->add($textbox); + } + public function getLabel() { + $this->ensureChildControls(); + return $this->_label; + } + public function getTextBox() { + $this->ensureChildControls(); + return $this->_textbox; + } +} + + + +

Using LabeledTextBox

+

+To use LabeledTextBox control, first we need to include the corresponding class file. Then in a page template, we can write lines like the following, +

+ +<com:LabeledTextBox ID="Input" Label.Text="Username" /> + +

+In the above, Label.Text is a subproperty of LabeledTextBox, which refers to the Text property of the Label property. For other details of using LabeledTextBox, see the above online examples. +

Extending Existing Controls

Extending TControl

Extending TWebControl

- +PostBackHandler +PostBackEvnetHandler +DataBoundControl
\ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.page b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.page new file mode 100644 index 00000000..ed05bfdf --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.page @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.php b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.php new file mode 100644 index 00000000..2b18972d --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/Home.php @@ -0,0 +1,13 @@ +Text=$this->Input->TextBox->Text; + } +} + +?> \ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.php b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.php new file mode 100644 index 00000000..999639d7 --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.php @@ -0,0 +1,18 @@ +ensureChildControls(); + return $this->getRegisteredObject('Label'); + } + + public function getTextBox() + { + $this->ensureChildControls(); + return $this->getRegisteredObject('TextBox'); + } +} + +?> \ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.tpl b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.tpl new file mode 100644 index 00000000..8b3c1406 --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox1/LabeledTextBox.tpl @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.page b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.page new file mode 100644 index 00000000..ed05bfdf --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.page @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.php b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.php new file mode 100644 index 00000000..2309bd88 --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/Home.php @@ -0,0 +1,13 @@ +Text=$this->Input->TextBox->Text; + } +} + +?> \ No newline at end of file diff --git a/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/LabeledTextBox.php b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/LabeledTextBox.php new file mode 100644 index 00000000..f4148148 --- /dev/null +++ b/demos/quickstart/protected/pages/Controls/Samples/LabeledTextBox2/LabeledTextBox.php @@ -0,0 +1,32 @@ +_label=new TLabel; + $this->_label->setID('Label'); + $this->getControls()->add($this->_label); + $this->getControls()->add(' '); + $this->_textbox=new TTextBox; + $this->_textbox->setID('TextBox'); + $this->getControls()->add($this->_textbox); + } + + public function getLabel() + { + $this->ensureChildControls(); + return $this->_label; + } + + public function getTextBox() + { + $this->ensureChildControls(); + return $this->_textbox; + } +} + +?> \ No newline at end of file -- cgit v1.2.3