Please, someone stop me if this has already been done elsewhere...
Cue in WWW::Widget, probably the most trivial API ever designed. Written as a Moose Role, it requires from wannabe-widget classes only two things: that they pass all configuration elements at object-creation time, and implement a
And that's pretty much it. The role auto-wraps the HTML generated by
as_html() in a <div> with two classes, WWW-Widget and WWW-Widget-ThisWidgetClass so that the display can be controlled via CSS. And, for the laziness-inclined, the role also overloads the object's stringification to be an alias to
Bottom-line, in Galuga (which uses Catalyst and Mason), I can now take care of my widgets by having the following in my configuration:
<widgets> <PerlIronMan> id yanick </PerlIronMan> <Twitter> username yenzie </Twitter> </widgets>
and put this stanza in my template:
% while ( my ( $widget, $conf ) = each %&#123; $c->config->&#123;widgets} } ) &#123; % my $package = 'WWW::Widget::'.$widget; % eval "use $package; 1" or next; % $conf ||= &#123;}; # in case there's no configuration item <% $package->new( %$conf ) %> % }
The module is not yet available on CPAN, but should be soon (as soon as I distributionify the code, slap a little bit of documentation on it, and make sure I'm not reinventing an already existing wheel).