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).
On Mar 4, 11:35 am, DAZ <daz4...@gmail.com> wrote:
On Fri, Mar 4, 2011 at 4:35 PM, DAZ <daz4...@gmail.com> wrote: > 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
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 sinatrarb@googlegroups.com. > To unsubscribe from this group, send email to > sinatrarb+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sinatrarb?hl=en.
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
> get '/foo' do > haml :foo IF there is a foo view, > else just display haml :generic > end
> cheers,
> DAZ
> -- > You received this message because you are subscribed to the Google Groups "sinatrarb" group. > To post to this group, send email to sinatrarb@googlegroups.com. > To unsubscribe from this group, send email to sinatrarb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
On Sat, Mar 5, 2011 at 3:49 AM, Konstantin Haase <k.ha...@finn.de> wrote: > 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
> On Mar 4, 2011, at 17:35 , DAZ wrote:
>> 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
>> -- >> You received this message because you are subscribed to the Google Groups "sinatrarb" group. >> To post to this group, send email to sinatrarb@googlegroups.com. >> To unsubscribe from this group, send email to sinatrarb+unsubscribe@googlegroups.com. >> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
> -- > You received this message because you are subscribed to the Google Groups "sinatrarb" group. > To post to this group, send email to sinatrarb@googlegroups.com. > To unsubscribe from this group, send email to sinatrarb+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
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
On Mar 5, 2:00 pm, Zachary Scott <zachary.s.sc...@gmail.com> wrote:
> Excellent solution Konstantin, thanks for sharing!
> This should go in contrib imo :)
> On Sat, Mar 5, 2011 at 3:49 AM, Konstantin Haase <k.ha...@finn.de> wrote:
> > 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
> > On Mar 4, 2011, at 17:35 , DAZ wrote:
> >> 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
> >> --
> >> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> >> To post to this group, send email to sinatrarb@googlegroups.com.
> >> To unsubscribe from this group, send email to sinatrarb+unsubscribe@googlegroups.com.
> >> For more options, visit this group athttp://groups.google.com/group/sinatrarb?hl=en.
> > --
> > You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> > To post to this group, send email to sinatrarb@googlegroups.com.
> > To unsubscribe from this group, send email to sinatrarb+unsubscribe@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/sinatrarb?hl=en.