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: '%>'
| [reply] [d/l] [select] |
|
|
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.
| [reply] |
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".
| [reply] [d/l] [select] |
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? | [reply] |
|
|
| [reply] |
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.
| [reply] [d/l] [select] |
|
|
<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]
| [reply] [d/l] [select] |
|
|
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. | [reply] [d/l] |
|
|
|
|
|
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... | [reply] |
|
|
#!/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 });
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
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]
| [reply] [d/l] [select] |
|
|
Thanks for your response. Yes I do. I think it might be something to do with interpolation.
| [reply] |
|
|
|
|