HTML::Element interface

malte on 2005-08-08T11:18:40

I have written a class with the following properties:

  • The new method creates an instance of HTML::Element and stores it away.
  • Method calls that can be handled by HTML::Element are delegated to this object, but they all return self.
  • All other method names are interpreted as html-attribute accessors.
  • Stringification is overloaded to as_HTML

That way you can write: HTML::Element::Whatever->new('input')->name("hallo")->value("welt")->size(10) and you get the HTML source for the appropriate input element.

This allows for some really concise generation of HTML.


Cool.

jk2addict on 2005-08-08T12:57:01

That's a great interface for $object to HTML generation. I kind of wish SQL::Translater had the same thing:
my $table = SQL::Translator::Schema::Table->new('mytable');
$table->column('id')->type('var char')->size('36')->default('foo');
*adds that to mental list of things to make...*

By the way..

jk2addict on 2005-08-08T13:02:24

What's it called, and when are you putting it on CPAN? :-)

Re:By the way..

malte on 2005-08-08T13:10:58

I will eventually. Its only in internal use now where we don't need it passing the whole HTML element test suite.

Do you have a name suggestion?

Re:By the way..

jk2addict on 2005-08-08T13:25:40

How about HTML::Element::Chained? I also thought of HTML::Element::Recursive. That sounds better, but my be misleading.

Re:

Aristotle on 2005-08-13T13:39:18

To be honest, I don’t think that’s particularly a good interface. Passing a hashref for attributes is both more obvious as well as more concise, syntax-wise.

I’m also never sure whether to like method chaining in general or not. It’s a kludge for Perl5; fortunately we won’t need it in Perl6.