Falling through to default templates

Hi,

I am using the following in one of my controller to change where it
looks for it’s template:

self.template_root = File.join( RAILS_ROOT, 'themes', theme, 

‘templates’ )

as a way of introducing theme support. However I would really like
the ability to have a “defaults” folder i.e. have the controller look
for templates/partials in the theme and, if it doesn’t find them, in a
default location, e.g. something like:

** imaginary **
self.template_roots = [
File.join( RAILS_ROOT, ‘themes’, theme, ‘templates’ ),
File.join( RAILS_ROOT, ‘themes’, ‘default’, ‘templates’ )
]

I’m just checking to see if anyone knows some way of achieving this
end (perhaps by some other means) right now? And if not if anyone
knows how feasible it will be to modify the controller code
accordingly?

Thanks,

Matt


Matt M. :: http://matt.blogs.it/

The Engines plugin does something similar to this, but you’ve got to
‘patch’ ActionView directly at runtime. Here’s the relevant code,
modified slightly to suit what you’re after(see the original here:
https://opensvn.csie.org/rails_engines/plugins/engines/lib/action_view_extensions.rb):

module ::ActionView
class Base
private
def full_template_path(template_path, extension)

    # Lets assume that AllThemes is an array of directories, which 

would be
# equivalent to the app/views folder in the normal
application, but with the theme
# specific templates in there.
#
# check in the themes to see if the template can be found there.
AllThemes.each do |theme_dir|
theme_specific_path = File.join(theme_dir,
template_path.to_s + ‘.’ + extension.to_s)
return theme_specific_path if File.exist?(theme_specific_path)
end

    # If it cannot be found anywhere, return the default path, where 

the
# user should have put it.
return “#{@base_path}/#{template_path}.#{extension}”
end
end
end

That should get you somewhere closer, hopefully.

  • james

On 15/11/05, James A. [email protected] wrote:

The Engines plugin does something similar to this, but you’ve got to
‘patch’ ActionView directly at runtime. Here’s the relevant code,
modified slightly to suit what you’re after(see the original here:
https://opensvn.csie.org/rails_engines/plugins/engines/lib/action_view_extensions.rb):

James that’s a very useful starting point, many thanks for that.

Regards,

Matt


Matt M. :: http://matt.blogs.it/

On 15-nov-2005, at 13:59, Matt M. wrote:

for templates/partials in the theme and, if it doesn’t find them, in a
knows how feasible it will be to modify the controller code
accordingly?

This was recently being discussed. I do just that (mainly because of
partials).

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


Julian “Julik” Tarkhanov