Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Dancer2 Template Toolkit interpolation

by cLive ;-) (Prior)
on Dec 20, 2016 at 06:35 UTC ( [id://1178159]=perlquestion: print w/replies, xml ) Need Help??

cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

EDIT: I worked it out. I thought Dancer2 defaulted to Template::Toolkit. It doesn't. Once I switched the templating engine, it worked as expected.

I'm making a simple Dancer2 App. I have the following data getting sent to the template:
template 'my_template', { ed => $ed, ad => $ad, voters => [ { 'lastname' => 'SCHMO', 'firstname' => 'JOE' }, { 'firstname' => 'SCHMO', 'lastname' => 'JIM' } ], };

And in the template I have:

<p>ed: <% ed %><br/> ad: <% ad %></p> <table> <tr> <th>First</th> <th>Last</th> </tr> <% FOREACH voter IN voters %> <tr> <td><% voter.firstname %></td> <td><% voter.lastname %></td> </tr> <% END %> </table>

When I load the view though, I see $ed and $ad, but all I see where I expect to see the voters is a bunch of HASH(0x409acc8) interpolations.

What am I doing wrong? It's probably something simple, but I can't see it. Any ideas?

Replies are listed 'Best First'.
Re: Dancer2 Template Toolkit interpolation
by kcott (Archbishop) on Dec 20, 2016 at 09:22 UTC

    G'day cLive ;-),

    By using the data structure you show ($vars in my code below), I'm getting the type of output I assume you're expecting. So, given the code and data you show, I can't reproduce your problem.

    By making a change to that data structure ($vars2), I can reproduce the type of output you're describing.

    "I have the following data getting sent to the template"

    I'd first confirm that with Data::Dumper (or similar). If that gets you nowhere, try hard-coding that hashref to see if you get the same results. You could also try running my test code, as is, and see if you're getting the same output as me.

    Here's my test code:

    #!/usr/bin/env perl use strict; use warnings; use Template; my $input = <<'EOT'; <p>ed=[% ed %] ad=[% ad %]</p> <table> <tr><th>First</th><th>Last</th></tr> [% FOREACH voter IN voters %] <tr> <td>[% voter.firstname %]</td> <td>[% voter.lastname %]</td> </tr> [% END %] </table> EOT my $vars = { ed => 'ED', ad => 'AD', voters => [ { firstname => 'F1', lastname => 'L1', }, { firstname => 'F2', lastname => 'L2', }, ], }; my $vars2 = { ed => 'ED', ad => 'AD', voters => [ { firstname => { name => 'F1' }, lastname => { name => 'L1' }, }, { firstname => { name => 'F2' }, lastname => { name => 'L2' }, }, ], }; my $t = Template::->new; $t->process(\$input, $vars); $t->process(\$input, $vars2);

    Output:

    <p>ed=ED ad=AD</p> <table> <tr><th>First</th><th>Last</th></tr> <tr> <td>F1</td> <td>L1</td> </tr> <tr> <td>F2</td> <td>L2</td> </tr> </table> <p>ed=ED ad=AD</p> <table> <tr><th>First</th><th>Last</th></tr> <tr> <td>HASH(0x7f8f68805630)</td> <td>HASH(0x7f8f688448b0)</td> </tr> <tr> <td>HASH(0x7f8f68844688)</td> <td>HASH(0x7f8f688446d0)</td> </tr> </table>

    — Ken

      Thanks for youe insights. In the end though, I worked out I had the wrong templating engine set in the config - knew it would be something simple :D
Re: Dancer2 Template Toolkit interpolation
by Anonymous Monk on Dec 20, 2016 at 08:23 UTC

    What am I doing wrong? It's probably something simple, but I can't see it. Any ideas?

    Old version of Template...? Mysterious template engine configuration option?

    This works for me

    $ cat tt2-iterate-arrayohash.tt [%# ## tt2-iterate-arrayohash.tt ## 2016-12-20-00:32:23 ## ## -%] [% SET ed = "eddy"; SET ad = "addy"; SET voters = [ { 'lastname' => 'SCHMO', 'firstname' => 'JOE' }, { 'firstname' => 'SCHMO', 'lastname' => 'JIM' } ]; USE Dumper Indent = 1; GET Dumper.dump( [ voters , ed, ad ] ); -%] [%- TAGS <% %> -%] <p>ed: <% ed %><br/> ad: <% ad %></p> <table> <tr> <th>First</th> <th>Last</th> </tr> <% FOREACH voter IN voters %> <tr> <td><% voter.firstname %></td> <td><% voter.lastname %></td> </tr> <% END %> </table> $ tpage tt2-iterate-arrayohash.tt $VAR1 = [ [ { 'firstname' => 'JOE', 'lastname' => 'SCHMO' }, { 'firstname' => 'SCHMO', 'lastname' => 'JIM' } ], 'eddy', 'addy' ]; <p>ed: eddy<br/> ad: addy</p> <table> <tr> <th>First</th> <th>Last</th> </tr> <tr> <td>JOE</td> <td>SCHMO</td> </tr> <tr> <td>SCHMO</td> <td>JIM</td> </tr> </table>
      Thanks for youe insights. In the end though, I worked out I had the wrong templating engine set in the config - knew it would be something simple :D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1178159]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found