|
Posted by Henk verhoeven on 05/03/07 20:32
Areric schreef:
> Hey all,
>
> ASP.NET has a concept of a user defined web control, where a person
> can implement their own web control and override the render method to
> write out html for that control.
>
> I was wondering, in general, do people ever follow a similar pattern
> when writing PHP4+ code? By which i mean, i could write a WebControl
> base class and a TextBoxControl that inherits from that, then in my
> html markup instead of writing out the input tag by hand i could call
> textBox->render().
Of course! I call them widgets, here is there class hierarchy:
http://www.phppeanuts.org/site/index_php/HcodePackage/pnt.web.widgets
Some special widgets are:
- DialogWidget: holds a reference to an object. It lets the user open a
dialog to search and select an object to reference. To try it out go to:
http://examples.phppeanuts.org/example4/index.php?pntType=Employee&id=2
Then click in the widget behind 'country'. After you have selected a
different coutry you will see the widget shows the name of the country
you just selected. If you then press the 'Update' button the form will
be submitted, all it values validated and stored, including the
reference to the county (visitors are not allowed to do this, but if you
enter invalid values the validation will produce error messages)
- MtoNDialogWidget holds references to multiple objects. It lets the
user open an MtoNDialog where the user can add and remove references.
After the dialog is closed the state of the widget reflects the choices
the user made. If you then press the 'Update' button the form will be
submitted, causing the relation in the database to be updated. To try it
out go:
http://examples.phppeanuts.org/example13/index.php?pntType=Activity&id=5
and click in the widget behind 'keywords'.
Of course the dialogs hemselves as well as all other pages are built
following the same render pattern, only these are composite
componenents, the composition pattern is explained here:
http://www.phppeanuts.org/site/index_php/Pagina/195
>
> Up to this point i have simply hard coded all the html myself and then
> inserted echos into the appropriate places. However, im wondering if
> this might be a better approach then having such "messy" html.
>
> Any thoughts? Opinions?
>
It is really worthwile following these patterns, your code becomes much
more readable and you can easily reuse reoccuring components. You do not
need a framework to do so, but if you take time to learn how to use the
framework it may significantly boost your productivity.
Greetings,
Henk Verhoeven,
www.phpPeanuts.org.
[Back to original message]
|