2013-08-03 28 views
0

我正在为我的投资组合制作一个简单的Sinatra静态网站。Sinatra - 制作嵌套的URL?

我想/profile下几页像/profile/history/profile/education

通过下面的官方文档,我可以做到这一点:

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

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

它变得多余。有没有DRY这样做?也许增加一个class什么的?

感谢

回答

2

安装sinatra-contrib和使用Sinatra::Namespace实现这一目标。

实施例:

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

namespace '/profile' do 
    get '/history' { ... } 
    get '/education' { ... } 
end 
+0

完美!如何调用模板?该模板位于'/ profile'子目录中,并将其称为我使用'erb:'profile/history''。任何避免写入子目录的方法?再次感谢 – hrsetyono

+1

似乎'Sinatra :: Namespace'不知道模板子目录。你尝试过'erb:history'吗? – davogones

+0

我试过'erb:history',它不起作用。我必须指定子目录'erb:'profile/history''。那就够了。非常感谢 – hrsetyono

1

使用字符串插值:

prefix = "/profile" 

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

get "#{prefix}/education" do 
    ... 
end 
+0

感谢您的回答,这个人是好的,但上述使用'西纳特拉-contrib'答案是清洁器 – hrsetyono

+0

@DarcCode,我同意。 davogones的回答要好得多。 – falsetru