Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-02-27 19:09 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (392 votes). Check out past polls.