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

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.

Replies are listed 'Best First'.
Re^4: How do you use Template Toolkit with Dancer2?
by choroba (Cardinal) on Apr 27, 2022 at 18:36 UTC
    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?
        That's why templates have their arguments, too. See INCLUDE:
        [% INCLUDE template var = val, ... %]

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