Hi Railers! I am trying to understand the logic of making shared templates override the normal ones. For example, I would like to have a partial in my app which is called "toolbar". In the "shared" toolbar I have: # Some stuff * Change settings * Logout In the specific toolbar (for instance in "foo/_toolbar") I have # What to do with foos * Grok a foo * Make a foo * List foos <%= render_partial ''shared/toolbar'' %> As a bonus my layout (1 per app) would pick up it''s own toolbar(if it has one) or the shared toolbar - would be super-neat and also more predictable. So why actually do shared templates override the normal ones (not the opposite - as I would think) and is it possible to change this behaviour somehow? (tinker with lookup order of ActionView). -- Julian "Julik" Tarkhanov
Hi Julian,
I''m not sure if this is on-target for you, but in one of my projects
we use the following code to over-ride the default behaviour by
preferring controller-specific partials before defaulting to the
"layouts" folder. You could change it to default to the
''shared''
folder if you''d like:
class ActionView
# Prefer controller-specific templates and partials if they exist.
# Otherwise, use files in the layouts folder.
class Base
private
def full_template_path(template_path, extension)
# Prefer the controller-specific (regular) partial first
preferred_path =
"#{@base_path}/#{template_path}.#{extension}"
# Check to see if the partial exists in our layouts folder
second
secondary_path = ("#{@base_path}/layouts/#
{template_path.split("/")[1]}.#{extension}" rescue
preferred_path)
if File.exist?(preferred_path)
preferred_path
elsif File.exist?(secondary_path)
secondary_path
else
# Even though the preferred file doesn''t exist, return it
# so a reasonable error message can be given
preferred_path
end
end
end
end
On Oct 2, 2005, at 8:01 AM, Julian ''Julik'' Tarkhanov wrote:
> Hi Railers!
>
> I am trying to understand the logic of making shared templates
> override the normal ones.
> For example, I would like to have a partial in my app which is
> called "toolbar".
>
> In the "shared" toolbar I have:
>
> # Some stuff
> * Change settings
> * Logout
>
> In the specific toolbar (for instance in "foo/_toolbar") I have
>
> # What to do with foos
> * Grok a foo
> * Make a foo
> * List foos
> <%= render_partial ''shared/toolbar'' %>
>
> As a bonus my layout (1 per app) would pick up it''s own toolbar(if
> it has one) or the shared toolbar - would be super-neat and also
> more predictable.
>
> So why actually do shared templates override the normal ones (not
> the opposite - as I would think) and is it possible to change this
> behaviour somehow? (tinker with lookup order of ActionView).
> --
> Julian "Julik" Tarkhanov
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
On 3-okt-2005, at 16:37, Duane Johnson wrote:> class ActionViewRight on the spot Duane! I tried diggingng into ActionView myself but it had like 25 methods for template lookup calling each other in no understandable order. Modified to my needs this looks so: module ActionView # Prefer controller-specific templates and partials if they exist. # Otherwise, use files in the layouts folder. class Base private def full_template_path(template_path, extension) # Prefer shared partial first (Rails default) shared_path = "#{@base_path}/#{template_path}.#{extension}" # Prefer the controller-specific (regular) partial second private_path = "#{@base_path}/#{template_path}.#{extension}" # Check to see if the partial exists in our "default" folder last default_path = ("#{@base_path}/default/#{template_path.split ("/")[1]}.#{extension}" rescue private_path) if File.exist?(shared_path) shared_path elsif File.exist?(private_path) private_path elsif File.exist?(default_path) default_path else # Even though the preferred file doesn''t exist, return it # so a reasonable error message can be given private_path end end end end This will use ''default'' directory under /views to lookup missing templates. Layouts is for layouts :-) And yet it seems to me that using templates from ''shared'' as overrieds is incorrect. -- Julian "Julik" Tarkhanov