Multiple view paths?

0 views
Skip to first unread message

Nate W

unread,
Oct 30, 2009, 10:11:55 AM10/30/09
to sinatrarb
Is there a way to set multiple view paths? If I try to specify an
array:

set :views, ['/path/1', '/path/2']

These are just concatenated:

No such file '/path/1/path/2/foo/bar'

This is something we have used alot in Rails to deal with skinning an
app. It's quite useful - put your skins in a skins/ directory and
then you can choose whether or not to override a specific page on a
case-by-case basis.

Thanks
Nate

Christopher Schneider

unread,
Oct 30, 2009, 11:56:48 AM10/30/09
to sina...@googlegroups.com
That isn't built into sinatra, but is easy to fake with a custom
render helper.

A very rough version to give you an idea:

before do
@skin = determine_skin.....
end

helpers do
def skin(template, options={})
view = "views/#{@skin}/#{template}"
erb view.to_sym
end
end

Graham Ashton

unread,
Nov 15, 2009, 12:31:24 PM11/15/09
to sina...@googlegroups.com, nwi...@gmail.com
2009/10/30 Nate W <nwi...@gmail.com>:

>
> Is there a way to set multiple view paths?  If I try to specify an
> array:
>
>   set :views, ['/path/1', '/path/2']
>
> These are just concatenated:
>
>   No such file '/path/1/path/2/foo/bar'

I've just implemented something like this in Nesta. I wanted to be
able to override the default views/foo.haml template by creating an
alternative in local/views/foo.haml that will only be used if it
exists on disk.

Ultimately all you need to do is pass the alternative view directory
to the template rendering method in the options hash:

haml(:foo, :views => "my/alternative/view/path")

Here's the patch I just committed to Nesta - it should be pretty easy
to work out how to re-use it elsewhere:

http://github.com/gma/nesta/commit/fa82d2706bc39aa3d708058721499a20c5d0dec4

Cheers,

Graham

Reply all
Reply to author
Forward
0 new messages