in reply to How do you use Template Toolkit with Dancer2?

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.

Replies are listed 'Best First'.
Re^2: How do you use Template Toolkit with Dancer2?
by choroba (Cardinal) on Apr 26, 2022 at 10:13 UTC
    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.
        Yes, you're breaking the Model - View - Controller paradigm. You can include the dropdown menu template from other templates, sending only the data from the code (i.e. no "dropdown_menu" in the code).

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