Peter Barry wrote:
>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.
>--i see what you mean if they are all functions then you would need to
>pass parameters
>
>
Indeed.
>--and its not really a big deal to do this.
>
>
You think so? Please show me how it''s not a big deal.
>If you really need to share data across templates, use instance
>variables. These will automatically vanish after request processing.
>--i just wanted to know why you say "If you really need to" ,
suggests
>that i shouldnt usually "need to"
>--i mean is it bad practise or something? I was just removing duplication
>by pulling out a partial and reusing it.
>
>
I did not mean to imply bad practice. But I have not experienced the
need for this, yet. If your partial is meant to work on/display some
object you can pass it to the partial directly (render_partial "path",
object). On the other hand, if the rendering code inside the partial
needs additional parameters, you can pass them via local_assigns
(render_partial "path", object, :x => 5, ...)
>-- Lets say for example in some places you want to loop through a
>collection and display all objects in the collection
>
>
You can do that using render :partial => "path", :collection =>
<your
collection here>, :locals => {:x =>5, ...}
>--And in another place you want to just display one object, but in a
>similair way. Would you not pull out the object rendering code
>--into a partial rhtml?
>
>
Of course I would. But I usually don''t have the need to automatically
pass all local variables of the calling context. It seems clearer to me
to make the required parameters explicit.
-- stefan