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

What am I doing wrong?

I created a new Dancer2 app with dancer2 gen --application="App::Photos" --directory="App-Photos" --git -x on the command line (my OS is one of the many which fork off of Ubuntu).

Out of the box, the Dancer2 default page works.

Great, now let's turn on a useful template engine.

Simply go into config.yml and comment out template: "simple" and uncomment the template: "template_toolkit" block underneath it.

Now restart the app and reload the page to see our change.

Wat.

The page title is now <% title %> and the displayed page consists of a single line containing <% content %> (obviously, no templating engine is now at work).

So now I ask you, venerable Perl Monks, what am I doing wrong regarding using Template Toolkit in a new Dancer2 app?

  • Comment on How do you use Template Toolkit with Dancer2?

Replies are listed 'Best First'.
Re: How do you use Template Toolkit with Dancer2?
by Corion (Patriarch) on Jan 17, 2022 at 12:23 UTC

    The Template::Toolkit templates have a different syntax than the default Dancer templating system. You will have to rewrite the existing templates to use TT syntax ([% ... %]) or switch Template::Toolkit to use <% ... %> as syntax and then look at where the Dancer template system differs from TT.

    Update: Just as I've posted this, I also read the same thing in Dancer::Template::TemplateToolkit. It seems you can even switch to <% ... %> syntax from within the config:

    template: template_toolkit engines: template_toolkit: start_tag: '[%' stop_tag: '%]'

    Update 2: This is the documentation for Dancer, not Dancer2... For Dancer2, it's the other way around (Dancer2::Template::TemplateToolkit) :

    engines: template: template_toolkit: start_tag: '<%' end_tag: '%>'

      Thanks!

      Also, I discovered I had turned on a different piece of config as well: the YAML session engine block at the bottom of the config.yml file.

      It was that session engine block which was breaking things. As soon as I turned that off, the page worked again.

Re: How do you use Template Toolkit with Dancer2?
by kcott (Archbishop) on Jan 20, 2022 at 21:29 UTC

    G'day v3ritas,

    Welcome to the Monastery.

    I was looking at a $work Dancer2 project this morning and recalled there was a question about this a few days ago. Here's some addional information.

    $ cat config.yml ... template: "template_toolkit" engines: template: template_toolkit: START_TAG: '<%' END_TAG: '%>' PRE_CHOMP: 1 COMPILE_EXT: '.tcc'

    In brief:

    • START_TAG and END_TAG are fairly self evident.
    • PRE_CHOMP gets rid of lots of unwanted whitespace in the generated output.
    • COMPILE_EXT caches templates.

    For more detailed information on those, see Template::Manual::Config and "Dancer2::Template::TemplateToolkit - Template Caching".

    — Ken

Re: How do you use Template Toolkit with Dancer2?
by etj (Priest) on Mar 16, 2022 at 13:45 UTC
    I remember finding the template toolkit options difficult to get right out of the box as well. Sounds like a doc patch would be a good idea?

      Sounds like you have a good idea. I'll help review your proposed changes.


      The way forward always starts with a minimal test.
Re: How do you use Template Toolkit with Dancer2?
by PiotrS (Novice) on Apr 25, 2022 at 20:08 UTC
    Hello again. My question is to those familiar with Dancer2 (and Template Toolkit.) I'm new to these, as I am considering them as migration tools from CGI. I've been looking for information on how are classic form elements (e.g., radio groups, pull-down menus, check boxes, etc.) implemented. I have been unsuccessful in finding any examples. I am particularly interested in those that are dynamically populated with options from a database, a hash, or some other data structure. For example, using CGI one would write:
    my $menu = popup_menu(-name=>'menu_name', -values=>['eenie','meenie','minie'], -default=>['meenie','minie'], -labels=>\%labels, -attributes=>\%attributes); print $menu;
    The content of the variable $menu could also then be used to fill a template "on-the-fly". Anyway, any suggestions or references to where I could glean such info would be greatly appreciated.
      Using Template::Toolkit, create a template like this:
      <select name="[% name %]" > [%- FOR option IN options %] <option [% FOREACH attr IN option.attributes.keys %][% attr %]="[% opt +ion.attributes.$attr %]" [% END -%] [%- IF option.default %]selected="selected" [% END -%] value="[% option.value %]">[% option.label %]</option> [%- END %] </select>

      In Dancer2 code, just call it with

      return template 'menu', {name => 'menu_name', options => [ {value => 'eenie', label => 'your first choice', attributes +=> {class => 'first choice'}}, {value => 'meenie', label => 'your second choice', default => + 1}, {value => 'minie', label => 'your third choice', default => + 1}], }

      Update: Accept any attributes, not just class (not sure I'd recommend it).

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Thank you for your suggestion.

        When I tried to encapsulate this code in a subroutine, and call it in another subroutine, Dancer2 seems to automatically use the main template located in the layout directory as a wrapper to this snippet, as well at to the entire page that generated an undesired visual effect. Ideally, it should be possible to write a simple subroutine that returns a menu, that, in turn can be invoked in some other subroutine that generates an arbitrary form:
        sub dropdown_menu { # should generate only an HTML snippet of a menu #my ..arguments..; return template 'form_menu.tt', { ...argument hash... }; } . . . get '/' => sub { # generates the actual form that is used as [% conten +t %] in the layout/main.tt template template 'some_form.tt', { menu1 => dropdown_menu(...parameters1...), menu2 => dropdown_menu(...parameters2...), radio1 => radio_group(...parameters3...), . . } }
        Does that make sense, or am I accidentally breaking some Dancer2 paradigm? I apologize if this question is trivial.
Re: How do you use Template Toolkit with Dancer2?
by PiotrS (Novice) on Apr 21, 2022 at 21:26 UTC
    Hi, everyone! I have a related question, the answer to which may be of some use to others. I have configured template_toolkit, and it seem to work, but I am running into an issue with UTF8 encoding. E.g.: if in the template I write 'François' the 'ç' is rendered correctly. However if I add % name % and assign name => 'François' in a route, the 'ç' is rendered as 'ħ'. Does anyone know how to remedy this? Thanks in advance...

      You have not mentioned Dancer2 at all, so I can only assume that part is irrelevant. In which case, here is my SSCCE:

      #!/usr/bin/env perl use strict; use warnings; use utf8; use Template; my $tt = Template->new; my $name = 'François'; my $ttext = <<EOT; As a literal: François As an interpolated variable: $name As a template variable: [% name %] EOT $tt->process (\$ttext, { name => $name });

      🦛

        Thank you for your quick response. Yes, I am investigating Dancer 2 to use as a replacement for the venerable CGI I've been using for many years. So my question is in context of Dancer 2. I am still in the early stages of familiarizing myself with it. Any suggestions would be greatly appreciated.
      What encoding do you save the source code in? Do you use utf8 to tell Perl your source code uses UTF-8?

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Thanks for your response. Yes I do. I think it might be something to do with interpolation.