0

I'm making a simple static site with Sinatra for my portfolio.

I want several pages under /profile like /profile/history and /profile/education

By following the official documentation, I can achieve this by:

get "/profile/history" do
    ...
end

get "/profile/education" do
    ...
end

It become redundant. Is there DRY way to do this? Maybe adding a class or something?

Thanks

4

2 回答 2

2

安装sinatra-contrib并使用Sinatra::Namespace来实现这一点。

例子:

require "sinatra/base"
require "sinatra/namespace"

namespace '/profile' do
  get '/history' { ... }
  get '/education' { ... }
end
于 2013-08-03T09:20:39.837 回答
1

使用字符串插值:

prefix = "/profile"

get "#{prefix}/history" do
    ...
end

get "#{prefix}/education" do
    ...
end
于 2013-08-03T09:20:31.927 回答