Re: [Mojolicious] Re: Structured helpers
The template is rendered within its own namespace unique to that compilation, so you cannot inject subs directly. You can however pass coderefs in the stash and then do $foobar->() in the template. But namespacing the helpers seems reasonable to me. -Dan On Thu, Mar 29, 2018 at 9:47 AM, Leo Cacciariwrote: > On Wed, Mar 28, 2018 at 2:39 PM, sri wrote: > >> That being said, I would ask to the collective wisdom of the group: > >> > >> 1. would proxy defined as explained in the cookboock work in > templates? > > > > > > Technically, you can always do "<%= $c->my_helper_prefix->foo('bar') > %>". > > > When I read this I started banging my head on the desk: I had > *completely missed* the paragraph in the rendering guide about the > controller being accessible > in the template scripts. Now that the headache has subsided, I can reply :) > > Since the main reason I wanted 'structured' helpers was that I had two > helpers that logically should have had the same name but > where actually quite different. So I thought to introduce two helpers: > foo.foobar, to be used in the context of controller Foo to do foobnar, > and bar.foobar meant to do a different foobar in the context of > controller Bar. > > What I did, was to define methods Foo::_foobar and Bar::_foobar and > then, in my app setup, define a single foobar helper with > > $app->helper(foobar => sub { > my $c = shift; > return $c->_foobar(@_) if $c->$_can('_foobar'); > # do something (e.g. croak) to handle the case were the > controller has no _foobar > }); > > > I tried to define directly Foo::foobar and Bar::foobar, but I get > something like > > Undefined subroutine > ::Template::Sandbox::6bd05336357ba117d6ac9a329ea3a6ec::foobar > > meaning there is something (well, more than some thing) that I do not > understand in how the functions are injected into the template object. > However, the solution above works for me, and I'm content :). > Obviously, I'm open to suggestion for better ways to do this. > > cheers > > > -- > Leo Cacciari > > Aliae nationes servitutem pati possunt. Populi Romani est propria libertas. > > -- > You received this message because you are subscribed to the Google Groups > "Mojolicious" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to mojolicious+unsubscr...@googlegroups.com. > To post to this group, send email to mojolicious@googlegroups.com. > Visit this group at https://groups.google.com/group/mojolicious. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Mojolicious" group. To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscr...@googlegroups.com. To post to this group, send email to mojolicious@googlegroups.com. Visit this group at https://groups.google.com/group/mojolicious. For more options, visit https://groups.google.com/d/optout.
Re: [Mojolicious] Re: Structured helpers
On Wed, Mar 28, 2018 at 2:39 PM, sriwrote: >> That being said, I would ask to the collective wisdom of the group: >> >> 1. would proxy defined as explained in the cookboock work in templates? > > > Technically, you can always do "<%= $c->my_helper_prefix->foo('bar') %>". > When I read this I started banging my head on the desk: I had *completely missed* the paragraph in the rendering guide about the controller being accessible in the template scripts. Now that the headache has subsided, I can reply :) Since the main reason I wanted 'structured' helpers was that I had two helpers that logically should have had the same name but where actually quite different. So I thought to introduce two helpers: foo.foobar, to be used in the context of controller Foo to do foobnar, and bar.foobar meant to do a different foobar in the context of controller Bar. What I did, was to define methods Foo::_foobar and Bar::_foobar and then, in my app setup, define a single foobar helper with $app->helper(foobar => sub { my $c = shift; return $c->_foobar(@_) if $c->$_can('_foobar'); # do something (e.g. croak) to handle the case were the controller has no _foobar }); I tried to define directly Foo::foobar and Bar::foobar, but I get something like Undefined subroutine ::Template::Sandbox::6bd05336357ba117d6ac9a329ea3a6ec::foobar meaning there is something (well, more than some thing) that I do not understand in how the functions are injected into the template object. However, the solution above works for me, and I'm content :). Obviously, I'm open to suggestion for better ways to do this. cheers -- Leo Cacciari Aliae nationes servitutem pati possunt. Populi Romani est propria libertas. -- You received this message because you are subscribed to the Google Groups "Mojolicious" group. To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscr...@googlegroups.com. To post to this group, send email to mojolicious@googlegroups.com. Visit this group at https://groups.google.com/group/mojolicious. For more options, visit https://groups.google.com/d/optout.
[Mojolicious] Re: Structured helpers
> > That being said, I would ask to the collective wisdom of the group: > > 1. would proxy defined as explained in the cookboock work in templates? > Technically, you can always do "<%= $c->my_helper_prefix->foo('bar') %>". > 2. is this the recommended way to organize helpers in 'namespaces'? > There is none, template helpers are meant to be short. > > An explication of the rationale behind the decision of allowing > "nested" helpers only in controllers would also be interesting to > read. > Most helpers are used as functions in templates (like "<%= url_for 'foo' %>"), but helpers with prefix require a method call, which doesn't look as nice in comparison ("<%= my_prefix->url_for('foo') %>") and is no better than "<%= $c->my_prefix->url_for('foo') %>". And of course it's consistent with stash value prefixes. -- sebastian -- You received this message because you are subscribed to the Google Groups "Mojolicious" group. To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscr...@googlegroups.com. To post to this group, send email to mojolicious@googlegroups.com. Visit this group at https://groups.google.com/group/mojolicious. For more options, visit https://groups.google.com/d/optout.