Hi, I'm trying to have my Dancer Plugin FlashMessage work with Dancer2. However, this plugin tries to installs a hook on before_template_render. This doesn't work in Dancer2 because the 'hook' keyword does : $self->app->add_hook(...), so it adds the hook on the app instance. However this app instance only supports these hooks : qw/before after before_request after_request/ (see Dancer::Core::App::supported_hooks ). To be able to add a before_template_render, I should access to a template instance that consumes the Dancer::Core::Role::Template role. So, basically, the code of 'hook()' in Dancer::Core::DSL should call add_hook on the app, it should find out which instance the hook should be added to, and call add_hook on it. Now that brings up some questions. An easy way would be that there is a central point that lists all the possible hooks, and which instance (either the app, or the template engine) ->add_hook should be called upon. But that breaks the encapsulation (each role advertise which hooks it supports). And loading every engines to find out which one supports a given hook seems a bit heavy to me, and prevent the "load only when needed" practice. So, any idea ?
So, Looking at the code, Dancer2 go ask every engine which hooks can be created. However, it requires the engines to be created. So you can't install a before_template hook before having setup the engine template. So you need to have setting template => 'template_toolkit'; for instance, before being able to create a template hook. It's problematic with Plugins, that may want to create hooks *before* the app has set a template engine for instance. On 14 March 2012 01:34, damien krotkine <dkrotkine@gmail.com> wrote:
Hi,
I'm trying to have my Dancer Plugin FlashMessage work with Dancer2.
However, this plugin tries to installs a hook on before_template_render. This doesn't work in Dancer2 because the 'hook' keyword does : $self->app->add_hook(...), so it adds the hook on the app instance. However this app instance only supports these hooks : qw/before after before_request after_request/ (see Dancer::Core::App::supported_hooks ).
To be able to add a before_template_render, I should access to a template instance that consumes the Dancer::Core::Role::Template role.
So, basically, the code of 'hook()' in Dancer::Core::DSL should call add_hook on the app, it should find out which instance the hook should be added to, and call add_hook on it.
Now that brings up some questions. An easy way would be that there is a central point that lists all the possible hooks, and which instance (either the app, or the template engine) ->add_hook should be called upon. But that breaks the encapsulation (each role advertise which hooks it supports). And loading every engines to find out which one supports a given hook seems a bit heavy to me, and prevent the "load only when needed" practice.
So, any idea ?
Thanks Damien for the precisions. Le 16 mars 2012 00:08, damien krotkine <dkrotkine@gmail.com> a écrit :
So,
Looking at the code, Dancer2 go ask every engine which hooks can be created. However, it requires the engines to be created.
So you can't install a before_template hook before having setup the engine template. So you need to have setting template => 'template_toolkit'; for instance, before being able to create a template hook.
It's problematic with Plugins, that may want to create hooks *before* the app has set a template engine for instance.
I see. Do you think it would be possible to isolate that in a test script for Dancer 2 ? That will be easier for me to dig in the code to find a solution. Thanks.
Yes I'll work on a test script. On 16 March 2012 17:33, Alexis Sukrieh <sukria@sukria.net> wrote:
Thanks Damien for the precisions.
Le 16 mars 2012 00:08, damien krotkine <dkrotkine@gmail.com> a écrit :
So,
Looking at the code, Dancer2 go ask every engine which hooks can be created. However, it requires the engines to be created.
So you can't install a before_template hook before having setup the engine template. So you need to have setting template => 'template_toolkit'; for instance, before being able to create a template hook.
It's problematic with Plugins, that may want to create hooks *before* the app has set a template engine for instance.
I see. Do you think it would be possible to isolate that in a test script for Dancer 2 ?
That will be easier for me to dig in the code to find a solution.
Thanks.
Hi, Test script is in the topic/hook_issues branch : https://github.com/PerlDancer/Dancer2/commit/4bd5f1d9a54792da2801257d4159976... See line 31. If you uncomment this line, things suddenly work. We should have to do that. On 16 March 2012 17:35, damien krotkine <dkrotkine@gmail.com> wrote:
Yes I'll work on a test script.
On 16 March 2012 17:33, Alexis Sukrieh <sukria@sukria.net> wrote:
Thanks Damien for the precisions.
Le 16 mars 2012 00:08, damien krotkine <dkrotkine@gmail.com> a écrit :
So,
Looking at the code, Dancer2 go ask every engine which hooks can be created. However, it requires the engines to be created.
So you can't install a before_template hook before having setup the engine template. So you need to have setting template => 'template_toolkit'; for instance, before being able to create a template hook.
It's problematic with Plugins, that may want to create hooks *before* the app has set a template engine for instance.
I see. Do you think it would be possible to isolate that in a test script for Dancer 2 ?
That will be easier for me to dig in the code to find a solution.
Thanks.
participants (2)
-
Alexis Sukrieh -
damien krotkine