rje has asked for the wisdom of the Perl Monks concerning the following question:
The structure above is actually the output of a process I've written that merges in regional content to default content (using Hash::Merge to great effect), which is the main purpose of generating HTML in the first place -- to route the employee automatically to regional content, rather than forcing her to navigate her way between multiple default and regional content pages.--- Benefits: Desc: your benefits Subcategories: Employee Recognition: Url: http://blah.com/recognition/index.html topics: Employee Service Awards: Url: http://blah.com/recognition/employee_service.html General Recognition and Performance-based Awards: Url: http://blah.com/recognition/awards.html Equity Plans: Url: Overridden Regional Content URL! topics: Incentive Plan: Url: http://blah.com/equity_plans/incentive/override.html Url: http://blah.com/index.html Compensation: Subcategories: {} Url: http://blah.com/comp/index.html Corporate Policies: Subcategories: {} Url: http://blah.com/corp_policies/index.html Early Retirement: Subcategories: {} Url: http://blah.com/ero/index.html
sub createPages { my $filename = shift; my $content = shift; my $desc = $content->{Desc}; my $url = $content->{Url}; my $subcontent = $content->{Regions} || $content->{Categories} || $content->{Subcategories} || $content->{Topics}; open OUT, ">$filename.html"; foreach my $key ( sort keys %$subcontent ) { print OUT buildSubject( $filename, $key, $subcontent->{$key} ); } close OUT; } sub buildSubject { my $filename = shift; my $name = shift; my $content = shift; my $desc = $content->{Desc}; my $url = $content->{Url}; my $subcontent = $content->{Regions} || $content->{Categories} || $content->{Subcategories} || $content->{Topics}; my @out = (); foreach my $key ( sort keys %$subcontent ) { my $url = $subcontent->{$key}->{Url}; push @out, "<a href='$url'>$key</a>\n" if $url; push @out, $key unless $url; } my $header = "<a href='$filename.$name.html'>$name</a>"; $header = "<a href='$url'>$name</a>" if $subcontent; print $subcontent; return "<h2>$header</h2>\n<h3>$desc</h3>\n", join ' | ', @out; }
<html> <body> # INTERPOLATE => 1 [% FOREACH name = Categories.keys %] <h2><a href="[% Categories.$name.Url %]">$name</a></h2> <h3>[% Categories.$name.Desc %]</h3> [% FOREACH subname = Categories.$name.Subcategories.keys %] <a href="[% Categories.$name.$subname.Url %]">$subname</a> [% END %] [% END %] </body> </html>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Generating HTML from a Hashref of Hashrefs
by holli (Abbot) on Oct 07, 2005 at 18:20 UTC | |
by rje (Deacon) on Oct 07, 2005 at 20:54 UTC | |
|
Re: Generating HTML from a Hashref of Hashrefs
by InfiniteSilence (Curate) on Oct 07, 2005 at 21:45 UTC | |
by rje (Deacon) on Oct 10, 2005 at 01:50 UTC |