[dancer-users] set vs var

David Precious davidp at preshweb.co.uk
Tue Feb 23 12:16:31 GMT 2016


On Mon, 22 Feb 2016 10:23:58 -0800
"Hermann Calabria" <hermann at ivouch.com> wrote:
> In this case, ‘vars’ is not passed on to Template Tookit, whereas
> ‘settings’ is.  If I had run this within a normal D1 route, ‘vars’
> would have been passed.
> 
> So it appears I need to refactor the entire (large) application to
> use “set” and not “var”.  Is this correct?  Why isn’t “var” the
> correct option here?  Or if it is, what am I doing wrong?

If you need to pass particular stuff to your template, passing it to
template() is the ideal way -  in your small example, you could have
said e.g.:

  print template 'test', { hello => '...' };

However, I suspect you know that, and the example was cut down from
something much bigger.  Still, you could assemble the template params
as you go, and then pass them to template().

Other than that - the fact that template() doesn't see vars if there's
no request being processed is by design, but a design that could be
re-considered, possibly.  In normal usage, vars gets cleared after a
request is done, and in your usage, you're in control of it, so I see
no real harm in it being passed to the template either way.


In Dancer::Template::Abstract, we say:

    # If we're processing a request, also add the request object,
    # params and vars as tokens:
    if (my $request = Dancer::SharedData->request) {
        $tokens->{request}        = $request;
        $tokens->{params}         = $request->params;
        $tokens->{vars}           = Dancer::SharedData->vars;
    }

So, that's why you're not getting the vars in your template.

I see no major reason that the adding of vars to the tokens couldn't be
moved outside of that if block so that anything in vars is passed on
either way.

Thoughts, anyone else?

Cheers

Dave P


More information about the dancer-users mailing list