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

Dancer 1.3124 templates FOREACH not working as expected?

by peter.pl (Initiate)
on Jun 21, 2014 at 06:49 UTC ( #1090729=perlquestion: print w/ replies, xml ) Need Help??
peter.pl has asked for the wisdom of the Perl Monks concerning the following question:

(SOLVED)

Greetings Monks,

By way of introduction, I'm a new user on the site and new to the Dancer framework, though I did spend some !quality time working with Django quirks at my last job so I'm not new to these frameworks in general. I'm porting a simple CLI Google Place miner into a simple web app, because then the user can run the queries herself and maybe I'll generate maps suitable for printing or something.

However, the included template parser doesn't seem to be handling arrays as expected using the <% FOREACH item IN array %> construct. I can't, for the life of me, figure out how to get it to work properly. I'm using Perl 5.16.2 with Dancer 1.3124 on OpenSuSE 12.3.

Here's the dispatch and template code

# Index / Landing Page get '/geomine/' => sub { my $filename = config->{public} . "/types.list"; my $all = read_file( $filename ); my @types = split /\n/, $all; template 'start', { 'types' => \@types }; };
<% FOREACH item IN types %> <% item %><br> <% END %>

From that I know, passing the arrayref is correct. (Passing the array results in only the first item being printed.) I've dumped the read_file results and know that it's an array of lines. (Yeah, I know I'm unnecessarily using File::Slurp->read_file in a scalar context but I figured, what the heck...)

$VAR1 = 'this'; $VAR2 = 'that'; $VAR3 = 'the other thing';

This results in one space-delimited line as if I had passed a scalar:

<h3>Select types</h3> this that the other thing <br> <input type="submit" name="Run Query">

(That's the HTML from Firebug.) Originally it was supposed to be a group of checkboxes, and the result was the same except it was followed by one checkbox. I've tried it by defining the arrayref directly in the template, just like in the examples, and the results are same. It's just not working properly.

Am I doing something wrong? Should I try to extract keys from a hashref instead? Use Dancer2? I feel a forehead slap coming...

Comment on Dancer 1.3124 templates FOREACH not working as expected?
Select or Download Code
Re: Dancer 1.3124 templates FOREACH not working as expected? (template engine?)
by Anonymous Monk on Jun 21, 2014 at 08:34 UTC
      Thanks! That worked.
        (i.e. I uncommented the
        template: "template_toolkit"
        directive in config.yml.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-07-29 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls