I had a problem because the local variables in my .rhtml(s) are not available to any nested partials E.g in parent .rhtml file <%for x in @xs%> <%=render(partial=>''child'')%> ... in child <%= x.someattribute %> this fails as x is not recognised My solution after looking at the apis was to pass in the locals hash containing any locals i want available from one .rhtml into a child .rhtml. You can see the render() docs for info on this. I was scratching my head for a while before going back to the apis on this one. It seems counterintuituve that a nested .rhtml file doesnt have the parents locals available. Is there a real showstopper that prevents the child sharinig the context of the parent (and all locals) or is there a better solution / work around than using the locals=> hash? Peter Barry skype: hairymail YIM: hairy_mail Remember from your perspective, you are always on top of the world. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Peter Barry wrote:>I had a problem because the local variables in my .rhtml(s) are not >available to any nested partials > >E.g in parent .rhtml file ><%for x in @xs%> ><%=render(partial=>''child'')%> >... > >in child ><%= x.someattribute %> > >this fails as x is not recognised > >My solution after looking at the apis was to pass in the locals hash >containing any locals i want available from one .rhtml into a child >.rhtml. You can see the render() docs for info on this. > >I was scratching my head for a while before going back to the apis on this >one. It seems counterintuituve that a nested .rhtml file doesnt have the >parents locals available. > >Is there a real showstopper that prevents the child sharinig the context >of the parent (and all locals) or is there a better solution / work around >than using the locals=> hash? > > >Short answer: the only way to pass local variables from one template to another is via the local_assigns hash. This is both by design, and for technical reasons. Local variables are meant to be local to each template, otherwise you wouldn''t have a local namespace at all. That''s the design. Since 0.14, templates are compiled into ruby functions and you can''t automatically pass local variables outside a function scope. That''s the technical reason. If you really need to share data across templates, use instance variables. These will automatically vanish after request processing. -- stefan
>--and its not really a big deal to do this. > >You think so? Please show me how it''s not a big deal. -- I think you misunderstood me, what I was saying is that its not a big deal to pass parameters inside the function call, I was just agreeing with the way it works now, I wasnt suggesting changing the design inorder to accomodate locals becoming viewable in child .rhtmls without parameter passing. I would never suggest this was not a big deal, especially now that I understand the way .rhtmls become functions. While I can think of at least one way it might be possible and surely there would be a way to do it but that was not what i was refering to by not a big deal, i think it would be a big deal and moreover , it would mess up rails and not enhance it. BTW Thanks for the explaination its helped me understand a bit more. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails