Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: How do you use Template Toolkit with Dancer2?

by choroba (Cardinal)
on Apr 26, 2022 at 10:13 UTC ( [id://11143304]=note: print w/replies, xml ) Need Help??


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

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]

Replies are listed 'Best First'.
Re^3: How do you use Template Toolkit with Dancer2?
by PiotrS (Novice) on Apr 27, 2022 at 13:54 UTC

    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]
        Thank you for your answer. If a menu template can be included in a "parent" template, how would one go about including two or more menus in the "parent" template, where the difference is only the menu's name and the data-set. Wouldn't that introduce ambiguity to the assignment of variables within the menu template?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143304]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-19 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found