Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Building html site maps

by dimar (Curate)
on Jan 30, 2005 at 17:33 UTC ( [id://426399]=note: print w/replies, xml ) Need Help??


in reply to Building html site maps

... and in addition to Template::Toolkit and the other fine and dandy modules out there ... you might consider the fine and dandy language known as "perl" (aka "Perl", aka "PERL"). (Who woulda imagined it!)

Straight out of the box, you can write perl code that "cleans up" all that code you have inherited, and encourages all that great stylesheet conformity, and all that great MVC separation, and whips up a hot batch of any other buzzword-soup-of-the-day you may want to serve up.

Here is an example that is all plain perl code (with one added subroutine) that keeps our HTML template looking more like HTML than like perl (instead of the other way around).

### begin_: init perl use strict; use warnings; ### begin_: init vars my $sOut; my $oAlph = [('A' .. 'Z')]; my $oSubj = {}; $oSubj->{A} = [qw(africa asia)]; $oSubj->{B} = [qw(belgium brazil )]; my $oArti = {}; $oArti->{africa} = [qw(afone aftwo afthree )]; $oArti->{asia} = [qw(asone astwo )]; $oArti->{belgium} = [qw(beone)]; $oArti->{brazil} = [qw(brone)]; ### begin_: OutputTemplate ### ------------------ ### ------------------ $sOut .= " <html> <head></head> <body> " .sLoop($oAlph,sub{ "<a href='#'>$_</a> || " }) ."<hr /> " .sLoop([keys %{$oSubj}],sub{my $sLett=$_; "<h2>$sLett</h2> " .sLoop($oSubj->{$sLett},sub{my $sName=$_; "<h3>$sName</h3> " .sLoop($oArti->{$sName},sub{ "<h4>$_</h4> " }) }) }) . " </body> <html> " ; ### ------------------ ### ------------------ print $sOut; ### begin_: subroutines ### Simple Looping subroutine ### we use to make the perl code in our ### output template more compact sub sLoop { join"",map{$_[1]->();} (@{$_[0]}) }###end_sub ### begin_: end perl 1; __END__

which gives us something akin to ...

A || B || C || D || E || F || G || H || I || J || K || L || M || N || O || P || Q || R || S || T || U || V || W || X || Y || Z ||

A

africa

afone

aftwo

afthree

asia

asone

astwo

B

belgium

beone

brazil

brone

Replies are listed 'Best First'.
Re^2: Building html site maps
by wfsp (Abbot) on Jan 31, 2005 at 18:54 UTC
    scooterm

    Many thanks for reply.

    I'll need to make a hard copy of that and study it to try and see what's happening there. Certainly looks a lot neater than what I have at the moment.

    I think I need to have one more 'go' at H::T, but I must admit I still hanker after having it all in the script! Again, many thanks
    John

      I think I need to have one more 'go' at H::T, but I must admit I still hanker after having it all in the script!

      It is your script, so do what you wish. However, it would be great if you share the reason for your "hankering." Maybe we will learn something.

      Otoh, trust me, no better yet, trust the wisdom of ages -- separating logic from display would be the best transition you will make in programming. Putting millions of print and printf statements in the script not only creates a rat's nest, it is a mess trying to fix errors, and a chore trying to change its looks after-the-fact.

      Other templating systems have received a lot of traction, and I know not much about them. What I like about H::T is its desire to be good at very, very few things -- kinda like the iPod. And I really appreciate Sam Tregar's (the author of H::T) almost bull-headed insistence (as it seems to me on reading his posts on the H::T list) on not introducing unnecessary logic in the H::T code. It makes for one of the most wonderful pieces of code to work with.

      Try it. You may never work without it again whether you are creating dynamic or static websites or even munging data in non-web apps.

        I've tried it. Below is the script. It is verbose with shed loads of temps. When I'm more comfortable with what's happening I'm sure I'll be able to improve it (and some error checking would be nice!)

        I've also included the business section of the template (sans static content). It doesn't look as bad as I thought it would although there seems to be more template than html.

        The hardest part was building the data structures for the nested loops.

        Thanks for your gentle cajoling it was what I needed!

        Again, many thanks John

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://426399]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-25 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found