cloudhead / toto

the 10 second blog-engine for hackers
MIT License
1.49k stars 245 forks source link

Custom error pages #4

Open cloudhead opened 14 years ago

cloudhead commented 14 years ago

Current workaround: patch Site#http:

http://github.com/cloudhead/toto/blob/master/lib/toto.rb#L110-112

ghost commented 14 years ago

I'm guessing there's no way to customize the Kansas message?

cloudhead commented 14 years ago

Read above : )

nogweii commented 14 years ago

Fixed in evaryont/toto@32b4d9ba75c0bfb99bcc2a60e24f618bfb929fde

To use it, just add set :error_page lambda { |code| "lol you got an error (#{code})" } to your config.ru

ariejan commented 14 years ago

Nice! But can it display a custom page instead?

nogweii commented 14 years ago

Not as easily as you may like, though you can use the go() method, so I think (this is just off the top of my head) go "/error/#{code}.html" should work, if you want to use static pages.

Of course, you can use go in any manner, redirecting to any (local) URL.

davejacobs commented 14 years ago

I get the following when I try to use go "/error/404" (which I can navigate to directly, so I know my page works):

NoMethodError at /2010/04/01/this-is-a-test4
undefined method 'go' for #<Toto::Config:0x10172a6f8>
cloudhead commented 14 years ago

This is now official, and the setting is just called :error

For custom pages, you could probably just load the file with File.read("public/#{code}.html") or something similar..

samnang commented 13 years ago

How could we render the 404 page with existing layout?

ariejan commented 13 years ago

Try this (I'm using HAML):

set :error do |code|
  ::Haml::Engine.new(File.read("templates/pages/#{code}.haml"), :layout => 'layout', :format => :html5, :ugly => true).render("templates/layout")
end
samnang commented 13 years ago

ariejan, how about using ERB?

ariejan commented 13 years ago

@samnang - I don't use ERB, but it could be something like this:

ERB.new(File.read("templates/pages/layout.rhtml")).result(File.read("templates/pages/#{code}.rhtml"))
samnang commented 13 years ago

@ariejan, I got an error because I have to pass binding object to ERB#result method instead of string.

ixti commented 13 years ago

The easiest way is to:

  # ...
  set :error, lambda {|code|
    ERB.new(File.read("templates/pages/error-#{code}.rhtml")).result
  }
  # ...