2013-03-06 79 views
1

Website裏面,我可以獲得root爲settings.root。在課堂之外,我看不到如何獲得類對象的句柄。我可以訪問網站應用程序類以外的Sintra設置嗎?

我可以在路由塊內插入一個實例變量@root = settings.root,這將使根可用HAML。這是正確的方式嗎?

class Website < Sinatra::Base 
    configure do 
    set :root, File.dirname(__FILE__) 
    end 
    get '/' do 
    haml :index, :layout => :base 
    end 

回答

1

我認爲正確的方法是使用:locals哈希作爲haml調用的參數,如:

class Website < Sinatra::Base 
    configure do 
    set :root, File.dirname(__FILE__) 
    end 
    get '/' do 
    haml :index, :layout => :base, :locals => {:root_path => settings.root} 
    end 
end 

在你的視圖的模板,你將有訪問一個root_path變量。

+0

非常好!我可以通過'locals [:root_path]'來訪問它 – Todd 2013-03-06 16:26:51

相關問題