Forum: Ruby on Rails Falling through to default templates

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Efbfad4ef638cfb7d1145ead4870e2d0?d=identicon&s=25 matt.mower (Guest)
on 2005-11-15 14:02
(Received via mailing list)
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 Mower :: http://matt.blogs.it/
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 james.adam (Guest)
on 2005-11-15 16:08
(Received via mailing list)
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/eng...

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
Efbfad4ef638cfb7d1145ead4870e2d0?d=identicon&s=25 matt.mower (Guest)
on 2005-11-15 21:40
(Received via mailing list)
On 15/11/05, James Adam <james.adam@gmail.com> 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/eng...
>

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

Regards,

Matt

--
Matt Mower :: http://matt.blogs.it/
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 listbox (Guest)
on 2005-11-16 12:39
(Received via mailing list)
On 15-nov-2005, at 13:59, Matt Mower 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
This topic is locked and can not be replied to.