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


In reply to Dancer 1.3124 templates FOREACH not working as expected? by peter.pl

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.