nathaniels has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, Having some trouble getting my templates to work in Dancer. In my web app package I have this:
get '/home' => sub { my $db = connect_db(); my $sth = $db->prepare('SELECT ID, FirstName, LastName FROM People +Tbl ORDER BY ID DESC') or die "Couldn't prepare statement: " . $db->errstr; $sth->execute or die $sth->errstr; template 'home.tt', { list => [ 'Tom', 'Dick', 'Larry'], }; };
Here's the Template:
<HTML> <TITLE>Home</TITLE> <H1>You're home!</H1> <H2>Here's a list of things:</H2> <BODY> <% FOREACH item IN list %> <LI><% item %></LI> <% END %> </BODY> </HTML>
And here's the config file in case I've gotten something wrong there.
#template engine # simple: default and very basic template engine # template_toolkit: TT template: "simple" template: "template_toolkit" # engines: # template_toolkit: # start_tag: '[%' # end_tag: '%]'
I know from previous attempts that I can carry over a scalar variable without a hitch but anytime I try to use a FOREACH loop I get nothing. Thanks!

Replies are listed 'Best First'.
Re: Dancer and Template-Toolkit
by kcott (Archbishop) on Sep 08, 2013 at 03:18 UTC

    G'day nathaniels,

    Welcome to the monastery.

    It's been a few years since I last used Template::Toolkit and I haven't used Dancer. Bearing those caveats in mind, your main problem seems to be this:

    FOREACH item IN list

    which, from the examples in Template::Manual::Intro, should be:

    FOREACH item = list

    You also have secondary problems in your HTML code: <title> should be in a <head> element; <h1> and <h2> should be inside the <body> element; and <li> should be in a <ul> element. I'd suggest your template should look a bit more like this:

    <html> <head> <title>...</title> </head> <body> <h1>...</h1> <h2>...</h2> <ul> <% FOREACH item = list %> <li><% item %></li> <% END %> </ul> </body> </html>

    Some browsers may be forgiving about this but not necessarily consistently and it's not something you should rely on.

    -- Ken

      "<title> should be in a <head> element; <h1> and <h2> should be inside the <body>..."

      Allow me to blow your mind... they already are!

      Many HTML elements have optional closing tags. For example:

      <p>Foo <p>Bar

      When an HTML parser encounters the second opening <p> tag, it can infer that the first <p> element must have closed, because paragraphs cannot be legitimately nested. (Well, actually they can if you use the right interleaving elements, but that's another story...)

      This is not just browsers being forgiving. It's part of the HTML spec; even allowed by the strict versions of HTML. (But not by XHTML obviously, which is an entirely different kettle of fish altogether.)

      Now, here's the mindblowing bit. And it relies on you being able to divorce tags and elements a little in your mind. We've seen how an element can legitimately exist without a closing tag; it's also possible for an element to exist without an opening tag!

      Given the following:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <title>Foo</title> <p>Bar</html>

      ... when a parser encounters the <title> tag, it infers that not only the <title> element has been opened, but that <html> and <head> have too. When it encounters <p> it infers that <head> must have closed (because <p> cannot be used within <head>) and <body> must have begun.

      The Foo Bar document above is a complete, valid HTML 4.01 Strict document. Don't believe me?

      spec ref

      Now, you could argue that omitting optional start tags is bad style. I might even agree with you. But it's very, very common. (Hint: take a look at the content model for the <table> element.)

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Ah. Thank you for your speedy reply!
Re: Dancer and Template-Toolkit
by CountZero (Bishop) on Sep 08, 2013 at 19:59 UTC
    Isn't the standard Template Toolkit tags pair [% and %] rather than <% and %> ?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Yes. However dancer defaults to using <% %> unless you set the config file to use the TT default.