Monks ~

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:
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", );
...with the hope of passing it roughly like this (where '$org' and '$group" are hashrefs):
slashDisplay( 'group_edit', { org => $org, group => $group, });
...into this template:
<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:
slashDisplay( 'group_edit', { hashref => $hashref });
...which is simple in the code but would mean this in the template:
<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.
slashDisplay( 'group_edit', { org => { id => $hashref->{ org_id }, name => $hashref->{ org_name }}, group => { id => $hashref->{ group_id }, name => $hashref->{ group_name }}, });
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)?
--
Man With No Legs, Inc.

In reply to Splitting a hashref into hashrefs by legLess

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.