# Falling through to default templates

**URL:** <https://www.ruby-forum.com/t/falling-through-to-default-templates/47953>\
**Category:** Rails\
**Created:** [November 15, 2005, 2:02pm UTC](https://www.ruby-forum.com/t/falling-through-to-default-templates/47953 "2005-11-15T14:02:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt.mower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@matt.mower](https://www.ruby-forum.com/u/matt.mower)\
**Post date:** [November 15, 2005, 2:02pm UTC](https://www.ruby-forum.com/t/falling-through-to-default-templates/47953/1 "2005-11-15T14:02:07Z")

</div>

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/](http://matt.blogs.it/)

---

<div class="post-metadata">

**Author:** ![matt.mower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@matt.mower](https://www.ruby-forum.com/u/matt.mower)\
**Post date:** [November 15, 2005, 4:08pm UTC](https://www.ruby-forum.com/t/falling-through-to-default-templates/47953/2 "2005-11-15T16:08:54Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![matt.mower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@matt.mower](https://www.ruby-forum.com/u/matt.mower)\
**Post date:** [November 15, 2005, 9:40pm UTC](https://www.ruby-forum.com/t/falling-through-to-default-templates/47953/3 "2005-11-15T21:40:41Z")

</div>

On 15/11/05, James A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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/](http://matt.blogs.it/)

---

<div class="post-metadata">

**Author:** ![matt.mower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@matt.mower](https://www.ruby-forum.com/u/matt.mower)\
**Post date:** [November 16, 2005, 12:39pm UTC](https://www.ruby-forum.com/t/falling-through-to-default-templates/47953/4 "2005-11-16T12:39:09Z")

</div>

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
