Couldn’t get that last thing to work so keeping it simple (stupid)

This could undoubtedly be more elegant, but it’s late and I want it to work now. May take another stab at it later…

TODO: limit the content types and only allow rendering if they are ok.

Setting up a before filter:

1
2
3
4
5
6
7
before do
  # remove and grab the file extension
  request.path_info.sub! %r{\.([^\./]+)$}, ''
  @format=$1 || 'html'
  @charset=mime_type($1) || 'text/html'
  content_type @charset, :charset => 'utf-8'
end

and using a case statement:

1
2
3
4
5
6
7
8
9
10
11
get "/home" do
  case @format
  when 'html'
    @stylesheet='home.css'
    haml :home, :layout=>:layout_simple
  when 'js'
    "{'js':true}"
  else
    pass
  end
end
Posted: April 1st, 2010 | Author: | Filed under: Code | Tags: , , , , | No Comments »

Doing something different depending on file extension (MIME type) in Sinatra

This looks like a solution: sinatra-respond_to

Posted: March 31st, 2010 | Author: | Filed under: Code | Tags: , , , | No Comments »