config.yml options not visible to Dancer::Template::HtmlTemplate
Hi all, While I can set my needed options for HTML::Template in config.yml per the documentation/guidance of the list, it never reaches Dancer::Template::HtmlTemplate. The issue is here, in HtmlTemplate.pm, in in the render method : ---- my $ht = HTML::Template->new( filename => $template, die_on_bad_params => 0, # Required, as we pass through other params too %{$self->config} ); $ht->param($tokens); return $ht->output; ---- The problem is that $self->config is empty here. I'm using version 0.06. Also, I don't necessarily want die_on_bad_params hard coded that way. Besides that, what I really miss is the ability to do this sort of thing: ---- if (condition) { set a certain param for the template } else { set some other param } ---- I can't do this because the module doesn't inherit the param method from HTML::Template. Can't Dancer be wangled into setting template tokens in multiple places for a given route, or must it happen all at once? Side note -- whitespace is apparently syntactically significant in YAML. Of what possible benefit is that? Clues or opinions welcome. Thanks, GJ
I'm not sure this is bug. I use Mason for template engine. I just pass config values into the template like any other value. I use the before_template for passing in common stuff: hook before_template_render => sub { my ($tokens) = @_; $tokens->{fb_app_id} = config->{facebook}->{app_id}; } On 18 March 2012 18:23, GJ <gj@freeshell.org> wrote:
Hi all,
While I can set my needed options for HTML::Template in config.yml per the documentation/guidance of the list, it never reaches Dancer::Template::HtmlTemplate. The issue is here, in HtmlTemplate.pm, in in the render method :
---- my $ht = HTML::Template->new( filename => $template, die_on_bad_params => 0, # Required, as we pass through other params too %{$self->config} ); $ht->param($tokens); return $ht->output; ----
The problem is that $self->config is empty here. I'm using version 0.06.
Also, I don't necessarily want die_on_bad_params hard coded that way.
Besides that, what I really miss is the ability to do this sort of thing:
---- if (condition) { set a certain param for the template } else { set some other param } ----
I can't do this because the module doesn't inherit the param method from HTML::Template. Can't Dancer be wangled into setting template tokens in multiple places for a given route, or must it happen all at once?
Side note -- whitespace is apparently syntactically significant in YAML. Of what possible benefit is that?
Clues or opinions welcome.
Thanks, GJ _______________________________________________ Dancer-users mailing list Dancer-users@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
On Sun, Mar 18, 2012 at 06:29:13PM +0000, Stephen Fenwick-Paul wrote:
I'm not sure this is bug. I use Mason for template engine. I just pass config values into the template like any other value.
I use the before_template for passing in common stuff:
hook before_template_render => sub { my ($tokens) = @_;
$tokens->{fb_app_id} = config->{facebook}->{app_id};
}
I suspect that the Mason plugin behaviour is irrelevant unfortunately. The guts of the Template::Toolkit plugin for example, are completely different than that of the HTML::Template one. However, if what you write above is true, then what is the point of setting the directives in config.yml, if the app cannot parse them in directly? Thanks GJ
Sorry for the slow reply. On Sun, 18 Mar 2012 18:23:59 +0000 GJ <gj@freeshell.org> wrote:
While I can set my needed options for HTML::Template in config.yml per the documentation/guidance of the list, it never reaches Dancer::Template::HtmlTemplate. The issue is here, in HtmlTemplate.pm, in in the render method :
---- my $ht = HTML::Template->new( filename => $template, die_on_bad_params => 0, # Required, as we pass through other params too %{$self->config} ); $ht->param($tokens); return $ht->output; ----
The problem is that $self->config is empty here. I'm using version 0.06.
What does your config.yml look like?
Also, I don't necessarily want die_on_bad_params hard coded that way.
Without it, unless you use all of the stuff Dancer provides automatically to the template (details of the request, config, etc), HTML::Template will die; that's probably not helpful.
Besides that, what I really miss is the ability to do this sort of thing:
---- if (condition) { set a certain param for the template } else { set some other param } ----
I can't do this because the module doesn't inherit the param method from HTML::Template. Can't Dancer be wangled into setting template tokens in multiple places for a given route, or must it happen all at once?
The typical Dancer way of handling it is a single "render this template with these params" action. You could, of course, build up the params you want to pass to your template individually, then hand them off at the end - e.g.: get '/foo' => sub { my %template_params; if (params->{foo}) { $template_params->{foo} = 'Some foo'; } # ... etc .... template 'foo', \%template_params; }
Side note -- whitespace is apparently syntactically significant in YAML. Of what possible benefit is that?
Yeah, that's just how YAML is. Some like it (Python fans), some don't. You never know, it's possible that Dancer2 might support various config file formats, so you could write your config in JSON if it felt more natural to you. Cheers Dave P -- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
On Tue, Mar 20, 2012 at 03:29:08PM +0000, David Precious wrote:
Sorry for the slow reply.
On Sun, 18 Mar 2012 18:23:59 +0000 GJ <gj@freeshell.org> wrote:
While I can set my needed options for HTML::Template in config.yml per the documentation/guidance of the list, it never reaches Dancer::Template::HtmlTemplate. The issue is here, in HtmlTemplate.pm, in in the render method :
---- my $ht = HTML::Template->new( filename => $template, die_on_bad_params => 0, # Required, as we pass through other params too %{$self->config} ); $ht->param($tokens); return $ht->output; ----
The problem is that $self->config is empty here. I'm using version 0.06.
What does your config.yml look like?
---- appname: "My::App" # The default layout to use for your application (located in # views/layouts/main.tt) layout: "main" # when the charset is set to UTF-8 Dancer will handle for you # all the magic of encoding and decoding. You should not care # about unicode within your app when this setting is set (recommended). charset: "UTF-8" #template: "simple" template: "html_template" engines: HtmlTemplate: loop_context_vars: '1' global_vars: '1' session: "cookie" session_cookie_key: "xxxx" plugins: Database: driver: 'mysql' database: 'xxxx' host: 'localhost' username: 'xxxx' password: 'xxxx' ---- I noticed that some of the other template plugins actually access $self->config in init() by the way. Thanks GJ
Hi David, I did some more investigation into this. On Tue, Mar 20, 2012 at 03:29:08PM +0000, David Precious wrote:
Sorry for the slow reply.
On Sun, 18 Mar 2012 18:23:59 +0000 GJ <gj@freeshell.org> wrote:
While I can set my needed options for HTML::Template in config.yml per the documentation/guidance of the list, it never reaches Dancer::Template::HtmlTemplate. The issue is here, in HtmlTemplate.pm, in in the render method :
---- my $ht = HTML::Template->new( filename => $template, die_on_bad_params => 0, # Required, as we pass through other params too %{$self->config} ); $ht->param($tokens); return $ht->output; ----
The problem is that $self->config is empty here. I'm using version 0.06.
It looks like the HTML::Template directives I've placed in config.yml that are supposed to be in %{$self->config} are buried in the guts of %$tokens. It should be possible to dig them out and then pass them off to HTML::Template in render() right? That is presuming that it is not supposed to be inheriting that method from elsewhere.
Also, I don't necessarily want die_on_bad_params hard coded that way.
Without it, unless you use all of the stuff Dancer provides automatically to the template (details of the request, config, etc), HTML::Template will die; that's probably not helpful.
It seems that you are talking about the same variables that are transformed in _flatten, yes? IMHO, the best thing would be save them off since the module is digging them out of %$tokens anyways and plop them in front of the user's own TMPL_VARs at the point that they are being passed on to HTML::Template's param method. That way, you leave die_on_bad_params alone, thereby preserving whatever behaviour is expected by the user, who is after all likely to be used to having the ability to set or not set that directive. Besides, if my config.yml was being parsed by this module correctly, then if I had set die_on_bad_params to true in config.yml, as I am wont to do, it could have thereby overridden the hard-coded setting and thus have caused a mess anyways.
Besides that, what I really miss is the ability to do this sort of thing:
---- if (condition) { set a certain param for the template } else { set some other param } ----
I can't do this because the module doesn't inherit the param method from HTML::Template. Can't Dancer be wangled into setting template tokens in multiple places for a given route, or must it happen all at once?
The typical Dancer way of handling it is a single "render this template with these params" action.
Noted. I am trying to avoid code rewriting wherever possible but this seems like a reasonable compromise. Thanks GJ
participants (3)
-
David Precious -
GJ -
Stephen Fenwick-Paul