legLess has asked for the wisdom of the Perl Monks concerning the following question:
I'm struggling with the very laudable goal of making my HTML templates as easy to use as possible, and hoping I can learn some new Perlery while I'm at it. I'm using Slash and Template Toolkit, but I think the Slash methods are clear enough so no knowledge of it is needed to understand the code (yeah, that surprised me too once I started hacking it :). My solution below works, but strikes me as ugly.
The Problem
I'm getting a hashref (cunningly named '$hashref' for your reading pleasure) back from a database:...with the hope of passing it roughly like this (where '$org' and '$group" are hashrefs):my $hashref = $slashdb->sqlSelectHashref( # SELECT 'o.org_id, o.name, g.group_id, g.name', # FROM 'groups AS g, orgs AS o', # WHERE "group_id = $group_id AND o.org_id = g.org_id", );
...into this template:slashDisplay( 'group_edit', { org => $org, group => $group, });
<p>Org id: [% org.id %]</p> <p>Org name: [% org.name %]</p> <p>Group id: [% group.id %]</p> <p>Group name: [% group.name %]</p>
First attempt: splitting hashrefs in the database return
I tried a few ways of doing this in the database call, but I confess that my Perl wasn't up the the task. I was hoping to pass the data to two hashrefs (e.g. $orgs and $groups) but I couldn't figure out how to slice up the returned hashref into two new hashrefs. Doing it in the database call would also allow me write the SELECT part of the query as written above rather than what follows, which is what I've used to prevent key collisions in the hashref:'o.org_id, o.name AS org_name, g.group_id, g.name AS group_name',
An arrayref won't work
I could get an arrayref from the database -- these are easy to split -- but that would mean using (e.g.) [% org.1 %] in the template, which isn't very readable and breaks when the field order changes.One hashref won't work
I could also pass the hashref directly to the template:...which is simple in the code but would mean this in the template:slashDisplay( 'group_edit', { hashref => $hashref });
<p>Org id: [% hashref.org_id %]</p> <p>Org name: [% hashref.org_name %]</p> <p>Group id: [% hashref.group_id %]</p> <p>Group name: [% hashref.group_name %]</p>
What worked
In the end I came up with this: constructing an anonymous hashref and passing it to the template. It gives me total control of the named parameters I pass to the template and ensures that the HTML guy (also me :) has the easiest experience possible.The only thing I don't like is that while the template has been cleaned up the code is much gnarlier. On balance I think this is a good trade-off, but I wonder if my ignorance is setting up a false dichotomy. Is there a less gnarly way of doing this (or at least a way with less typing)?slashDisplay( 'group_edit', { org => { id => $hashref->{ org_id }, name => $hashref->{ org_name }}, group => { id => $hashref->{ group_id }, name => $hashref->{ group_name }}, });
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Splitting a hashref into hashrefs
by submersible_toaster (Chaplain) on Mar 20, 2003 at 04:01 UTC | |
Re: Splitting a hashref into hashrefs
by Aragorn (Curate) on Mar 20, 2003 at 09:35 UTC | |
Re: Splitting a hashref into hashrefs
by perrin (Chancellor) on Mar 20, 2003 at 05:07 UTC | |
by legLess (Hermit) on Mar 20, 2003 at 08:19 UTC | |
by perrin (Chancellor) on Mar 20, 2003 at 17:21 UTC | |
by legLess (Hermit) on Mar 20, 2003 at 22:43 UTC | |
by perrin (Chancellor) on Mar 20, 2003 at 22:53 UTC |