[Perl-widget-developer] Widget and templates - QED - stringify overloading
Status: Alpha
Brought to you by:
spadkins
From: <ja...@la...> - 2001-06-08 17:10:57
|
There were a couple of good points about my proposed how to use widgets in Templating systems. I know the thought of changing a system like Template Toolkit is unattractive. In the long run I feel a few strategic changes could make all lives easier. HOWEVER - GET THIS: I thought to myself if we do something like: $widget=[ a widget instance ]; print $widget; We get a lovely output like: Widget::HTML::Thinggy=HASH(0x80903040) Using the overload (perldoc overload) package we can simply overload the stringify operator to whatever we wish: Pacakge Widget::HTML::Thinggy; sub render_html { } use overload '""' => \&render_html; # or *whatever* Now print $widget will actually call render_html to create what to print. Delicious. I can rip out the funky code I whacked into Template Toolkit and still just place my widget where I want, calling it by name and seeing its default rendering (HTML) in all its glory. We should look at putting this into the Widget::HTML::Base.pm package. I see that right we're waffling with what to call - render, print, display, whatever... package Widget::HTML::Base; # Default rendering - shows the bad news sub render { my $self=shift; return "no html rendering for this widget."; } use overload '""' => \&render; # Does this inherit properly? Maybe not. Might need a bit of fancy footwork for subclasses to Widget::HTML::Base 1; |