[Dancer-users] Tokens and Template plugins

Mike Schroeder mike at donor.com
Sat Oct 2 21:32:52 CEST 2010


I'm a new Dancer user, but I'm loving it so far.  In 4 days I was able to
learn dancer, create plugins for accessing our existing APIs, session
management and authentication, and port a relatively complex application.
 Dancer is elegantly simple.  Thanks for a great architecture.  Now for my
question:

Since the app I was porting used HTML::Template, I decided to use
the Dancer::Template::HtmlTemplate module contributed by David Precious.  It
nicely renders the tokens passed in, however I believe HTML::Template
expects a Hash Ref made up of either scalars or of Array of Hash - something
to the effect of:

{
                      PARAM => 'value',
                      PARAM2 => 'value',
                      LOOP_PARAM =>
                      [
                        { PARAM => VALUE_FOR_FIRST_PASS, ... },
                        { PARAM => VALUE_FOR_SECOND_PASS, ... }
                        ...
                      ],
                      ANOTHER_LOOP_PARAM =>
                      [
                        { PARAM => VALUE_FOR_FIRST_PASS, ... },
                        { PARAM => VALUE_FOR_SECOND_PASS, ... }
                        ...
                      ]
                    }


However, Dancer::Helpers::template()  passes the settings, session, request
and params as a Hash of Hash - making the vars inaccessible to
HTML::Template.

    $tokens ||= {};
    $tokens->{settings} = Dancer::Config->settings;
    $tokens->{request} = Dancer::SharedData->request;
    $tokens->{params}  = Dancer::SharedData->request->params;
    if (setting('session')) {
        $tokens->{session} = Dancer::Session->get;
    }

I think there are two approaches:

1) Dancer::Template::HtmlTemplate needs to be smart enough to fix up the
$tokens hash to reformat those default values into something visible to
HTML::Template - *something* like:

foreach my $kind ( qw( settings request params session ) ) {
      foreach my $key ( keys %{ $tokens->{$kind} } ) {
            $tokens->{$kind.'-'.$key} = delete $tokens->{$kind}->{$key};
      }

}

2) an alternate solution would be to make the creation of tokens a
subroutine that a Template plugin can expose back to Dancer as an
alternative way of serializing the tokens for the given Template engine.
 *something like:

$tokens ||= {};
if (Dancer::Template->engine->can('create_default_tokens') ) {
    $tokens = Dancer::Template->engine->create_default_tokens($tokens);
} else {
$tokens->{settings} = Dancer::Config->settings;
$tokens->{request} = Dancer::SharedData->request;
$tokens->{params} = Dancer::SharedData->request->params;
if (setting('session')) {
$tokens->{session} = Dancer::Session->get;
}
}



If I am missing something really obvious, my apologies in advance.

Assuming this *is*  a legitimate issue, which way is the preferred
solution?

Mike.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20101002/3307a60c/attachment.htm>


More information about the Dancer-users mailing list