HTML-Seamstress
view release on metacpan or search on metacpan
lib/HTML/Seamstress.pm view on Meta::CPAN
my ($tree, $c, $stash) = @_;
$tree->content_handler(head => 'Wally World Products);
$tree->content_handler(body => html::productpage::body->new->guts)
}
We have solved our problem. However, we can create even more re-use
because the both of these classes are very similar. They only vary in
2 things: the particular head and body they provide.
You can abstract this with whatever methodmaker you like. I tend to
prefer prototype-based oop
over class-based, so with L<Class::Prototyped|Class::Prototyped>,
here's how we might do it:
package html::abstract::common;
use base qw(HTML::Seamstress Class::Prototyped);
sub head { 'ABSTRACT BASE METHOD' }
sub body { 'ABSTRACT BASE METHOD' }
__PACKAGE__->reflect->addSlots(
html_file => 'html/base.html',
);
sub new {
my $self = shift;
my $tree = $self->new_from_file($self->html_file);
}
sub process {
my ($tree, $c, $stash) = @_;
$tree->content_handler(head => $tree->head);
$tree->content_handler(body => $tree->body);
}
1;
and then have both of the above classes instantiate and
specialize this common class accordingly.
[ Again: you can run the two Seamstress approaches. They are in
F<$DISTRO/samples/perpage> ]
=head3 Parallel generation of a single page natural
A tree of HTML usually contains subtrees with no
inter-dependance. They therefore can be manipulated in parallel. If a
page contains 5 areas each of which takes C<N> time, then one could
realize an N-fold speedup.
=head2 Reap the benefits of using HTML::Tree
=head3 Pragmatic HTML instead of strict X(HT)ML
The real world is unfortunately more about getting HTML to work with
IE and maybe 1 or 2 other browsers. Strict XHTML may not be acceptable
under time and corporate pressures to get things to work with quirky
browsers.
=head3 Rich API and User Contributions
L<HTML::Tree> has a nice large set of accessor/modifier functions. If
that is not enough, then take a gander at Matthew Sisk's
contributions: L<http://search.cpan.org/~msisk/> as well as
L<HTML::Element::Library>.
=head1 Seamstress contains no voodoo elements whatsoever
If you know object-oriented Perl and know how to rewrite trees, then
everything that Seamstress offers will make sense: it's just various
boilerplates and scripts that allow your mainline code to be very
succinct: think of it as Class::DBI for HTML::Tree.
=over
=item * unifying HTML and the HTML processing via a Perl class
Seamstress contains two scripts, F<spkg.pl> and F<sbase.pl> which
together make it easy to access and modify an HTML file in very few
lines of startup code. If you have a file named
F<html/hello_world.html>, Seamstress makes it easy for that to become
the Perl module C<html::hello_world> with a C<new()> method that
loads and parses the HTML into an L<HTML::Tree|HTML::Tree>.
=item * a Catalyst View class with meat-skeleton processing
The meat-skeleton HTML production concept is discussed below.
L<Catalyst::Seamstress::View|Catalyst::Seamstress::View> is all ready
to go for rendering simple or more complex pages.
=item * Loading in the HTML::Tree support classes
One a Perl class has been built for your HTML, it has
L<HTML::Element|HTML::Element> and
L<HTML::Element::Library|HTML::Element::Library> as superclasses, ready
for you to use to rewrite the tree.
=back
=head2 Seamstress is here to help you use HTML::Tree, that's all.
=head2 Unify HTML and the processing of the HTML via a Perl class
Let's see why this is a good idea. In Mason, your Perl and HTML are
right there together in the same file.
Same with Template. Now, since Seamstress
operates on the HTML without touching the HTML, the operations and
the HTML are not in the same file. So we create a Perl module to
glue the HTML file to the operations we plan to perform on it.
This module (auto-created by F<spkg.pl> and perhaps F<sbase.pl>)
has a constructor C<new()>, which grabs the HTML file and
constructs an L<HTML::Element|HTML::Element> tree from it and
returns it to you.
( run in 0.970 second using v1.01-cache-2.11-cpan-39bf76dae61 )