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

Dancer 1.3124 templates FOREACH not working as expected?

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


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...

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1090729]
Front-paged by Corion
[atcroft]: I thought I saw them on, and just wanted to let them know.
[LanX]: hmm ... or even better /msg /msg [discipulus]
LanX better!
[atcroft]: .oO(Plus it was a little slow for a few minutes, at least... ;) )

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-02-24 20:45 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (311 votes). Check out past polls.