Checking a View Exists

29 views
Skip to first unread message

DAZ

unread,
Mar 4, 2011, 11:35:31 AM3/4/11
to sinatrarb
Is it possible to have check if a view exists?

For example

get '/foo' do
haml :foo IF there is a foo view,
else just display haml :generic
end

cheers,

DAZ

djangst

unread,
Mar 4, 2011, 2:35:29 PM3/4/11
to sinatrarb
If there's no built-in way to accomplish this, you could always try to
load the view from the local file system using standard Ruby
(File.exist?, for example).

Francisco Cabrita

unread,
Mar 4, 2011, 8:36:50 PM3/4/11
to sina...@googlegroups.com, DAZ
Hi DAZ,

Interesting question but, sorry, I can't see how and why I need use this. Lights please :)
 
cheers,

DAZ

--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To post to this group, send email to sina...@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.




--
--

Konstantin Haase

unread,
Mar 5, 2011, 3:49:27 AM3/5/11
to sina...@googlegroups.com
If you're on Sinatra 1.2, try something like this:

helpers do
def find_template(views, name, engine, &block)
super(views, name, engine, &block) # will try to render :foo
super(views, :generic, engine, &block) # if that is missing, try :generic
end
end

get '/foo' do
haml :foo

end

Note that this will use :generic for all templates. You can of course check the value of name and engine.
If you use that with a lot of different fallback templates, try something like this:

helpers do
def template_exists?(engine, name)
find_template(settings.views, name, Tilt[engine]) do |file|
return true if File.exist? file
end
false
end
end

get '/foo' do
if template_exists? :haml, :foo
haml :foo
else
haml :generic
end
end

Konstantin

Zachary Scott

unread,
Mar 5, 2011, 9:00:58 AM3/5/11
to sina...@googlegroups.com
Excellent solution Konstantin, thanks for sharing!

This should go in contrib imo :)

DAZ

unread,
Mar 6, 2011, 3:23:19 PM3/6/11
to sinatrarb
Thanks for the info guys ... and the solution Konstantin (I always
find just looking at your code examples helps me immensley!)

Here is what I'm trying to do (to answer Francisco's question and
hopefully make things clearer):

set :pages, %w[about download contact]

settings.pages.each do |page|
get '/'+page do
@title = page.capitalize
#CODE NEEDS TO GO HERE
end
end

__END__
@@about
Howdy!
========
Welcome to the home page

@@page
h1= @title
p This is the #{@title} page.

Basically, you put your page names in the array, then it generates
some urls for you. The idea is to then create some simple pages using
markdown, but if they haven't been created yet, then the generic page
view is shown using slim.

I'll give Konstantin's code a try and get back to you all.

cheers,

DAZ
> >> For more options, visit this group athttp://groups.google.com/group/sinatrarb?hl=en.
Reply all
Reply to author
Forward
0 new messages