1

Website类中,我可以获得 root 为settings.root. 在课堂之外,我看不到如何获得类对象的句柄。

我可以在路由块内插入一个实例变量@root = settings.root,这将使 HAML 的 root 可用。这是正确的方法吗?

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

1 回答 1

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变量。

于 2013-03-06T16:12:40.770 回答