2013-08-03 25 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